Home > database >  How can I access a JButton on another file or class in Java
How can I access a JButton on another file or class in Java

Time:10-20

I'm having a problem with JButton. I need to change the text on the goPauseButton when it has been clicked, but I get this error: goPauseButton cannot be resolved. I'm quite new to Java, so I started trying to solve the issue using techniques from other languages such as Free Pascal. There you need to refer to the class where the button is in, and then the button. In my code it would look like this:

PrisonersDilemma.goPauseButton.setText("Pause");

But then I get this error: Cannot make a static reference to the non-static field PrisonersDilemma.goPauseButton

This is my code (so far), I've erased unimportant things:

Main class

import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.util.Hashtable;

//...

public class PrisonersDilemma /* possible extends... */ {
    // declaring
    JFrame frame;
    PlayingField field;
    JPanel componentPanel;
    public JButton goPauseButton;

    public JPanel createComponentPanel() {

        componentPanel = new JPanel();
        componentPanel.setLayout(new GridLayout(2,6));
        
        // set goPauseButton
        goPauseButton = new JButton("GO!");
        goPauseButton.addActionListener(field);
        goPauseButton.setBounds(110,350, 80,20); // first coordinates, then size
        frame.add(goPauseButton);

        return componentPanel;
    }

    void buildGUI() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                field = new PlayingField();

                // set frame
                frame = new JFrame("Prisoners Dilemma");
                frame.add(field);

                createComponentPanel();

                frame.add(field, BorderLayout.CENTER);
                frame.setLocation(200, 200);
                frame.pack();
                frame.setVisible(true);

                frame.setSize(400, 450);
                frame.setVisible(true);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }

        } );
    }

Class with ActionEventHandler

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JButton;

public class PlayingField extends JPanel 
                          implements ActionListener, 
                                     ChangeListener {
    
    private boolean started;

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO
        if ("GO!".equals(e.getActionCommand())){
            System.out.println("GO!");
            started = true;

            goPauseButton.setText("Pause"); // here is the error

        } else if ("Pause".equals(e.getActionCommand())){
            System.out.println("Pause");
            started = false;
        } else if ("Reset".equals(e.getActionCommand())){
            System.out.println("Reset");
        }
    }
}

CodePudding user response:

I think you need to change the way you're approaching the problem. The PlayingField has no responsibility for modifying the state of the goPauseButton in PrisonersDilemma. Instead, PrisonersDilemma should update the goPauseButton and call an appropriate method of PlayingField

For example...

goPauseButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        goPauseButton.setText("Pause");
        field.start();
    }
});

And...

public class PlayingField extends JPanel {

    public void start() {
        System.out.println("GO!");
        started = true;
    }

    public void pause() {
        started = false;
        System.out.println("Pause");
    }

    public void reset() {
        System.out.println("Reset");
    }
}
  • Related