Results 1 to 5 of 5
  1. #1
    Metuschelach is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default 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 );

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    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

  3. #3
    Metuschelach is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    Hi Darryl.Burke,
    thanks for your answer. I'm using an ArrayList of JToggleButtons now, as found here: ActionListener zusammenfassen? - java-forum.org:
    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()));
                  }
            });
              
            }
    That works very well.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    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:
    Java Code:
    // private ArrayList<JToggleButton> list1 = new ArrayList<JToggleButton>();
    private List<JToggleButton> list1 = new ArrayList<JToggleButton>();
    -- 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:
    // for (int i=0; i<4; i++){
    for (int i = 0; i < list1.size(); i++) {
    -- Adequate whitespace and consistent indentation improve code readability. Recommended reading:
    Code Conventions for the Java(TM) Programming Language: Contents

    db

  5. #5
    Metuschelach is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default Late Thanks to Darryl.Burke

    Hi Darryl.Burke,

    thank you for your helpful answer. Sorry for responding late.

    Metuschelach

Similar Threads

  1. Replies: 2
    Last Post: 02-14-2011, 02:27 PM
  2. how to implement simply a progress bar
    By Allgorythm in forum New To Java
    Replies: 1
    Last Post: 02-15-2010, 03:28 PM
  3. How to simply get if a key is pushed?
    By ChazZeromus in forum New To Java
    Replies: 6
    Last Post: 07-28-2009, 01:32 AM
  4. It simply won't sort
    By xf021209 in forum New To Java
    Replies: 7
    Last Post: 04-25-2009, 03:53 PM
  5. input to an integer (simply)
    By chitwood in forum Advanced Java
    Replies: 3
    Last Post: 03-18-2009, 06:34 AM

Posting Permissions

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