Results 1 to 2 of 2
  1. #1
    jean28 is offline Member
    Join Date
    Aug 2012
    Rep Power

    Default Help with JButton please.

    First of all, I am extremely new to JButtons. I have watched a tutorial online and saw the basics of how it works, but the example I have only displays a message in the screen and I want to use the JButton for a different function than displaying text.

    I want to put an if statement INSIDE each of the 2 JButtons that pop on the screen. Short example (not actually in java, but what I am trying to do):

    if (button 1 is pressed)
    perform certain actions

    else if (button 2 is pressed)
    dont perform actions and go back to how it was before pressed

    This is the simple JButton program I have made. Any help or any link to a tutorial would be HUGELY appreciated. Thank you very much.

    Java Code:
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JOptionPane;
    public class Gui extends JFrame 
    	private JButton reg;
    	private JButton custom;
    	//private JButton custom;
    	public Gui ()
    		super("The title");
    		setLayout(new FlowLayout());
    		reg = new JButton("Check");
    		custom = new JButton("Pass");
    		HandlerClass handler = new HandlerClass();
    	private class HandlerClass implements ActionListener 
    		public void actionPerformed(ActionEvent event) 
    			JOptionPane.showMessageDialog(null, String.format("%s", event.getActionCommand()));
    Java Code:
    import javax.swing.JFrame;
    public class ButtonTester
    	public static void main(String[] args)
    		Gui go = new Gui();

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Rep Power

    Default Re: Help with JButton please.

    You can do it two ways, either make a new listener for every button, or attach the same listener to all the buttons and check which button was pressed inside the actionPerformed. Take a look at the ActionEvent methods, I'm pretty sure the one you are using "getActionCommand()" gives you the name of the button that was clicked.

Similar Threads

  1. JButton
    By Proshitness in forum AWT / Swing
    Replies: 11
    Last Post: 12-07-2011, 04:25 PM
  2. JButton help
    By kyle_maddisson in forum New To Java
    Replies: 12
    Last Post: 11-14-2011, 08:39 AM
  3. ActionListener for JButton after changing Button to JButton
    By ravi.joshi53 in forum Java Applets
    Replies: 2
    Last Post: 10-07-2011, 07:35 AM
  4. JButton help
    By ThrashingBoy in forum New To Java
    Replies: 2
    Last Post: 01-29-2011, 12:39 PM
  5. Help with JButton
    By geoffreybarwise in forum New To Java
    Replies: 4
    Last Post: 05-21-2008, 10:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts