Domanda

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);
                }
            }));
        }
È stato utile?

Soluzione 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();
            }
        });
    }    

}

Altri suggerimenti

  • 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.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top