Dynamic generation of buttons in Java

后端 未结 3 1721
深忆病人
深忆病人 2020-12-04 00:03

I am trying to dynamically generate a form. Basically, I want to load a list of items for purchase, and generate a button for each. I can confirm that the buttons are bein

3条回答
  •  天涯浪人
    2020-12-04 00:50

    You can use revalidate(), as shown in this example.

    Addendum: Here's my variation on @mKorbel's interesting answer that shows a similar result for GridLayout. It looks like repaint() may be necessary after revalidate().

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    /** @see https://stackoverflow.com/questions/6395105 */
    public class ValidateRevalidateRepaint {
    
        private JPanel center;
        private boolean validate = false;
        private boolean revalidate = true;
        private boolean repaint = true;
    
        public ValidateRevalidateRepaint() {
            center = new JPanel(new GridLayout(1, 0, 10, 10));
            JFrame f = new JFrame();
            f.setTitle("VRR");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(center, BorderLayout.CENTER);
            f.add(getRadioPanel(), BorderLayout.EAST);
            f.add(getCheckBoxPanel(), BorderLayout.SOUTH);
            makeChange(4);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    
        private JPanel getRadioPanel() {
            JPanel panel = new JPanel(new GridLayout(0, 1));
            ButtonGroup group = new ButtonGroup();
            ActionListener l = new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    JRadioButton radio = (JRadioButton) e.getSource();
                    int n = Integer.parseInt(radio.getActionCommand());
                    makeChange(n);
                }
            };
            for (int j = 0; j < 4; j++) {
                String s = String.valueOf(j + 1);
                JRadioButton radio = new JRadioButton(s);
                radio.setActionCommand(s);
                radio.addActionListener(l);
                group.add(radio);
                panel.add(radio);
                if (j == 3) {
                    group.setSelected(radio.getModel(), true);
                }
            }
            return panel;
        }
    
        private JPanel getCheckBoxPanel() {
            final String[] operations = {"validate", "revalidate", "repaint"};
            ActionListener l = new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    JCheckBox checkBox = (JCheckBox) e.getSource();
                    String ac = checkBox.getActionCommand();
                    boolean state = checkBox.isSelected();
                    if (ac.equals("validate")) {
                        validate = state;
                    }
                    if (ac.equals("revalidate")) {
                        revalidate = state;
                    }
                    if (ac.equals("repaint")) {
                        repaint = state;
                    }
                }
            };
            JPanel panel = new JPanel();
            for (int j = 0; j < operations.length; j++) {
                JCheckBox check = new JCheckBox(operations[j]);
                if (j == 0) {
                    check.setSelected(false);
                } else {
                    check.setSelected(true);
                }
                check.setActionCommand(operations[j]);
                check.addActionListener(l);
                panel.add(check);
            }
            return panel;
        }
    
        private void makeChange(int number) {
            center.removeAll();
            for (int j = 0; j < number; j++) {
                center.add(getFiller());
            }
            if (validate) {
                center.validate();
            }
            if (revalidate) {
                center.revalidate();
            }
            if (repaint) {
                center.repaint();
            }
        }
    
        private JPanel getFiller() {
            JPanel panel = new JPanel();
            panel.setBorder(BorderFactory.createLineBorder(Color.blue, 5));
            panel.setBackground(Color.red);
            panel.setPreferredSize(new Dimension(50, 50));
            return panel;
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new ValidateRevalidateRepaint();
                }
            });
        }
    }
    

提交回复
热议问题