【问题标题】:How to check the state of a dynamically generated JCheckBox in an action listener如何在动作监听器中检查动态生成的 JCheckBox 的状态
【发布时间】:2022-02-03 20:27:56
【问题描述】:

我创建了链接到文件的动态 JChekbox。我想看看通过动作监听器检查了哪些。我试过 getSource(),我试过 getState() 都没有工作...

for (int f = 0; f < numberCheckBox[0]; f++) {
            String line1 = tableLines[f].toString().trim();
            String[] dataRow1 = line1.split("/");
            checkBoxList[f] = new JCheckBox(Arrays.toString(dataRow1).replace("[", "").replace("]", ""));
            Checkp.add(checkBoxList[f]);
            System.out.print(checkBoxList[f]);
        }

         save.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            BufferedReader br = null;
            try {
                br = new BufferedReader(new FileReader(files));
                Object[] tableLines = br.lines().toArray();
                numberCheckBox[0] = tableLines.length;
                for (int j = 0; j < numberCheckBox[0]; j++) {
                    if(e.getSource() == finalCheckBoxList[j]){
                        if(e.getStateChange() == 1){

                        }
                    }
                }
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            }

【问题讨论】:

  • 您需要做的就是获取 ActionEvent 的源并将该对象转换为 JCheckBox。然后您可以使用 getText() 方法。无需检查状态。
  • 好吧,您可能想使用isSelected 来确定按钮是否被选中
  • 我确实使用了 isSelected 但效果不佳。
  • @boualpso 那你做错了,因为isSelected 对我来说工作得很好,是查找按钮当前选定状态的最佳解决方案

标签: java swing


【解决方案1】:

您要做的第一件事是查看How to Use Buttons, Check Boxes, and Radio Buttons

所以来自JCheckBox JavaDocs

被选中

public boolean isSelected()

返回状态 按钮。如果选择了切换按钮,则为 true,否则为 false。

返回:
如果选择了切换按钮,则为 true,否则为 false

此外,如果您需要某种方式将信息传递给ActionListener。您可以继续依赖 index 的值,但如果可能,我总是更喜欢封装这些信息。

因此,在此示例中,我使用了 actionCommandclientProperty 方法来播种我可能想要在 ActionListener 中使用的信息

我确实使用了 isSelected,但效果不佳。

那你做错了,因为下面的例子工作得很好

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
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;

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());

            int[] numberCheckBox = new int[] { 5 };
            JCheckBox[] checkBoxList = new JCheckBox[5];
            String[] tableLines = new String[] {
                "this/is/a/test",
                "hello/world",
                "something/wicked/this/way/comes",
                "a/long/long/time/ago",
                "i/have/something/to/say",
            };

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = gbc.REMAINDER;
            gbc.anchor = GridBagConstraints.LINE_START;
            for (int f = 0; f < numberCheckBox[0]; f++) {
                String line1 = tableLines[f].toString().trim();
                String[] dataRow1 = line1.split("/");                
                checkBoxList[f] = new JCheckBox(String.join(", ", dataRow1));
                checkBoxList[f].setActionCommand(line1);
                checkBoxList[f].putClientProperty("value", line1);
                add(checkBoxList[f], gbc);
                System.out.print(checkBoxList[f]);
            }

            JButton save = new JButton("Save");
            gbc.anchor = GridBagConstraints.CENTER;
            add(save, gbc);

            save.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    for (JCheckBox cb : checkBoxList) {
                        if (cb.isSelected()) {
                            System.out.println("Action Command = " + cb.getActionCommand());
                            System.out.println("Client Property = " + (String)cb.getClientProperty("value"));
                        }
                    }
                }
            });
        }
    }
}

【讨论】:

  • 我很感激
猜你喜欢
  • 1970-01-01
  • 2018-01-06
  • 1970-01-01
  • 1970-01-01
  • 2020-03-27
  • 1970-01-01
  • 2012-07-12
  • 1970-01-01
  • 2021-07-02
相关资源
最近更新 更多