Results 1 to 5 of 5
- 03-01-2011, 05:01 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
96 JToggleButtons - how to simply read the state of all?
Hello,
after hours and hours of trying and searchung the web, I'm here now.
I have a window with 96 JToggleButtons. What is the smartest and easiest way to read the state of them all? I want save the states in an array.
I first tried to do it with an ItemListener and was able to get the state, but then I didn't know which JToggleButton it was.
Java Code:ItemListener buttonListener = new ItemListener() { @Override public void itemStateChanged( ItemEvent e ) { System.out.print( ((JToggleButton ) e.getItem()).getText() ); System.out.println( e.getStateChange() == ItemEvent.SELECTED ? " selected" : " unselected" ); } }; toggleButton1 .addItemListener( buttonListener ); toggleButton2.addItemListener( buttonListener );
- 03-01-2011, 07:17 AM #2
I would use ActionListener in preference to ItemListener, and take one of two approaches:
Implement an ActionListener with a "private final int index" field and add a separate listener to each button
OR
Extend JToggleButton to add a "private final int index" field with a public accessor and detect the index in a common listener.
Depending on how the state array is used, I might prefer a BitSet instead of a boolean array.
db
- 03-03-2011, 01:22 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
Hi Darryl.Burke,
thanks for your answer. I'm using an ArrayList of JToggleButtons now, as found here: ActionListener zusammenfassen? - java-forum.org:
That works very well.Java Code:private ArrayList<JToggleButton> list1 = new ArrayList<JToggleButton>(); list1.add(0, panelProfile.getLoad1()); list1.add(1, panelProfile.getLoad2()); list1.add(2, panelProfile.getLoad3()); list1.add(3, panelProfile.getLoad4()); for (int i=0; i<4; i++){ list1.get(i).addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){ System.out.println(list1.indexOf(e.getSource())); } }); }
- 03-03-2011, 05:31 AM #4
Glad to know you found a workable solution. A couple of notes:
-- Instead of a whole series of methods getLoad1, getLoad2 etc. it may be cleaner to have a single getLoad(int index) method
-- Coding to the interface is preferred for the flexibility of being able to make just a single change if you later decide to use a different implementation. Thus:-- In the example code you posted, the upper limit of the for loop is hardcoded to 4, which you would require to change manually if the number of buttons changed. A better approach is to set the limit according to the size() of the List. Thus:Java Code:// private ArrayList<JToggleButton> list1 = new ArrayList<JToggleButton>(); private List<JToggleButton> list1 = new ArrayList<JToggleButton>();
-- Adequate whitespace and consistent indentation improve code readability. Recommended reading:Java Code:// for (int i=0; i<4; i++){ for (int i = 0; i < list1.size(); i++) {
Code Conventions for the Java(TM) Programming Language: Contents
db
- 03-15-2011, 10:54 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
How to release multiple threads from waiting state to runnable state
By Dayanand in forum New To JavaReplies: 2Last Post: 02-14-2011, 02:27 PM -
how to implement simply a progress bar
By Allgorythm in forum New To JavaReplies: 1Last Post: 02-15-2010, 03:28 PM -
How to simply get if a key is pushed?
By ChazZeromus in forum New To JavaReplies: 6Last Post: 07-28-2009, 01:32 AM -
It simply won't sort
By xf021209 in forum New To JavaReplies: 7Last Post: 04-25-2009, 03:53 PM -
input to an integer (simply)
By chitwood in forum Advanced JavaReplies: 3Last Post: 03-18-2009, 06:34 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks