Events with multiple JPanels and JButtons

O

Oxnard

I am trying to have 2 JPanels in one JFrame. The first JPanel (Panel1) has
two buttons. The second JPanel has a JTextField. The app works as expected
if I have only one button, but if I have a second button the second button
does not work. I found this example online and would like to expand it to be
able to use more than one button. What do I need to do to get the second
button to do a simple println? I have seen a number of posting but I am just
not quite getting it.

Thanks


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

public class Test extends JFrame{

public Test() {
super("hello");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,400);
Panel1 p1 = new Panel1();
Panel2 p2 = new Panel2();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(p1, BorderLayout.SOUTH);
getContentPane().add(p2, BorderLayout.CENTER);
pack();
setVisible(true);
}

public static void main(String[] args){
setDefaultLookAndFeelDecorated(true);
new Test();
}
}

public class YourHandler implements ActionListener{
private static JButton theButton;
private static JTextField theField;

public YourHandler() {
}

public YourHandler(JButton b){
theButton = b;
}

public YourHandler(JTextField tf){
theField = tf;
}

public void actionPerformed(ActionEvent e) {
if(e.getSource() == theButton){
System.out.println(theField.getText());
}
}
}

public class Panel1 extends JPanel{
private JButton Show;
private JButton Clear;

public Panel1() {
Show = new JButton("Show");
Clear = new JButton("Clear");
Show.addActionListener(new YourHandler(Show));
Clear.addActionListener(new YourHandler(Clear));
this.add(Show);
this.add(Clear);
}
}

public class YourHandler implements ActionListener{
private static JButton theButton;
private static JTextField theField;

public YourHandler(JButton b){
theButton = b;
}

public YourHandler(JTextField tf){
theField = tf;
}

public void actionPerformed(ActionEvent e) {
if(e.getSource() == theButton){
System.out.println(theField.getText());
}
}
}
 
A

Andrew Thompson

I am trying to have 2 JPanels in one JFrame. The first JPanel (Panel1) has
two buttons. The second JPanel has a JTextField. The app works as expected

What is 'expected'?
if I have only one button, but if I have a second button the second button
does not work.

Gets lazy does it? Have you tried flogging it with a whip?

You need to define.
- I expected this output.
- But got this ouput instead.
- The two differ by..
..I found this example online and would like to expand it to be
able to use more than one button. What do I need to do to get the second
button to do a simple println?

Suggestions:

Learn OO concepts and how to construct GUI's.

Start asking questions on a more appropriate group.
<http://www.physci.org/codes/javafaq.jsp#cljh>
...and then when you have the hang of asking well framed
questions, move on to a more specific group.
..I have seen a number of posting but I am just
not quite getting it.

Looking at your code, it seems you have little idea of what you are doing.

Your example does not compile, and you could have made changes to
allow it to be an SSCCE*. I wrestled with it for ten minutes, but
it is not clear what you are trying to achieve, or why you subclassed
the JPanels, so I set it aside.

For tips on preparing examples for others to see, check..
* <http://www.physci.org/codes/sscce.jsp>

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top