-2

I've got a problem with changing JPanels color from another class. I'm trying to change it by clicking a button. I am creating an application where user can pick a colour theme what he wants.

First class:

public class First extends JFrame {

    public JPanel contentPane = new JPanel();
    public JPanel panel = new JPanel();

    public First() {

        JButton btnNewButton = new JButton("New button");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                Second s = new Second();
                s.startSecond();
            }
        });
    }
 }

Second class:

public class Second extends JFrame {
    First f;
    private JPanel contentPane;

    public static void startSecond() {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Second sframe = new faijaso();
                    sframe.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Second() {   
        JButton btnNewButton = new JButton("New button");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                f.panel.setBackground(Color.red);
                f.panel.repaint();
                f.panel.revalidate();
            }
        });
    }

}
Sampson
  • 13
  • 1
  • Please tell the problems that you're having. From a quick glance at the code, it looks like this will throw a NullPointerException because I don't see that `f` the First variable in Second, is ever initialized. – DontKnowMuchBut Getting Better Nov 26 '17 at 14:26
  • Also for general relevant recommendations that tell you best practice, but which won't solve your current problem, please look at [The Use of Multiple JFrames: Good or Bad Practice?](https://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-or-bad-practice) – DontKnowMuchBut Getting Better Nov 26 '17 at 14:27
  • It looks like you want to pass your `this` instance of First into your Second's constructor and use it to initialize the `f` variable. You will want to get rid of the static code in Second and instead wire up the GUI within its constructor. – DontKnowMuchBut Getting Better Nov 26 '17 at 14:28

1 Answers1

1

You have several problems with this code, but most importantly is that your First f variable is never initialized leaving it null, and so if you try to call a method off of it, the program will throw a NullPointerException. You certainly don't want to create a new First() and assign that to f since that instance will not be the same as the visualized First that already exists. Instead change the Second's constructor to accept a First argument, and then pass in the current instance when you call the Second's constructor.

public Second(First f) {   
    this.f = f;
    // ....

and in First:

public First() {

    JButton btnNewButton = new JButton("New button");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            Second s = new Second();
            s.startSecond(First.this);  // pass in the First instance
        }
    });
}

Other problems --

  • get rid of the static code in Second as there's no need for this. Instead set the Second visible within First's ActionListener.
  • You really don't want to use two JFrames, and this link will tell you why: The Use of Multiple JFrames: Good or Bad Practice?.
  • Instead have Second be a JDialog.