简体   繁体   English

使用Java Swing删除选定的复选框

[英]Deleting selected checkboxes using java swing

I am new to java swing. 我是java swing的新手。 I have a code that generates checkboxes. 我有一个生成复选框的代码。 I want to have a button somewhere in my frame, which on clicking, should delete the selected checkbox entries. 我想在框架的某处有一个按钮,单击该按钮应删除所选的复选框条目。 Here is what I have so far. 这是我到目前为止所拥有的。

public class Scroll extends JPanel  {  


public static void main(String[] args) {   
     SwingUtilities.invokeLater(new Runnable(){  
         public void run()    {  
             createAndShowGUI();   
             }   
         });   
     }   
 public static void createAndShowGUI()     {  
     JFrame frame = new JFrame("JFrame with ScrollBar"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
     JComponent newContentPane = new ResultButtonBar();
     newContentPane.setOpaque(true);
     JScrollPane scrollPane = new JScrollPane(newContentPane); 
     scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);  
     frame.getContentPane().add(scrollPane);  
     frame.setSize(800, 800); 
     frame.setVisible(true);
     JButton startButton = new JButton("Start");
     frame.add(startButton, BorderLayout.SOUTH);
     startButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub

            JOptionPane.showMessageDialog(null, "basdsadad");

                }
        });

    }

  } 

and the new ResultButtonBar().java 和新的ResultButtonBar()。java

public class ResultButtonBar extends JPanel  {   

  private HashMap<JCheckBox, ArrayList<Integer>> map = new HashMap<>();
    private JLabel _label;

    private static final int MAX_CHECKS = 1000;

    public ResultButtonBar() {
        super();

        JButton btn = new JButton();
        btn.setVisible(true);

        JCheckBox checkBox;
        Random r = new Random();

        JPanel checkPanel = new JPanel(new GridLayout(0, 1));
        _label = new JLabel("You selected nothing");
        checkPanel.add(_label);

        for (int i = 0; i < MAX_CHECKS; i++) {
            StringBuilder sb = new StringBuilder();
            ArrayList<Integer> a = new ArrayList<>();
            for (int j = 0; j < 2; j++) {
                Integer temp = (r.nextInt()) % 100;
                a.add(temp);
                sb.append(temp).append(" ");
            }

            checkBox = new JCheckBox(sb.toString().trim());
            checkBox.setName("CheckBox" + i);

            map.put(checkBox, a);
            checkPanel.add(checkBox);
        }

        add(checkPanel);

    }

}

First of all, keep all your check boxes in an ArrayList so you will have a reference to them when you need it. 首先,将所有复选框保留在ArrayList以便在需要时可以对其进行引用。

Then, add a JButton wherever you need. 然后,在需要的地方添加一个JButton Then iterate over this ArrayList and call invalidate() on the component which contains your check boxes. 然后遍历此ArrayList并在包含您的复选框的组件上调用invalidate() Next statement would be to call the remove() method on the container; 下一条语句是在容器上调用remove()方法。 the checkPanel . checkPanel

Alternatively, you may call removeAll() if all the components in the container are check boxes and you want to remove them. 或者,如果容器中的所有组件都是复选框并且您要删除它们,则可以调用removeAll()

The alternative pointed by StanislavL is also a good one if you have a lot of different components along with check boxes 如果您有很多不同的组件以及复选框,那么StanislavL指出的替代方法也是一个很好的选择。

I can think of two approaches: 我可以想到两种方法:

  • if You are maintaining one JPanel instance which contains only the instances of JCheckBox , then you can first get all the checkbox's using panel.getComponents() method, check their selection state and depending on the state remove it by calling panel.remove(component) . 如果要维护一个仅包含JCheckBox实例的JPanel实例,则可以首先使用panel.getComponents()方法获取所有复选框的复选框,检查其选择状态,并根据状态通过调用panel.remove(component)将其删除。 。 For example: 例如:

     Component checkBox[] = checkBoxPanel.getComponents(); for(Component c:checkBox) if(((JCheckBox)c).isSelected()) checkBoxPanel.remove(c); checkBoxPanel.revalidate(); checkBoxPanel.repaint(); 

    The last call revalidate() and repaint() on the checkBoxPanel is important for reflecting changes on the layout and graphics rendering of the components. checkBoxPanel上的最后一次调用revalidate()repaint()对于反映组件的布局和图形呈现方面的更改很重要。

  • You can use ItemListener with the instances of JCheckBox to do things on selection state change. 您可以将ItemListenerJCheckBox实例一起使用以执行选择状态更改。 Use an instance of ArrayList<JCheckBox> to add the selected checkBox to the list. 使用ArrayList<JCheckBox>的实例将selected checkBox添加到列表中。 However you should use an implemented ItemListener: MyItemListener implements ItemListener and create one instance and add this instances to all the checkboxes to react on state change. 但是,您应该使用实现的ItemListener: MyItemListener implements ItemListener并创建一个实例,然后将此实例添加到所有checkboxes以对状态更改做出反应。 You can use event source e.getSource() to get the JCheckBox instance on which the ItemEvent is performed. 您可以使用事件源e.getSource()获取在其上执行ItemEventJCheckBox实例。

Tutorial resource: 教程资源:

  1. How to Write an Item Listener 如何编写项目监听器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM