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
}
}
}
}