Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

swing - Dynamic generation of buttons in Java

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 being generated with the debugger, but they aren't being displayed. This is inside a subclass of JPanel:

private void generate() {
    JButton b = new JButton("height test");
    int btnHeight = b.getPreferredSize().height;
    int pnlHeight = this.getPreferredSize().height;
    int numButtons = pnlHeight / btnHeight;

    setLayout(new GridLayout(numButtons, 1));

    Iterator<Drink> it = DrinkMenu.iterator();

    for (int i = 0; i <= numButtons; ++i) {
        if (!it.hasNext()) {
            break;
        }
        final Drink dr = it.next();
        b = new DrinkButton(dr);
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                order.addDrink(dr);
        }});
        add(b);
    }
    revalidate();
}

DrinkButton is a subclass of JButton. Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

example about validate() revalidate() plus repaint(), look like as required for correct output to the GUI, layed by some of LayourManagers

EDIT: as trashgod noticed, I added Schedule a job for the EDT

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class ValidateRevalidateRepaint {

        private JPanel panel;
        private GridBagConstraints gbc;
        private boolean validate, revalidate, repaint;

        public ValidateRevalidateRepaint() {
            validate = revalidate = repaint = false;
            panel = new JPanel(new GridBagLayout());
            gbc = new GridBagConstraints();
            gbc.insets = new Insets(0, 20, 0, 20);
            panel.add(getFiller(), gbc);
            JFrame f = new JFrame();
            f.setJMenuBar(getMenuBar());
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(panel);
            f.getContentPane().add(getRadioPanel(), "East");
            f.getContentPane().add(getCheckBoxPanel(), "South");
            f.setSize(400, 200);
            f.setLocation(200, 200);
            f.setVisible(true);
        }

        private JMenuBar getMenuBar() {
            JMenu menu = new JMenu("change");
            ActionListener l = new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    JMenuItem item = (JMenuItem) e.getSource();
                    int n = Integer.parseInt(item.getActionCommand());
                    makeChange(n);
                }
            };
            for (int j = 1; j < 5; j++) {
                String s = String.valueOf(j) + " component";
                if (j > 1) {
                    s += "s";
                }
                JMenuItem item = new JMenuItem(s);
                item.setActionCommand(String.valueOf(j));
                item.addActionListener(l);
                menu.add(item);
            }
            JMenuBar menuBar = new JMenuBar();
            menuBar.add(menu);
            return menuBar;
        }

        private JPanel getRadioPanel() {
            JPanel panel1 = new JPanel(new GridBagLayout());
            GridBagConstraints gbc1 = new GridBagConstraints();
            gbc1.insets = new Insets(2, 2, 2, 2);
            gbc1.weighty = 1.0;
            gbc1.gridwidth = GridBagConstraints.REMAINDER;
            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);
                panel1.add(radio, gbc1);
            }
            return panel1;
        }

        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 panel2 = new JPanel();
            for (int j = 0; j < operations.length; j++) {
                JCheckBox check = new JCheckBox(operations[j]);
                check.setActionCommand(operations[j]);
                check.addActionListener(l);
                panel2.add(check);
            }
            return panel2;
        }

        private void makeChange(int number) {
            panel.removeAll();
            for (int j = 0; j < number; j++) {
                panel.add(getFiller(), gbc);
            }
            if (validate) {
                panel.validate();
            }
            if (revalidate) {
                panel.revalidate();
            }
            if (repaint) {
                panel.repaint();
            }
        }

        private JPanel getFiller() {
            JPanel panel3 = new JPanel();
            panel3.setBackground(Color.red);
            panel3.setPreferredSize(new Dimension(40, 40));
            return panel3;
        }

        public static void main(String[] args) {//added Schedule a job for the EDT
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                ValidateRevalidateRepaint rVR = new ValidateRevalidateRepaint();
            }
        });
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...