Welcome to Dream.In.Code
Become a Java Expert!

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!




Capturing what button was pressed

 
Reply to this topicStart new topic

Capturing what button was pressed

emmitsuks
13 Jul, 2007 - 07:49 AM
Post #1

New D.I.C Head
*

Joined: 12 Jul, 2007
Posts: 6


My Contributions
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!");


Here is my code so far.

// Import swing and awt classes
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class UsingButtons2 {


    ButtonGroup rates;
    JLabel label;
    JRadioButton radio1;
    JRadioButton radio2;
    JRadioButton radio3;

    private void buildapp() {
        // Fancy decorations
        JFrame.setDefaultLookAndFeelDecorated(true);

        //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);

        rates.add(radio1);
        rates.add(radio2);
        rates.add(radio3);

        // 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
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Capturing What Button Was Pressed
13 Jul, 2007 - 08:04 AM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

dragon-slayer
RE: Capturing What Button Was Pressed
13 Jul, 2007 - 09:23 AM
Post #3

D.I.C Head
Group Icon

Joined: 6 May, 2007
Posts: 69


Dream Kudos: 100
My Contributions
I would do it something like this
example:


CODE

   Button press = new Button("press");
    press.addActionListener(ButtonListen);


    ActionListener ButtonListen = new ActionListener(){
    
     public void actionPerformed(ActionEvent evt) {
          
       System.out.println("phool press button is clicked");
        }
    };
    

User is offlineProfile CardPM
+Quote Post

emmitsuks
RE: Capturing What Button Was Pressed
13 Jul, 2007 - 10:11 AM
Post #4

New D.I.C Head
*

Joined: 12 Jul, 2007
Posts: 6


My Contributions
As suggested I have added a new class. It compiles correctly but I don't get any output. Also, did I post correctly?





CODE


// Import swing and awt classes
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class UsingButtons2 {


    ButtonGroup rates;
    JLabel label;
    JRadioButton radio1;
    JRadioButton radio2;
    JRadioButton radio3;

    private void buildapp() {
        // Fancy decorations
        JFrame.setDefaultLookAndFeelDecorated(true);

        //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);


        rates.add(radio1);
        rates.add(radio2);
        rates.add(radio3);

        // 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);
    }


[color=#FF0000]// event listeners
  public void actionPerformed(ActionEvent ae)
  {
    Object source = ae.getSource();

    {
      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();
                }
            }
        );
    }
}


User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Capturing What Button Was Pressed
13 Jul, 2007 - 11:19 AM
Post #5

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hi, this your part of the code:
QUOTE
public void actionPerformed(ActionEvent ae)
{
Object source = ae.getSource();

{
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);}
}
}


You've declared source but you don't use it.

try to change the if conditions into something like this:
CODE
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);}

And yes you posted correctly well almost you cannot use colors inside the code tags.
User is offlineProfile CardPM
+Quote Post

emmitsuks
RE: Capturing What Button Was Pressed
13 Jul, 2007 - 04:54 PM
Post #6

New D.I.C Head
*

Joined: 12 Jul, 2007
Posts: 6


My Contributions
I can get it to compile and display buttons but there is nothing from the show.Message.Dialog. Talk about frustrating..



CODE

// Import swing and awt classes
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class UsingButtons2 {


    ButtonGroup rates;
    JLabel label;
    JRadioButton radio1;
    JRadioButton radio2;
    JRadioButton radio3;

    private void buildapp() {
        // Fancy decorations
        JFrame.setDefaultLookAndFeelDecorated(true);

        //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);


        rates.add(radio1);
        rates.add(radio2);
        rates.add(radio3);

        // 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);
    }


// event listeners
  public void actionPerformed(ActionEvent ae)
  {
    Object source = ae.getSource();


      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();
                }
            }
        );
    }
}


User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Capturing What Button Was Pressed
14 Jul, 2007 - 02:42 AM
Post #7

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
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 smile.gif

CODE
// Import swing and awt classes
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class UsingButtons2 {


    ButtonGroup rates;
    JLabel label;
    JRadioButton radio1;
    JRadioButton radio2;
    JRadioButton radio3;

    private void buildapp() {
        // Fancy decorations
        JFrame.setDefaultLookAndFeelDecorated(true);

        //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);


        rates.add(radio1);
        rates.add(radio2);
        rates.add(radio3);

        // 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);
    }



  /*public void actionPerformed(ActionEvent ae)
  {
    Object source = ae.getSource();

    {
      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();
                }
            }
        );
    }
}

User is offlineProfile CardPM
+Quote Post

emmitsuks
RE: Capturing What Button Was Pressed
14 Jul, 2007 - 06:58 PM
Post #8

New D.I.C Head
*

Joined: 12 Jul, 2007
Posts: 6


My Contributions
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.

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 06:54PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month