Results 1 to 3 of 3
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Help with buttonClick please..

    Java Code:
    public class TestClass {
    	
    	public static void main(String[] args)
    	{
    		JFrame aFrame = new JFrame();
    		aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		aFrame.setSize(500,500);
    		aFrame.setLocationRelativeTo(null);
    		aFrame.setResizable(false);
    	
    		JButton aButton = new JButton("click me");
    		aButton.addActionListener(new myListener());
    		aFrame.getContentPane().setLayout(new FlowLayout());
    		aFrame.add(aButton);
    		
    		aFrame.setVisible(true);
    
    	}
    
    }
    class myListener implements ActionListener
    {
    	@Override
    	public void actionPerformed(ActionEvent e) 
    	{
    		
    		JButton tempButton = (JButton) e.getSource();
    
    		if(tempButton.isSelected())
    		{
    			System.out.println("Button is selected!");
    			tempButton.setSelected(false);
    			tempButton.setText("text text");
    			tempButton.getParent().repaint();
    			tempButton.getParent().validate();
    		}
    		else
    		{
    			tempButton.setSelected(true);
    			System.out.println("Button is selected!");
    		}
    	}
    }
    When I click the button the first time I see the text:
    Button is selected!

    When I click the button the second time I see the text:
    Button is selected! AND the button text changes to text text.

    Why doesn't it change with the first click ?

    Thanks.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Help with buttonClick please..

    You have the same message being printed in two places. How do you know which print statement executed?
    Make the text of the messages different and see what happens.

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Help with buttonClick please..

    Oh, thanks.. It seems I forgot to change the text to Button is not selected..

Posting Permissions

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