Skip to content

Instantly share code, notes, and snippets.

@RustyKnight
Created May 5, 2018 04:14
Show Gist options
  • Save RustyKnight/a743eb9ff03603d13d1e7b8ad6c2bbdb to your computer and use it in GitHub Desktop.
Save RustyKnight/a743eb9ff03603d13d1e7b8ad6c2bbdb to your computer and use it in GitHub Desktop.
Testing `ResizableCardLayout` proposed as an answer to https://stackoverflow.com/questions/50184994/cardlayout-in-java-gui - but which doesn't do anything that `CardLayout` doesn't already do
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// ResizableCardLayout cardLayout = new ResizableCardLayout(frame::pack);
CardLayout cardLayout = new CardLayout();
JPanel base = new JPanel(cardLayout);
base.add(makePanel(200, 200, Color.RED), "red");
base.add(makePanel(400, 400, Color.BLUE), "blue");
frame.add(base);
JButton blue = new JButton("Blue");
blue.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cardLayout.show(base, "blue");
// cardLayout.showAndResize(base, "blue");
}
});
JButton red = new JButton("red");
red.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cardLayout.show(base, "red");
// cardLayout.showAndResize(base, "red");
}
});
JPanel buttons = new JPanel();
buttons.add(red);
buttons.add(blue);
frame.add(buttons, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public TestPane makePanel(int width, int height, Color background) {
TestPane pane = new TestPane();
pane.setPreferredSize(new Dimension(width, height));
pane.setBackground(background);
return pane;
}
public class TestPane extends JPanel {
private JLabel label;
public TestPane() {
label = new JLabel("...");
setLayout(new GridBagLayout());
add(label);
}
@Override
public void invalidate() {
super.invalidate();
label.setText(getWidth() + "x" + getHeight());
}
}
class ResizableCardLayout extends CardLayout {
private final Runnable resize;
public ResizableCardLayout(Runnable resize) {
this.resize = resize;
}
public void showAndResize(Container deck, String cardName) {
show(deck, cardName);
resize.run();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment