This is my overloaded Jpanel i want to pass the event of the buttons, so that i can change the text in the jLabel in my main class listed below.
import java.util.Scanner;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Keypad extends JPanel{
private JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12;
public Keypad()
{
setLayout(new GridLayout(4,3));
setBackground(Color.black);
setBorder(BorderFactory.createLineBorder(Color.red,1));
KeypadListener listener = new KeypadListener();
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");
b10 = new JButton("*");
b11 = new JButton("0");
b12 = new JButton("#");
b1.addActionListener(listener);
b2.addActionListener(listener);
b3.addActionListener(listener);
b4.addActionListener(listener);
b5.addActionListener(listener);
b6.addActionListener(listener);
b7.addActionListener(listener);
b8.addActionListener(listener);
b9.addActionListener(listener);
b10.addActionListener(listener);
b11.addActionListener(listener);
b12.addActionListener(listener);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(b10);
add(b11);
add(b12);
}
private class KeypadListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == b1)
label.setText("1");
}
}
}
import java.util.Scanner;
import java.awt.*;
import javax.swing.*;
public class Telephone {
public static void main (String args[]) {
JFrame frame = new JFrame("Telephone");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
Keypad key1 = new Keypad();
JButton clear = new JButton("Clear");
JLabel label = new JLabel("THIS IS THE LABEL");
frame.getContentPane().add(key1,BorderLayout.CENTER);
frame.getContentPane().add(clear,BorderLayout.EAST);
frame.getContentPane().add(label,BorderLayout.NORTH);
frame.pack();
frame.setVisible(true);
}
}
The label is what i need to change, but clearly the event in my Jpanel has no clue what jlabel that i'm referencing because there in two separate classes. Any ideas i'm fairly new to events and listeners, i understand how they work to an extent but i am stumped. Thanks for the help in advance.