Hello,
I am writing a simple Java program that has 2 buttons and 1 text field. How
I would like for it to work is that when I click on Button 1, the actionPeformed
method would get the text from the text field and then change the text of
Button 2 to this text (in the text field). And when I click on Button 2, the
actionPeformed method would get the text from the text field and change
the text of Button 1 to this text.
When I run my program the text of the button does not change as intented
and I got error from the screen :
Exception in thread "AWT-EventQueue-0" java.lang.NullP ointerException
at ButtonPanel.act ionPerformed(Bu ttonTest.java:3 3)
at javax.swing.Abs tractButton.fir eActionPerforme d(AbstractButto n.java:1995)
My code is as follows :import java.awt.*;
import java.awt.event. *;
import javax.swing.*;
class ButtonPanel extends JPanel implements ActionListener
{
public ButtonPanel()
{
button1 = new JButton("Button 1");
button2 = new JButton("Button 2");
JTextField chrisField = new JTextField("Hel lo", 20);
setLayout (new FlowLayout(Flow Layout.LEFT));
add(button1);
add(button2);
add(chrisField) ;
button1.addActi onListener(this );
button2.addActi onListener(this );
chrisField.addA ctionListener(t his);
}public void actionPerformed (ActionEvent evt)
{
Object source = evt.getSource() ;
if (source == button1)
{
/* button2.setText ("Hello There"); */
String text = chrisField.getT ext().trim();
button2.setText (text);
}
else if (source == button2)
{
/* button1.setText ("Hello There"); */
String text = chrisField.getT ext().trim();
button1.setText (text);
}
}
private JButton button1;
private JButton button2;
private JTextField chrisField;
}class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("Butto nTest");
setSize(300, 200);
addWindowListen er(new WindowAdapter()
{ public void windowClosing(W indowEvent e)
{ System.exit(0);
}
} );
Container contentPane = getContentPane( );
contentPane.add (new ButtonPanel());
}
}
public class ButtonTest
{
public static void main(String[] args)
{ JFrame frame = new ButtonFrame();f rame.show();
}
}
Your help is much appreciated.
Akino.
I am writing a simple Java program that has 2 buttons and 1 text field. How
I would like for it to work is that when I click on Button 1, the actionPeformed
method would get the text from the text field and then change the text of
Button 2 to this text (in the text field). And when I click on Button 2, the
actionPeformed method would get the text from the text field and change
the text of Button 1 to this text.
When I run my program the text of the button does not change as intented
and I got error from the screen :
Exception in thread "AWT-EventQueue-0" java.lang.NullP ointerException
at ButtonPanel.act ionPerformed(Bu ttonTest.java:3 3)
at javax.swing.Abs tractButton.fir eActionPerforme d(AbstractButto n.java:1995)
My code is as follows :import java.awt.*;
import java.awt.event. *;
import javax.swing.*;
class ButtonPanel extends JPanel implements ActionListener
{
public ButtonPanel()
{
button1 = new JButton("Button 1");
button2 = new JButton("Button 2");
JTextField chrisField = new JTextField("Hel lo", 20);
setLayout (new FlowLayout(Flow Layout.LEFT));
add(button1);
add(button2);
add(chrisField) ;
button1.addActi onListener(this );
button2.addActi onListener(this );
chrisField.addA ctionListener(t his);
}public void actionPerformed (ActionEvent evt)
{
Object source = evt.getSource() ;
if (source == button1)
{
/* button2.setText ("Hello There"); */
String text = chrisField.getT ext().trim();
button2.setText (text);
}
else if (source == button2)
{
/* button1.setText ("Hello There"); */
String text = chrisField.getT ext().trim();
button1.setText (text);
}
}
private JButton button1;
private JButton button2;
private JTextField chrisField;
}class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("Butto nTest");
setSize(300, 200);
addWindowListen er(new WindowAdapter()
{ public void windowClosing(W indowEvent e)
{ System.exit(0);
}
} );
Container contentPane = getContentPane( );
contentPane.add (new ButtonPanel());
}
}
public class ButtonTest
{
public static void main(String[] args)
{ JFrame frame = new ButtonFrame();f rame.show();
}
}
Your help is much appreciated.
Akino.
Comment