Results 1 to 4 of 4
  1. #1
    graympa is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default Multiple JButton/ActionListener problem

    I am unable to implement more than one actionlistener. I have two questions, 1, how should I code this, and 2) is my class / program design off? would like not only how to make it work but any advice on doing it (program/class design) correctly.

    I have one actionlistener and several buttons. I want to use if statements in the action performed method to decide which action to do. When I run the program, it says listener is not iniatialized. I have found examples on the Internet to initialize the listener, but I couldn't figure out how to use that method for several buttons.

    Here's my code:

    Java Code:
    public class FlashCard {
    
        public FlashCard()
        {
            OperationsWindow();
            DisplayQuestionWindow();
        }
    
    
    
        public void OperationsWindow()
        {
            //TODO OperationsWindow covers entire page
            //asks if Question, Add Question, or Exit
            int w;
            int h;
            ActionListener listener;
    
            Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
            JFrame opsF = new JFrame("FlashCard");
            Container content =opsF.getContentPane();
            content.setLayout(new FlowLayout());
    
            //format to add Button
            //declare jbutton, initialize name
            //create a listener for that button
            //add listener to button
            //enable button
            //add button to jframe
    
            JButton jbQuit = new JButton("Quit");
            jbQuit.addActionListener(listener);
            jbQuit.setEnabled(true);
            opsF.add(jbQuit);
    
            JButton jbStart = new JButton("Start");
            jbStart.addActionListener(listener);
            jbStart.setEnabled(true);
            opsF.add(jbStart);
    
            w=(int) dim.getWidth();
            h=(int) dim.getHeight();
            opsF.setSize(w-4, h-400);
            opsF.setDefaultCloseOperation(opsF.EXIT_ON_CLOSE);
            //opsF.pack();
            opsF.setVisible(true);
        }
    
        	public void actionPerformed(ActionEvent a)
            {
                String str = a.getActionCommand();
                System.out.println("str is: ");
    		if(a.getSource() == "Quit")
                    {
    			System.out.println("Start button was pressed...");
    		}
    
    		//if(a.getSource() == stopButton){
    		//	System.out.println("Stop button was pressed...");
    	}
    Last edited by Fubarable; 04-03-2011 at 08:14 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Where's the rest of your class? The imports? The main method?

    I've added code tags to your post above so that your code will retain its formatting and we can better be able to read it. To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]

    Good luck!

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  4. #4
    graympa is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default

    Thanks very much. I was trying to figure out how to only have one action method; the anonymous class works great. Unusual for me, I was overthinking the problem. I have since changed my signature.

    I will format any coding next time.

Similar Threads

  1. Problem with ActionListener?
    By Manish87 in forum AWT / Swing
    Replies: 5
    Last Post: 03-31-2011, 02:54 PM
  2. JButton ActionListener problems
    By Fortu in forum New To Java
    Replies: 6
    Last Post: 02-05-2011, 04:20 PM
  3. JButton actionlistener not working, very strange
    By alacn in forum New To Java
    Replies: 4
    Last Post: 08-05-2010, 02:25 AM
  4. Replies: 6
    Last Post: 02-01-2010, 09:30 AM
  5. Replies: 2
    Last Post: 04-19-2009, 05:39 AM

Tags for this Thread

Posting Permissions

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