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 3 of 3

Thread: Problem with ActionEvent not doing anything

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Problem with ActionEvent not doing anything

    Hi, I've been learning the basics of Java on and off for a few years, and I am currently attempting to learn how to code GUI stuff. I'm brand new to coding GUIs, only having started using it yesterday. I was trying to use action listeners to respond to button presses. I had some success, although some of my code works and some doesn't. I've commented next to the bit I think is wrong, but I'm unsure what I need to do to fix it. Basically, the nextOne ActionEvent doesn't work. I popped in a System.out.println in there to see if it even 'triggered' at all, but nothing happens.

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JLabel;
    import javax.swing.JButton;
    import javax.swing.BoxLayout;
    import javax.swing.ImageIcon;
    import java.awt.BorderLayout;
    import javax.swing.BorderFactory;
    import javax.swing.border.Border;
    import java.awt.Dimension;
    import javax.swing.JSeparator;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
     
    public class simpleCardGame implements ActionListener{
     
    	JButton higher, lower, nextOne;
    	JPanel mainPane, topPane;
     
    	public JFrame setupGame(){
    		Border empty = BorderFactory.createEmptyBorder(10, 10, 10, 10);
    		JFrame frame = new JFrame("Nic's simple card game");
    		frame.setPreferredSize(new Dimension(600, 350));
     
        		mainPane = new JPanel();
        		mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS));	
    	        topPane = new JPanel();
     
            	JLabel cardPile = new JLabel("", new ImageIcon("cardback.jpg"), JLabel.LEADING);
    	        cardPile.setBorder(empty);
            	cardPile.setVerticalTextPosition(JLabel.TOP);
             	cardPile.setHorizontalTextPosition(JLabel.CENTER);
     
             	JLabel firstCard = new JLabel("",new ImageIcon("card.jpg"), JLabel.LEADING);
                    topPane.add(firstCard);
    	 	topPane.add(cardPile);
     
       		JPanel buttonPanel = new JPanel();
    		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
     
    		higher = new JButton("Higher");
    		higher.setSize(400,800);
    		higher.addActionListener(this);
     
    		lower = new JButton("Lower");
    		lower.setSize(200,800);
    		lower.addActionListener(this);
     
    		buttonPanel.add(higher);
    		buttonPanel.add(lower);   
     
    		mainPane.add(topPane);
    		mainPane.add(buttonPanel);
    		frame.setContentPane(mainPane);
        		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.pack();
     
       		return frame;
    	}
     
    	public void showAnswer(){
    		mainPane.removeAll();
    		topPane = new JPanel();
     
         		JLabel firstCard = new JLabel("",new ImageIcon("card.jpg"), JLabel.LEADING);
        		topPane.add(firstCard);
     
        		JLabel revealCard = new JLabel("",new ImageIcon("card2.jpg"), JLabel.LEADING);
        		topPane.add(revealCard);
        		JPanel answerPanel = new JPanel();
    		answerPanel.setLayout(new BoxLayout(answerPanel, BoxLayout.X_AXIS));
     
    		JLabel answer = new JLabel("Correct!  Or incorrect! Please code this properly!", JLabel.LEADING);
     
    		JButton nextOne = new JButton("Next");
    		nextOne.addActionListener(this);
     
    		answerPanel.add(answer);
    		answerPanel.add(nextOne);
    		mainPane.add(topPane);
    		mainPane.add(answerPanel);
    		mainPane.updateUI();
    	}
     
    	String choice = null;
    	public void actionPerformed(ActionEvent e) {
          		if(e.getSource() == higher) {
              		choice = "higher";
             		showAnswer();
    			System.out.println("higher clicked");
          		}
          		else if(e.getSource() == lower){
              		choice = "lower";
              		showAnswer();
             		System.out.println("lower clicked");
          		}
     
    /** THIS IS PROBABLY WHERE I'VE GONE WRONG */
     
     		else if(e.getSource() == nextOne){
          			System.out.println("Next clicked");
          			mainPane.removeAll();
          			JFrame anotherFrame = setupGame();
          			showGame(anotherFrame);
          		}
      	}
     
    	public void showGame(JFrame frame){
    		frame.setVisible(true); 
    	}
    }

    And, probably irrelevant here, but this is the code I use to actually run this...

    import javax.swing.JFrame;
     
    public class cardTest {
     
    	public static void main(String[] args) {
     
    		simpleCardGame myGame = new simpleCardGame();
    		JFrame thisFrame = myGame.setupGame();
    		myGame.showGame(thisFrame);
     
    	}
    }

    Thanks!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Problem with ActionEvent not doing anything

    The JButton you're adding the ActionListener to is not the same JButton you're checking against in your ActionListener.

    When you do:

    JButton nextOne = new JButton("Next");

    That's "hiding" the nextOne variable that's global, that you're using in your ActionListener.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    thisisnic (April 14th, 2011)

  4. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem with ActionEvent not doing anything

    Ah, awesome, that would have taken me a very long time to figure out myself! Thanks for your help

Similar Threads

  1. How to generate an ActionEvent progrematically
    By nasi in forum Java Theory & Questions
    Replies: 7
    Last Post: May 22nd, 2010, 12:31 PM