简体   繁体   English

选择特定的 JComboBox 项目时如何向 JPanel 添加额外的 JButton

[英]How to add additional JButton to JPanel when specific JComboBox item is selected

I have been facing this small issue regarding making my JCombobox as dynamic as it can be.我一直面临这个关于使我的 JCombobox 尽可能动态的小问题。
For an example, when a selected item in combobox is selected, it will dynamically change the number of buttons accordance to the number of days within the month and be added in the panel例如,当combobox中的某一项被选中时,它会根据当月的天数动态改变按钮的数量并添加到面板中

The problem i am facing is that it is not automatically changing the display of the panel , but when i tried to see if the code runs in my console log.我面临的问题是它不会自动更改面板的显示,但是当我尝试查看代码是否在我的控制台日志中运行时。 it runs smoothly .它运行顺利。 ive tried my best to find a solution but to no avail.我已尽力寻找解决方案,但无济于事。

The main problem is within the actionListener, for eg if February is selected , it will display 28 buttons, if January is selected, it will display 31 days etc etc but when i run the code, my system.out.println states it runs but my Gui shows no button .主要问题是在 actionListener 中,例如,如果选择二月,它将显示 28 个按钮,如果选择一月,它将显示 31 天等等,但是当我运行代码时,我的 system.out.println 声明它运行但是我的 Gui 没有显示按钮。

在此处输入图片说明

private static JButton method_Btn(int i){
    JButton btn = new JButton(Integer.toString(i));
    return btn;
}

public static void day(){
    JFrame frame = new JFrame();
    JPanel topPanel = new JPanel();
    JPanel centerPanel = new JPanel();
    JButton days = new JButton();
    JLabel days_label = new JLabel();


    //-- Top Panel
    String month[] = {"--Select Month--" , "January", "February"};
    JComboBox month_combo = new JComboBox(month);
    topPanel.add(month_combo, BorderLayout.PAGE_START);

    //-- Center Panel
    centerPanel.setLayout(new FlowLayout());
    centerPanel.add(days_label);



    //------- Change when jcombo is selected
    month_combo.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            if(month_combo.getSelectedItem().equals("January")){
                for(int i = 0;i < 31;i++){
                    centerPanel.add(method_Btn(i));
                }
            }

            if(month_combo.getSelectedItem().equals("February")){
                for(int i = 0;i < 28;i++){
                    centerPanel.add(method_Btn(i));
                }
            }
        }
    });

    frame.add(topPanel, BorderLayout.PAGE_START);
    frame.add(centerPanel , BorderLayout.CENTER);

    frame.setSize(400,400);
    frame.setVisible(true);
}

public static void main(String[] args){
    day();

}

Addtional Note, Ive come to realised an additional problem i faced is that it will stack up the number of button created after selecting the month 2nd time.附加说明,我意识到我面临的另一个问题是它会叠加第二次选择月份后创建的按钮数量。 How i solve it is i added centerPanel.removeAll();我如何解决它是我添加了 centerPanel.removeAll(); and centerPanel.repaint();和 centerPanel.repaint();

month_combo.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            int count = 0;
            //---- gettind days of month selected in comboBox


                if (month_combo.getSelectedItem().equals("February")) {
                    centerPanel.removeAll();
                    centerPanel.repaint();
                    for (int i = 1; i <= 28; i++) {
                        centerPanel.add(method_Btn(i));
                        System.out.println("days in feb " + i);
                    }
                    centerPanel.revalidate();
                }



            if (month_combo.getSelectedItem().equals("March")) {

                centerPanel.removeAll();
                centerPanel.repaint();
                for (int i = 1; i <= 31; i++) {
                    centerPanel.add(method_Btn(i));
                }
                centerPanel.revalidate();

            }
        }
    });

Hope this help anybody who is in need.希望这能帮助任何有需要的人。 :) :)

You need to revalidate() the component you have added like following:您需要revalidate()已添加的组件,如下所示:

centerPanel.revalidate();

You need to change the following code:您需要更改以下代码:

month_combo.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent arg0) {
        if(month_combo.getSelectedItem().equals("January")){
            for(int i = 0;i < 31;i++){
                centerPanel.add(method_Btn(i));
            }
        }

        if(month_combo.getSelectedItem().equals("February")){
            for(int i = 0;i < 28;i++){
                centerPanel.add(method_Btn(i));
            }
        }

        centerPanel.revalidate(); // Need to add this for revalidation for the component
    }
});

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

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