Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Swing GUI: Using buttons from a different classfile in ActionListener

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Swing GUI: Using buttons from a different classfile in ActionListener

    Hello,

    I'm testing out buttons and GUI, and I'm trying to figure out how to have my actionListener take buttons from a different file, if at all possible

    my current code

    package visualDerp;
     
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.*;
     
    public class PressButtonWindow extends JFrame {
     
    	public JButton button1 = new JButton("Button 1");
    	public JButton button2 = new JButton("Button 2");
     
    	public PressButtonWindow(String title){
     
    		setTitle(title);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		LayoutManager layout = new FlowLayout();
    		setLayout(layout);
     
    		//create the button
     
    		//setMnemonic adds an ALT+paramater hotkey to the button
    		button1.setMnemonic('1');
    		button2.setMnemonic('2');
     
     
    		//greys out the button
    		//button2.setEnabled(false);
     
    		//adds the button to the GUI
    		add(button1);
    		add(button2);
    		//creates a listener
    		PressButtonListener buttonlistener = new PressButtonListener();
    		//ties the listener to the button
    		button1.addActionListener(buttonlistener);
    		button2.addActionListener(buttonlistener);
    		//adjusts window size
    		pack();
    	}
     
     
    	/*
    	 * the event handler
    	 * should print different messages for the different buttons
    	 */
    	class PressButtonListener implements ActionListener{
     
    		public void actionPerformed(ActionEvent event){
    			if(event.getSource() == button1){			
    				System.out.println("You pressed button 1. Yay!");
    			}
    			else if(event.getSource() == button2){
    				System.out.println("You pressed button 2. Woo!");
    			}
    		}
     
    	}
    }

    and the main:

    package visualDerp;
     
    public class PressButtonWindowTest {
     
    	public static void main(String[] args) {
    		PressButtonWindow aWindow = new PressButtonWindow("Window with a button.");
    		aWindow.setVisible(true);
    	}
     
    }

    what i WOULD LIKE is a seperate file for the buttonlistener able to use the buttons in the different file, something like this

    package visualDerp;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
     
     
    class PressButtonListener implements ActionListener{
     
    	public void actionPerformed(ActionEvent event){
    		if(event.getSource() == button1){			
    			System.out.println("You pressed button 1. Yay!");
    		}
    		else if(event.getSource() == button2){
    			System.out.println("You pressed button 2. Woo!");
    		}
    	}
    }

    while button1 and button2 are declared in a seperate file

    is this possible?

    rg,
    VikingCoder


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Swing GUI: Using buttons from a different classfile in ActionListener

    One way is to get the actionCommand String from the actionPerformed's ActionEvent parameter, event, by calling event.getActionCommand(), and then checking if the string equalsIgnoreCase("Button 1") or equalsIgnoreCase("Button 2"). Another way is to give each Button its own ActionListener. A third way, is to have the main class give the GUI an instance of a stand-alone Control class, give the JButton's anonymous inner class ActionListeners, and have these ActionListeners call methods from the control.

Similar Threads

  1. How to Add ActionListener to a JButton in Swing?
    By JavaPF in forum Java Swing Tutorials
    Replies: 17
    Last Post: April 24th, 2013, 06:14 PM
  2. Help with actionListener and buttons
    By jm24 in forum AWT / Java Swing
    Replies: 10
    Last Post: February 11th, 2012, 01:50 PM
  3. Java swing buttons are flickering in windows 7 environment
    By javasam in forum AWT / Java Swing
    Replies: 1
    Last Post: February 14th, 2011, 12:13 PM
  4. How to Add ActionListener to a JButton in Swing?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 15
    Last Post: February 10th, 2011, 02:21 AM
  5. [SOLVED] Problem with swing-buttons
    By Minken in forum AWT / Java Swing
    Replies: 8
    Last Post: October 15th, 2010, 10:20 PM

Tags for this Thread