Results 1 to 3 of 3
Thread: Help with buttonClick please..
- 03-01-2012, 09:53 PM #1
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Help with buttonClick please..
When I click the button the first time I see the text: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!"); } } }
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.
- 03-02-2012, 01:38 PM #2
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.
- 03-02-2012, 01:39 PM #3
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks