I have a JFrame, a JSplitPane as the container of the frame.
On the left side of the splitpane, there are two radio buttons (more to come, but I am stuck).
On the right side, there is a CardLayout. This CardLayout has two 'cards' (JPanels, actually). And to test if it shows the panels when I switch them, I put one checkbox each.
But it does not show anything. I cannot think anything to do. I tried to put only the checkboxes thinking maybe it only accepts components, but it didn't work.
This is the GUI class I have. I have commented what I am trying to do so that you don't have to read everything. Also, it does not have the imports and it is started from another class.
Since I am just testing, I created a project for this, so the main class main method just starts this.
public class SolveSomehow extends JFrame{ private JSplitPane splitPane; private JPanel leftPanel,rightPanel,panelOne,panelTwo; private JCheckBox cbTwo,cbOne,cbCardOne,cbCardTwo; private CardLayout cardLay; private final String ONE = "PanelOne"; private final String TWO = "PanelTwo"; public SolveSomehow() { setResizable(false); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setBounds(100, 100, 450, 300); // I use WindowBuilder as GUI designer and it had a JPanel as the container. // I deleted and added splitPane. splitPane = new JSplitPane(); splitPane.setResizeWeight(0.5); setContentPane(splitPane); //Next 6 lines I put panels to the left and right side of the splitpane. leftPanel = new JPanel(); splitPane.setLeftComponent(leftPanel); leftPanel.setLayout(null); //WindowBuilder made this null but it actually has absoluteLayout rightPanel = new JPanel(); splitPane.setRightComponent(rightPanel); // I have to define the cardLay before the rightPanel, or else it does not recognize it // as the parent. [show(Container parent, String name);] cardLay = new CardLayout(0, 0); rightPanel.setLayout(cardLay); //Next 6 lines are about the left side of the splitPane. I add my checkboxes cbTwo = new JCheckBox("Box Two"); cbTwo.setBounds(6, 7, 97, 23); leftPanel.add(cbTwo); cbOne = new JCheckBox("Box One"); cbOne.setBounds(6, 34, 97, 23); leftPanel.add(cbOne); // The two panels I try to add to the cardLayout. panelOne = new JPanel(new FlowLayout()); panelTwo = new JPanel(new FlowLayout()); // Adding the checkboxes to the panels before adding them into the cardlayout. cbCardOne = new JCheckBox("Panel One"); cbCardTwo = new JCheckBox("Panel Two"); //Finally, boxes are added to the panel panelOne.add(cbCardOne); panelTwo.add(cbCardTwo); //Panels are added to the cardLayout cardLay.addLayoutComponent(panelOne, ONE); cardLay.addLayoutComponent(panelTwo, TWO); //I want it to show the relevant panel when the checkbox gets the focus. //So I wrote this Focus Adapter. cbTwo.addFocusListener(new FocusAdapter(){ @Override public void focusGained(FocusEvent arg0) { System.out.println("Did it change?"); //Testing if 'focus' is what I thought, cardLay.show(rightPanel, ONE); //And it is what I thought. } }); cbOne.addFocusListener(new FocusAdapter(){ @Override public void focusGained(FocusEvent arg0) { System.out.println("Didn't it change?"); cardLay.show(rightPanel, TWO); } }); } }
What am I doing wrong?