0

I have a gui class that extends the JFrame. I separated the JPanels for code readability. I defined combobox from top panel and I would like to access it's selected item to my center panel. My center panel is a grid panels that is clickable. How can I access the selected item from combo box inside my BoxListener event?

My code goes here:

       //Gui ==================================================
    public class Gui extends JFrame  {

        final int WINDOW_WIDTH = 1000; // Window width in pixels
        final int WINDOW_HEIGHT = 800; // Window height in pixels

        private TopPanel topPanel;
        private CenterPanel centerPanel;

        public SchedulerGui() {
            // Display the title
            setTitle("Class Scheduler");

            setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

            // specify action for the close button
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            // Create border layout
            setLayout(new BorderLayout());

            // create the custom panels;
            topPanel = new TopPanel();
            centerPanel = new CenterPanel(15,7);

            // Add it to the content pane
            add(topPanel, BorderLayout.NORTH);
            add(centerPanel, BorderLayout.CENTER);

            setVisible(true);
        }


        public static void main(String args[]) {
            new Gui();
        }
    }


    //top panel =====================================================


    public class TopPanel extends JPanel {

        JLabel labelCurrentStatus;

        // create combo boxes
        public JComboBox nameBox;

        String[] listNameBox = { "Select Box”, “Box1”, “Box2”, “Box3”};

        String selectedNameBox = "";

        public TopPanel() {
            nameBox = new JComboBox(listNameBox);

            // Register an action listener 
            nameBox.addActionListener(new ComboBoxListener());

            // add the combo boxes into the content pane
            add(nameBox);
        }

        private class ComboBoxListener implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent e) {
                selectedNameBox = (String) nameBox.getSelectedItem();
                labelCurrentStatus.setText(selectedNameBox);
            }
        }

    }


    //center panel ================================================
    // creates panel grids that is clickable

    public class CenterPanel extends JPanel {

        public CenterPanel(int row, int col) {

            setLayout(new GridLayout(row, col));
            setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));

            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                    JPanel pan = new JPanel();

                    pan.setEnabled(true);
                    pan.setBackground(Color.WHITE);
                    pan.setPreferredSize(new Dimension(3, 3));
                    pan.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                    // an exception to not click the top row and most left column headers
                    if (i != 0 && j != 0) {
                        pan.addMouseListener(new BoxListener()); // add a mouse listener to make the panels clickable
                    }
                    // set names for each panel for later use
                    pan.setName("PANEL_" + i + "_" + j);
                    add(pan);
                }

            }
        }

        //Class that defines what happens when a panel is clicked
        public static class BoxListener extends MouseAdapter
        {
            public void mouseClicked(MouseEvent me)
            {   
                  JPanel clickedBox =(JPanel)me.getSource(); 
                  clickedBox.setBackground(Color.RED); 

        // insert here the code defining what happens when a grid is clicked. 
// Need to access the value of the selected item from the combo box
            }
        }

    }
tdel
  • 899
  • 1
  • 7
  • 12
  • Best to try to MVC-ize your code, separate the logic from the view if possible. Also, none of your fields should be `public` and in fact all should be `private`. Any communication between objects should be in a controlled fashion through public methods. – Hovercraft Full Of Eels Dec 02 '16 at 03:53
  • 1
    Arggghhh taking too much time to create an MVC example. tdel, -- check out @trashgod's excellent example: [here](http://stackoverflow.com/questions/10523343/how-to-wire-one-pane-to-another) – Hovercraft Full Of Eels Dec 02 '16 at 04:41
  • 1
    Note: `Gui` should not extend `JFrame`, and neither `TopPanel` nor `CenterPanel` should extend `JPanel`. In all three cases, the code should just use plain instances of the component, then add other components to them. – Andrew Thompson Dec 02 '16 at 04:59
  • 1
    Possible duplicate of [*How to wire one pane to another*](http://stackoverflow.com/q/10523343/230513), also examined [here](http://stackoverflow.com/a/3072979/230513). – trashgod Dec 02 '16 at 05:05

1 Answers1

0

Well, I think you ran into this problem because your setup is flawed. The only way I see around this is creating a GetSelectedNameBox() method in that Top panel Class, and a set method in your Center panel class.

So in your Gui class you would do something like:

String temp=topPanel.getSelectedNamebox();

And then you would do centerPanel.setXXXX(temp);

You can see that in an effort to create a more readable code by splitting things up, it probably makes readability worse. Furthermore, this is not the proper use of getters and setters.

I would reconfigure, and put all your different JPanels in the same class. I would also focus on using anonymous classes, rather than inner classes, for your actionListeners. This will shorten up your code very much. And get rid of all the blank lines :P

Nick Ziebert
  • 1,258
  • 1
  • 9
  • 17