0

My program looks like this!

enter image description here

I want to have the bottom part dynamically load a frame into the bottom frame depending on the item selected in the ComboBox. For example, if the first item is selected I want a panel from the PresentValue.java file displayed. The idea is that I have one java file for each selection that displays what I design in its respective java file.

enter image description here

enter image description here

These two java files should be put into the "bottom" box from my first screenshot, depending on the selection from the combobox.

I'm more used to Android programming and there I would simple call the replace method from fragments to swap out the fragment loaded... looking for the analogy here.

final JComboBox selectorBox = new JComboBox(selection);
    selectorBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            int selectionID = selectorBox.getSelectedIndex();

        }
    });

but cant find a way to do what I want to do. Please explain.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Killerpixler
  • 4,200
  • 11
  • 42
  • 82

2 Answers2

4

For each Java file that you have, the output of that Java file should be a JPanel. Not a JFrame.

Before you display anything, execute all of the Java files you have. Create all of the possible JPanels.

Create your JFrame in your GUI, then use the remove and add methods of JFrame to remove or add the desired JPanel.

Here's an example from one of my GUI's.

public void updatePartControl() {
    Thread thread = new CountdownThread(model, this, displayPanel);
    thread.start();

    frame.remove(alarmPanel.getPanel());
    frame.add(displayPanel.getPanel());
    frame.validate();
    frame.pack();
    frame.setBounds(getBounds());
}

The setBounds method resets the bounds if the display JPanel is bigger or smaller than the alarm JPanel.

Your application should have one JFrame. You use multiple JPanels to create your GUI.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
3

Changing the bottom component will depend on the layout manager that are using. CardLayout is purpose designed for swapping panels.

public void actionPerformed(ActionEvent arg0) {
   int selectionID = selectorBox.getSelectedIndex();
   if (selectionID == 0) {
      cardLayout.show(basePanel, SELECTED_1);
   } 
   // handle other selections

}
Reimeus
  • 158,255
  • 15
  • 216
  • 276