• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with ChangeListener of JToggleButton  RSS feed

 
Tapan Maru
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On attampting to implement change listener fot JToggleButton, i get the following output

editCommand.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent ce) {
if (!editCommand.isSelected()) {
System.out.println("Edit Command Selected "+editCommand.isSelected());
}
if (editCommand.isSelected()) {
System.out.println("Edit Command Selected "+editCommand.isSelected());
}

Output
------
Edit Command Selected false
Edit Command Selected false
Edit Command Selected true
Edit Command Selected true

initially my toggle button is not selected, when i click on it it gets selected but i m getting above output. also i want to take input from the user through JOptionPane, but the problem is my OptionPane is showing three times..

Can you please help me if you have come across the above problem and have solution??
 
Lanny Gilbert
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We'd need your JOptionPane code to help with that issue, but on the System.out.println() being done twice, are you sure that you're not doing a repaint after each use of the JToggle? that might cause another call to change event.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why use a changeListener?
actionListener does what you seem to be trying to do

 
Tapan Maru
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the support.

i have changed the code. Initially i was using ChangeListener
but now i have changed to implements ItemListener and use the method itemStateChanged(ItemEvent ie)

int state = ie.getStateChanged

which gives me either ItemEvent.SELECTED or ItemEvent.DESELECTED for my JToggleButton

any ways thanks once more.. and do post your queries.. i will try to answer them
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!