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.

Recommended Answers

All 2 Replies

Not sure how you'd do it using that code, but somehow you need to access the parent class. I just tried, and "parent" doesn't seem to work the same way that "this" works, so there goes what I was going to suggest ;-)

The way I would do this though if I started from scratch would be to have a Phone class containing your label and keypad all together. That would mean that you have the label in the same class as the buttons, and therefore don't have an issue with trying to access the label using your KeypadListener.

So the new files if you wanted to do that, would be:

  • Phone.java (combination of your Telephone + Keypad)
  • RunPhone.java (with main method, and creates an instance of Phone. That's all this class does...)

You can pass to the KeyPad constructor the element you want to change.

JLabel label = new JLabel("THIS IS THE LABEL");
Keypad key1 = new Keypad(label);
public class Keypad extends JPanel{
 
private JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12;

[B]private JLabel label;
public Keypad(JLabel lbl) {
   this.label = lbl;
}[/B]

{
	public void actionPerformed(ActionEvent event)
	{
		if(event.getSource() == b1)
			label.setText("1");
		}
}
}

Although If I were to do this myself, I would do exactly what leiger has suggested

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.