简体   繁体   English

在Java-NetBeans中动态创建dcheckbox

[英]Creating dcheckbox dynamically in Java-NetBeans

I am developing a simple "To-do List" application which involves obtaining 'things to do' for a particular day from the database and displaying them as checkbox text on a panel resting on a frame.There is a button "done" which can be used to remove the ticked checkboxes after a task is complete. 我正在开发一个简单的“待办事项列表”应用程序,该应用程序涉及从数据库中获取特定日期的“要做的事情”,并将其作为复选框文本显示在框架上的面板上。有一个按钮“完成”可以用于在任务完成后删除勾选的复选框。

The code that I used for the dynamic creation of the checkboxes is shown below: 我用于动态创建复选框的代码如下所示:

//cnt-variable used to store the number of tasks for a day    
//rs1-ResultSet variable into which the task description is read into.    
//DATA-variable with 'to-do' description

 for(int i=0;i<cnt&&rs1.next();i++)
 {
     String s2=rs1.getString("DATA");
     JCheckBox cb = new JCheckBox("New CheckBox");
     cb.setText(s2);
     cb.setVisible(true);

     jPanel1.add(cb);
     jPanel1.validate();
 }

On running the code all it displays is an empty frame with the panel. 在运行代码时,它显示的是带有面板的空框架。 Could someone help me figure out why the check boxes are not being displayed? 有人可以帮我弄清楚为什么没有显示复选框? Thanks in advance. 提前致谢。

Try this. 尝试这个。 This allows you to create a random number of check boxes... 这允许您创建一个随机数量的复选框...

在此输入图像描述

public class TestCheckboxes {

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

    public TestCheckboxes() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new CheckBoxPane());
                frame.setSize(400, 400);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public class CheckBoxPane extends JPanel {

        private JPanel content;

        public CheckBoxPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1;
            gbc.weighty = 1;
            gbc.anchor = GridBagConstraints.CENTER;
            content = new JPanel(new GridBagLayout());
            add(content, gbc);

            JButton more = new JButton("More");
            more.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    GridBagConstraints gbc = new GridBagConstraints();
                    gbc.gridx = 0;
                    gbc.gridy = 0;

                    content.removeAll();
                    int count = 10 + (int) Math.round(Math.random() * 90);
                    System.out.println(count);
                    for (int index = 0; index < count; index++) {
                        gbc.gridx++;
                        if (index % 8 == 0) {
                            gbc.gridx = 0;
                            gbc.gridy++;
                        }
                        content.add(new JCheckBox(Integer.toString(index)), gbc);
                    }

                    content.revalidate();
                    repaint();

                }

            });

            gbc.gridy++;
            gbc.weightx = 0;
            gbc.weighty = 0;

            add(more, gbc);
        }
    }
}

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

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