2

The situation: The states of a set of components are saved to an XML file. Everything saves fine. I am trying to load the same data to the same components (although the rest of the application may be in a different state, e.g. new instance of the application).

The problem: One of the components is a JRadioButton, and I would like to enable this button without causing its actionlistener to fire (the listener invokes objects that may not be created at this point in time).

The question: Is it possible to enable a JRadioButton without firing its listener?

Kirsty Williams
  • 340
  • 1
  • 2
  • 10
  • The only reason I can think of for an actionPerformed event to be fired on a radio button is if its selected state has changed (NBC not at ZOC to test thus though) – MadProgrammer Mar 16 '13 at 23:55
  • I'm sorry, I'm not sure I understand your comment. What do you mean by NBC and ZOC? – Kirsty Williams Mar 17 '13 at 00:00
  • Haaa, 1 fingered, auto correcting iPhone :P should be "not at pc" – MadProgrammer Mar 17 '13 at 00:02
  • Oh, haha! What I am trying to do will be changing the buttons selected state. I am trying to select one of three buttons based on an index stored in an xml file. This is working fine, but because the method called by the actionlistener is using objects not yet initialised I'm getting an error. I'm sure I'll come up with something using a boolean value somewhere :) – Kirsty Williams Mar 17 '13 at 00:07
  • 1
    Solution: don't give the JRadioButtons ActionListeners. – Hovercraft Full Of Eels Mar 17 '13 at 00:09
  • 1
    Remove the ActionListener, change the state, add the ActionListener back to the button. – camickr Mar 17 '13 at 00:15

1 Answers1

2

I solved this problem. Thank you for your comments.

What I did: I created a boolean value 'loading'. Set this to true when I'm loading my XML data, do what I want to do and set this value back to false. The actionPerformed on the radiobutton only runs if loading is false.

Kirsty Williams
  • 340
  • 1
  • 2
  • 10
  • 4
    I made my suggestion because the code is all isolated in one part of the program. Any time you implement your solution you have the code in three places. 1) you need to define a class variable. 2) When you load the XML file you need to access the variable. 3) When the ActionListener is invoked you need to access the variable. – camickr Mar 17 '13 at 01:04