0

I'm creating a ToolBar with one JButton and some JCheckBoxs to hide or show columns from a JTable.

The JButton main purpose is to once clicked reset all the other JCheckBox in the ToolBar to either all checked or all unchecked. Actually I pretend to once reset is clicked to check all of them.

I can create them and place them, no problems here.

My problem is how to make the JButton once clicked to reset all the JCheckBox in the ToolBar.

Here is my code, where I create them and add the Action.

        final JToolBar toolBarTop = new JToolBar();

        // The Reset Button
        toolBarTop.add(new JButton(new AbstractAction("Reset") {
            @Override
            public void actionPerformed(ActionEvent e) {
            columnModel.setAllColumnsVisible();
            }   
        }));

        // Create a JCheckBox for each column
        for(int i = 0; i < labelsCheckBox.size(); i++)
        {
            final int index = i;
            toolBarTop.add(new JCheckBox(new AbstractAction(labelsCheckBox.get(i)) {
            @Override
                public void actionPerformed(ActionEvent e) {
                TableColumn column = columnModel.getColumnByModelIndex(index);
                boolean visible = columnModel.isColumnVisible(column);
                columnModel.setColumnVisible(column, !visible);
                }
            }));
        }
dazito
  • 7,740
  • 15
  • 75
  • 117
  • 1
    I'm still confused -- where are you stuck? Where is your code where you try to iterate through your collection of JCheckBoxes (for surely you have them in a collection of some sort such as an ArrayList)? – Hovercraft Full Of Eels Oct 10 '13 at 21:45
  • For your first question: I'm stuck on where and how to add the (some kind of) action to the JButton to reset the JCheckBoxes. Your second question, I'm not sure if I understood it correctly but that for loop is intended to create an X amount of JCheckBoxes, this X amount comes in as a parameter for this method, createTable(Class c, List data, boolean toolBarUp, boolean toolBarBottom, ArrayList labelsCheckBox) – dazito Oct 10 '13 at 21:50
  • I could easily do it if the JCheckBoxes had variables associated with just using checkBox.setSelected(true) on the JButton AbstractAction but this way the the JCheckBoxes are created 'on the fly' as anonymous and I don't know how to use the setSelected(true) or an equivalent method. – dazito Oct 10 '13 at 21:58
  • @dwnz: that's the problem. Don't create them like this. Store them in a collection that the JButton listener can iterate on. – JB Nizet Oct 10 '13 at 22:00

2 Answers2

5
  • Create an ArrayList<JCheckBox> class field.
  • Fill it in the for(int i = 0; i < labelsCheckBox.size(); i++) for loop with the JCheckBoxes that you are creating.
  • Iterate through this collection setting the state of the JCheckBoxes in your Button's Action.
  • I wonder if you should be using a JToggleButton for your button.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
2

My problem is how to make the JButton once clicked to reset all the JCheckBox in the ToolBar.[...] Actually I pretend to once reset is clicked to check all of them.

Try iterating over toolBarTop components asking each one if it's an instance of JCheckBox. If so, set selected to true:

JButton reset = new JButton("Reset");
    reset.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            for(Component c : toolBarTop.getComponents()){
                if(c instanceof JCheckBox){
                    JCheckBox checkBox = (JCheckBox) c;
                    checkBox.setSelected(true);
                }
            }
        }
    });

Here's a complete SSCCE to test it:

import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;

public class Demo {

    private void initGUI(){

        final JToolBar toolBarTop = new JToolBar();
        toolBarTop.add(new JCheckBox("Check 1"));
        toolBarTop.add(new JCheckBox("Check 2"));
        toolBarTop.add(new JCheckBox("Check 3"));

        JButton reset = new JButton("Reset");
        reset.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for(Component c : toolBarTop.getComponents()){
                    if(c instanceof JCheckBox){
                        JCheckBox checkBox = (JCheckBox) c;
                        checkBox.setSelected(true);
                    }
                }
            }
        });

        toolBarTop.add(reset);

        JPanel content = new JPanel(new FlowLayout());
        content.setPreferredSize(new Dimension(300, 200));
        content.add(toolBarTop);

        JFrame frame = new JFrame("Demo");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setContentPane(content);       
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }


    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Demo().initGUI();
            }
        });
    }    

}
dic19
  • 17,821
  • 6
  • 40
  • 69