• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Event handling

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope this isn't to tall an order to ask of someone, but if anyone would be kind enough to look at the code below.....
I am trying to basically go from one page to the next by clicking on a button, the fist page comes up OK, but the button doesn't seem to be working, I can't think why, if anyone has got the time to look at it and come up with a solution I would be much obliged..............Frustrating business this programming game!!!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Quiz extends JFrame implements ItemListener,ActionListener
{

JRadioButton radio1, radio2, radio3, radio4;
ButtonGroup group;
Button b;
JTextField text;
JLabel jl;

public void showGui()
{
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout(0,1));
b.addActionListener(this);
jl = new JLabel("Who played the guitar solo on Peg?");
contentPane.add(jl);
group = new ButtonGroup();
radio1 = new JRadioButton("Dennis Dias.");
radio2 = new JRadioButton("Walter Becker.");
radio3 = new JRadioButton("Steve Gadd.");
radio4 = new JRadioButton("Jeff Skunk Baxter");
group.add(radio1);
group.add(radio2);
group.add(radio3);
group.add(radio4);
radio1.addItemListener(this);
radio2.addItemListener(this);
radio3.addItemListener(this);
radio4.addItemListener(this);
contentPane.add(radio1);
contentPane.add(radio2);
contentPane.add(radio3);
contentPane.add(radio4);
text = new JTextField(15);
contentPane.add(text);

b = new Button("Question Two.");

contentPane.add(b);

setSize(300,300);
setVisible(true);

}
public static void main(String args[])
{
Quiz f = new Quiz();
f.showGui();
f.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
}

public void actionPerformed(ActionEvent e) {
if(e.getSource() == b);
Quiz f = new Quiz();
f.ShowGui2();
}

public void itemStateChanged(ItemEvent e)
{
if (e.getItemSelectable() == radio1) {
text.setText("Dennis Dias.");
} else if (e.getItemSelectable() == radio2) {
text.setText("Walter Becker.");
} else if (e.getItemSelectable() == radio3) {
text.setText("Steve Gadd.");
} else if (e.getItemSelectable() == radio4) {
text.setText("Jeff Skunk Baxter.");
}
}

public class Quiz2 extends JFrame implements ItemListener//,ActionListener
{



public void showGui2()
{
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout(0,1));
//b.addActionListener(this);
jl = new JLabel("Who was the song Chain lightning written about?");
contentPane.add(jl);
group = new ButtonGroup();
radio1 = new JRadioButton("The Christians.");
radio2 = new JRadioButton("The Klu Klux Klan.");
radio3 = new JRadioButton("The Nazi Party.");
radio4 = new JRadioButton("The Mormons.");
group.add(radio1);
group.add(radio2);
group.add(radio3);
group.add(radio4);
radio1.addItemListener(this);
radio2.addItemListener(this);
radio3.addItemListener(this);
radio4.addItemListener(this);
contentPane.add(radio1);
contentPane.add(radio2);
contentPane.add(radio3);
contentPane.add(radio4);
text = new JTextField(15);
contentPane.add(text);

b = new Button("Question Two.");

contentPane.add(b);

setSize(300,300);
setVisible(true);

}

//public void actionPerformed(ActionEvent e) {
//if(e.getSource() == b);
//Quiz f = new Quiz();
//f.ShowGui2();
//}

public void itemStateChanged(ItemEvent e)
{
if (e.getItemSelectable() == radio1) {
text.setText("The Christians.");
} else if (e.getItemSelectable() == radio2) {
text.setText("The Klu Klux Klan.");
} else if (e.getItemSelectable() == radio3) {
text.setText("The Nazi Party.");
} else if (e.getItemSelectable() == radio4) {
text.setText("The Mormons.");
}
}
}

}
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If all you are doing is trying to change what is displayed on the frame, you are doing it in the wrong way. You should rather create a bunch of panels that contain the screen layouts that you want and use the CardLayout on your frame to change what is displayed.
What you are actually doing currently, is creating a brand new frame when the user clicks the button, instead of just changing what is displayed on the current frame.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What Brian says is correct, but there is an even more fundamental problem: you've implemented ActionListener, but you haven't registered the listener with the button:
b.addActionListener(this);
as a result your buttons do nothing.
 
Tell me how it all turns out. Here is a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic