0

I am new to Java and I had a question.

I have a class that extends a JPanel. It Contains a JButton. If I press this button, I want another class to notice this. By doing so, I had read that I must use Observer/Observable.

Only problem is, I can extend it only once.. Any suggestions?

the two classes:

public class ControlsPanel extends JPanel{

    private JButton start;
    private JButton stop;
    private int animationSpeed = 5;
    private boolean startIsPressed;
    private CashRegistersPanel cr_panel;

    public ControlsPanel(final ParametersPanel panel) {

        start       = new JButton("Start");
        stop        = new JButton("Stop");

        start.setFont(new Font("Arial", Font.BOLD, 14));
        stop.setFont(new Font("Arial", Font.BOLD, 14));

        this.setLayout(null);
        this.setBackground(new Color(199,202,255));

        this.add(start);
        this.add(stop);

        start.setBounds(10, 10, 280, 30);
        stop.setBounds(10, 50, 280, 30);

        stop.setEnabled(false);

        start.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {

            if (start.getText().equals("Start")) {
                start.setText("Pause");
                stop.setEnabled(true);
                startIsPressed = true;
            }

            }
        });

        //For the STOP BUTTON
        stop.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (stop.isEnabled()) {
                    stop.setEnabled(false);
                    start.setText("Start");
                    startIsPressed = false;
                }
            }
        });
    }

    public boolean StartisPressed() {
        return startIsPressed;
    }
}

and

public class CashRegistersPanel extends JPanel{

    private Image img;
    private int amount;
    private ParametersPanel parametersPanel;
    private ControlsPanel controlsPanel;
    private boolean startIsPressed;
    private CashRegister register;


    private ArrayList<CashRegister> myRegister = new ArrayList<CashRegister>();

    public CashRegistersPanel(ParametersPanel parametersPanel, ControlsPanel controlPanel) {

        startIsPressed = controlsPanel.StartisPressed();
    }


    public void paintComponent(Graphics g) {
        if (startIsPressed) {
        Need to do the painting here
        }
        }
    }
}
Frakcool
  • 10,915
  • 9
  • 50
  • 89
Mirwais
  • 1
  • 1
  • 6
  • Why are you extending `JPanel`? Subclassing Swing classes should _only_ be done to add behavior, _not_ to control layout. –  Aug 21 '14 at 16:31
  • I have a JFrame class and two JPanel classes. In the first JPanel class I have a button. If this button is pressed, I want the second JPanel to draw a circle. – Mirwais Aug 21 '14 at 16:37
  • Use a `PropertyChangeEvent`, as shown [here](http://stackoverflow.com/a/11832979/230513). – trashgod Aug 21 '14 at 18:53

3 Answers3

0

You can implement ActionListener in your class suppose MyPanel which is extending JPanel. Then you can pass the object of MyPanel in the ActionListener of the button.

class MyPanel extends JPanel implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
       // Your Drawing Code
    }
    //your other jpanel code
} 

Create object of your customized JPanel

 MyPanel myPanel1;

Now you can pass the myPanel1 object as the ActionListener of the button.

button.addActionListener(myPanel1);
mirmdasif
  • 6,014
  • 2
  • 22
  • 28
  • Thank you for the reply. I have an ActionListener and if I press the button, a boolean isPressed becomes true. I want my second class to notice this change and draw a circle with graphics. – Mirwais Aug 21 '14 at 16:45
  • See my updated answer. Tell me if you have any issue. – mirmdasif Aug 21 '14 at 17:35
  • It doesn't seem to work for me.. Hold on, let my paste a bit of my code and class. I am really stuck here and hope you can help me – Mirwais Aug 21 '14 at 20:10
0

You should add an ActionListener to the Jbutton you want to monitor.

ortis
  • 2,203
  • 2
  • 15
  • 18
  • This won't work because yes I can add an ActionListener to the JButton (and I have), but the JPanel in the second class can't see if the button is pressed or not. – Mirwais Aug 21 '14 at 17:06
  • You can call a method on whatever class you want to notify that the Jbutton have been pressed. – ortis Aug 21 '14 at 18:30
0

Don't extend JPanel, there is not a single reason you should. Now, if you want the other class to notice you should use a controller as a sort version of the MVC pattern.

Stelium
  • 1,207
  • 1
  • 12
  • 23