Join 149,505 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,333 people online right now. Registration is fast and FREE... Join Now!
Let me first state that this code works. I now need to implement an event listening interface that will tell me which button was selected. I have tried some IF statements with the getsource() but I was not even close. I have already registered a listener with the button. I am simply trying to ascertain what needs to be interogatted to check which button was pressed.
I tried adding this code to test with but I was waaaaaay off.
CODE
if (e.getSource() == radio1) system.out.println("You selectged the 5.35%"); else system.out.println("You did not select the first button!");
//Create and set up the frame JFrame frame = new JFrame("Mortgage Rates"); //Close application when close button clicked frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// A JPanel will hold the contents JPanel pane = new JPanel(new BorderLayout());
// Add a ButtonGroup to hold three radio buttons rates = new ButtonGroup();
radio1 = new JRadioButton("5.35%"); radio1.setActionCommand("5.35%"); myRadioListener listenIn = new myRadioListener(); radio1.addActionListener(listenIn);
radio1.setSelected(true); radio2 = new JRadioButton("5.50%"); radio2.setActionCommand("5.50%"); radio2.addActionListener(listenIn); radio3 = new JRadioButton("5.75%"); radio3.setActionCommand("5.75%"); radio3.addActionListener(listenIn);
// A JPanel will hold the contents JPanel radiopane = new JPanel(new GridLayout(3,1)); radiopane.add(radio1); radiopane.add(radio2); radiopane.add(radio3);
pane.add(radiopane, BorderLayout.WEST);
// Create and add a label label = new JLabel(" "); pane.add(label, BorderLayout.CENTER);
// Add the JPanel to the frame frame.getContentPane().add(pane, BorderLayout.CENTER);
//Display the frame frame.setSize(200, 200); frame.setVisible(true); }
class myRadioListener implements ActionListener { public void actionPerformed(ActionEvent e) { label.setText(rates.getSelection().getActionCommand());
} }
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater( new Runnable() { public void run() { UsingButtons2 app = new UsingButtons2 (); app.buildapp(); } } ); } }
This post has been edited by William_Wilson: 13 Jul, 2007 - 08:00 AM
please do not forget to use [code] tags around all code. I believe you were aiming for integrated, and not a word which more closely resembles interrogated... though i'm sure there are a few members which would gladly do either, lol
to be honest it is better to implement separate classes for each button, this may seem like a waste, but it is more efficient in larger programs as the complicated if statement to handle large sets of buttons, is very inefficient.
//Create and set up the frame JFrame frame = new JFrame("Mortgage Rates"); //Close application when close button clicked frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// A JPanel will hold the contents JPanel pane = new JPanel(new BorderLayout());
// Add a ButtonGroup to hold three radio buttons rates = new ButtonGroup();
radio1 = new JRadioButton("5.35%"); radio1.setActionCommand("5.35%"); myRadioListener listenIn = new myRadioListener(); radio1.addActionListener(listenIn); radio1.setSelected(true);
radio2 = new JRadioButton("5.50%"); radio2.setActionCommand("5.50%"); radio2.addActionListener(listenIn);
radio3 = new JRadioButton("5.75%"); radio3.setActionCommand("5.75%"); radio3.addActionListener(listenIn);
// A JPanel will hold the contents JPanel radiopane = new JPanel(new GridLayout(3,1)); radiopane.add(radio1); radiopane.add(radio2); radiopane.add(radio3);
pane.add(radiopane, BorderLayout.WEST);
// Create and add a label label = new JLabel(" "); pane.add(label, BorderLayout.CENTER);
// Add the JPanel to the frame frame.getContentPane().add(pane, BorderLayout.CENTER);
//Display the frame frame.setSize(200, 200); frame.setVisible(true); }
{ if (radio1.isSelected()) {JOptionPane.showMessageDialog(null,"You selected %5.35", "Message Dialog",JOptionPane.PLAIN_MESSAGE);} if (radio2.isSelected()) {JOptionPane.showMessageDialog(null,"Your selecgted %5.50", "Message Dialog",JOptionPane.PLAIN_MESSAGE);} } }[/color]
class myRadioListener implements ActionListener { public void actionPerformed(ActionEvent e) { label.setText(rates.getSelection().getActionCommand());
} }
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater( new Runnable() { public void run() { UsingButtons2 app = new UsingButtons2 (); app.buildapp(); } } ); } }
//Create and set up the frame JFrame frame = new JFrame("Mortgage Rates"); //Close application when close button clicked frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// A JPanel will hold the contents JPanel pane = new JPanel(new BorderLayout());
// Add a ButtonGroup to hold three radio buttons rates = new ButtonGroup();
radio1 = new JRadioButton("5.35%"); radio1.setActionCommand("5.35%"); myRadioListener listenIn = new myRadioListener(); radio1.addActionListener(listenIn); radio1.setSelected(true);
radio2 = new JRadioButton("5.50%"); radio2.setActionCommand("5.50%"); radio2.addActionListener(listenIn);
radio3 = new JRadioButton("5.75%"); radio3.setActionCommand("5.75%"); radio3.addActionListener(listenIn);
// A JPanel will hold the contents JPanel radiopane = new JPanel(new GridLayout(3,1)); radiopane.add(radio1); radiopane.add(radio2); radiopane.add(radio3);
pane.add(radiopane, BorderLayout.WEST);
// Create and add a label label = new JLabel(" "); pane.add(label, BorderLayout.CENTER);
// Add the JPanel to the frame frame.getContentPane().add(pane, BorderLayout.CENTER);
//Display the frame frame.setSize(200, 200); frame.setVisible(true); }
if (source==radio1) {JOptionPane.showMessageDialog(null,"You selected %5.35","Message Dialog",JOptionPane.PLAIN_MESSAGE);} if (source==radio2) {JOptionPane.showMessageDialog(null,"Your selecgted %5.50","Message Dialog",JOptionPane.PLAIN_MESSAGE);} }
class myRadioListener implements ActionListener { public void actionPerformed(ActionEvent e) { label.setText(rates.getSelection().getActionCommand());
} }
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater( new Runnable() { public void run() { UsingButtons2 app = new UsingButtons2 (); app.buildapp(); } } ); } }
Hi here is the code that works, but let me explain a little why it wasn't working. I wasn't working because you assigned to the buttons the object listenIn from your class myRadioListener. So that means that the buttons will work only for the actionPerformed method in the myRadioListener class. They wont listen any other actionPerformed method so the code you wanted them to do should be in the actionPerformed from the myRadioListener class. the other actionPerformed I saw no use of it so I commented out. Now look through this code very carefully and you'll understand how things work. Enjoy
//Create and set up the frame JFrame frame = new JFrame("Mortgage Rates"); //Close application when close button clicked frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// A JPanel will hold the contents JPanel pane = new JPanel(new BorderLayout());
// Add a ButtonGroup to hold three radio buttons rates = new ButtonGroup();
radio1 = new JRadioButton("5.35%"); radio1.setActionCommand("5.35%"); myRadioListener listenIn = new myRadioListener(); radio1.addActionListener(listenIn); radio1.setSelected(true);
radio2 = new JRadioButton("5.50%"); radio2.setActionCommand("5.50%"); radio2.addActionListener(listenIn);
radio3 = new JRadioButton("5.75%"); radio3.setActionCommand("5.75%"); radio3.addActionListener(listenIn);
// A JPanel will hold the contents JPanel radiopane = new JPanel(new GridLayout(3,1)); radiopane.add(radio1); radiopane.add(radio2); radiopane.add(radio3);
pane.add(radiopane, BorderLayout.WEST);
// Create and add a label label = new JLabel(" "); pane.add(label, BorderLayout.CENTER);
// Add the JPanel to the frame frame.getContentPane().add(pane, BorderLayout.CENTER);
//Display the frame frame.setSize(200, 200); frame.setVisible(true); }
{ if (source==radio1) {JOptionPane.showMessageDialog(null,"You selected %5.35");} if (source==radio2) {JOptionPane.showMessageDialog(null,"Your selecgted %5.50", "Message Dialog",JOptionPane.PLAIN_MESSAGE);} } }*/
class myRadioListener implements ActionListener { public void actionPerformed(ActionEvent e) { Object source = e.getSource(); ///label.setText(rates.getSelection().getActionCommand()); if (source==radio2) { JOptionPane.showMessageDialog(null,"Your selecgted %5.50", "Message Dialog",JOptionPane.PLAIN_MESSAGE); } else if (source==radio1) { JOptionPane.showMessageDialog(null,"You selected %5.35", "Message Dialog",JOptionPane.PLAIN_MESSAGE); } else if (source==radio3) { JOptionPane.showMessageDialog(null,"You selected %5.75", "Message Dialog",JOptionPane.PLAIN_MESSAGE); }
}
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater( new Runnable() { public void run() { UsingButtons2 app = new UsingButtons2 (); app.buildapp(); } } ); } }
I think I understand what you are saying. My code was using the actionPerformed method but not within the myRadioListener. I appreciate your assistance and explanation. I feel better knowing I was very close. Thanks again.