0

I'm having problem with adding another JPanel to my frame when I'm using combobox. I want to change the center panel according to the selection in combobox. I made differehnt panel to all selections but it didn't add to my main panel. I added the code.

thanks :)

import AllClasses.FlightCompany;
{
public class WorkerDialog extends JFrame {
    private JPanel Worker;
    private String[] LabelNames = { "Worker Type:", " Worker Name:" };
    String [] str = { "Office", "Host",
    "Pilot" };
    JComboBox<String> ChooseBox = new JComboBox<>(str);
    public JPanel MainPanel;
    private JPanel [] p= new JPanel[3];

    public WorkerDialog(FlightCompany f) {
        super("Worker Dialog");

        p[0] = new Office_Gui();
        p[1] = new Host_Gui();
        p[2] = new Pilot_Gui();
        Worker = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
        JLabel LabelName = new JLabel(LabelNames[0]);
        JLabel LabelName2 = new JLabel(LabelNames[1]);
        JTextField fieldBox = new JTextField();
        LabelName.setSize(40, 25);
        ChooseBox.setPreferredSize(new Dimension(180, 22));
        Worker.add(LabelName);
        Worker.add(ChooseBox);
        Worker.add(LabelName2);
        fieldBox.setPreferredSize(new Dimension(180, 22));
        Worker.add(fieldBox);
        JPanel AddPanel = new JPanel(new GridLayout(2, 1, 1, 1));
        AddPanel.add(new JButton("Add"));
        AddPanel.add(new JButton("TakeOff"));
        MainPanel = new JPanel(new BorderLayout(3, 3));
        AddPanel.setPreferredSize(new Dimension(0, 110));
        ChooseBox.addItemListener(new ItemListener() {



            @Override
            public void itemStateChanged(ItemEvent e) {
                // TODO Auto-generated method stub
                //String str = e.getActionCommand();
                String jb =  (String) ChooseBox.getSelectedItem();
                if (jb.equals("Office")){

                    MainPanel.add(p[0],BorderLayout.CENTER);
                    System.out.println("Office");}
            }
        });








        MainPanel.add(Worker, BorderLayout.NORTH);
        MainPanel.add(AddPanel, BorderLayout.SOUTH);
        add(MainPanel);
        //pack();
        setSize(560, 300);
        setAlwaysOnTop(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
user2986655
  • 21
  • 1
  • 3

1 Answers1

1

What you want to do is use a CardLayout for your mainPanel, that will allow you to easily switch between panels. Then add all your panels to the mainPanel, specifying a name for the panel. That name will go in the combo box. When you want to show a certain panel, just call cardLayout.show(mainPanel, "nameOfPanel")

To learn more about Cardlayout see How to Use CardLayout. You can also see a simple example here

An aside: Use Java naming convention. Variables begin with lower case letters, using camel casing. ie:

  • ChooseBoxchooseBox
  • MainPanelmainPanel
  • etc.
Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720