0

Here's an abrevated version of what my code looks like:

public class ColorFactory extends JFrame {
            public ColorFactory(){
                buildTopPanel();
            }

            public void buildTopPanel(){
            JPanel topPanel = new JPanel();
            this.add(topPanel, BorderLayout.NORTH);
            }

}

As you can see I have a method that makes a new JPanel object when called. How can I access that particular JPanel object from another class? I have a button listener class that I want to change the color of the JPanel from outside the ColorFactory class. This code is right after the ColorFactory class.

public class ButtonListener implements ActionListener{
     public void actionPerformed(ActionEvent e) {
           //Change JPanel color here. 
     }
}

Would it be better just to instantiate JPanel in the ColorFactory constructor and then just access it through there?

3 Answers 3

1

For starters, you need to make the JPanel a field in ColorFactory, so references to it don't disappear when you exit buildTopPanel(). Once you've saved a reference to it, then you have a couple of choices. From the design standpoint, the bad choice is to expose it, e.g.:

JPanel getTopPanel(){
    return topPanel;
}

The better choice is to have your action listener send a message to ColorFactory that says "respondToButton(Color newColor)", and have ColorFactory decide to change topPanel's color... e.g.:

public void respondToButton(Color newColor){     
    topPanel.setBackground(newColor);
}
Sign up to request clarification or add additional context in comments.

Comments

0

You are facing a design issue; in general, this type of situations require more investigation to understand how to end up with a clean and maintainable design. However, For the specific problem you are reporting, I would:

  1. Create a constructor of ButtonListener that receives a parameter (i.e. the ColorFactory) which could access the information you need, so that you can initialize a field in ButtonListener itself
  2. Create a method changeColor in the ColorFactory. This method actually applies the color change
  3. In the ButtonListener, invoke changeColor on the field, i.e. the reference to the ColorFactory

Comments

0

You should make the JPanel a field of the class like this:

public class ColorFactory extends JFrame {

        JPanel topPanel;


        public ColorFactory(){
            buildTopPanel();
        }

        public void buildTopPanel(){
            topPanel = new JPanel();
            this.add(topPanel, BorderLayout.NORTH);
        }

        public void changeColor(Color color) {
            //color changing code here
        }


}

now You can get the JPanel from another class. All you have to do now, is get the ColorFactory into your Button listener:

public class ButtonListener implements ActionListener{

    ColorFactory colorFactory;
    public ButtonListener(ColorFactory colorFactory) {
        this.colorFactory = colorFactory;
    }

    public void actionPerformed(ActionEvent e) {
           colorFactory.changeColor(/* color here */);
    }
}

1 Comment

I strongly discourage accessing the top panel from a listener. One could break everything by misusing the panel in a dependent (component) class. A panel should manage itself and expose methods, to which it can react according to its current state.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.