简体   繁体   English

如何使JComboBox创建一个或多个新的JComboBox

[英]How to make JComboBox create one or more new JComboBoxes

How could I make a selection from a JComboBox remove or add selections to another JComboBox? 如何从JComboBox中选择删除或添加选择到另一个JComboBox?

More specifically, I have a JComboBox with the months of the year as selections. 更具体地说,我有一个JComboBox,一年中的几个月作为选择。 I have another with the days of the month in it. 我有另一个月的日子。 While the 31st should be a choice for December, it should not be for February. 虽然31日应该是12月的选择,但不应该是2月。 Any help would be appreciated. 任何帮助,将不胜感激。

    final JComboBox<String> monthBox = new JComboBox<String>();
    monthBox.addItem("January");
    monthBox.addItem("February");
    monthBox.addItem("March");
    monthBox.addItem("April");
    monthBox.addItem("May");
    monthBox.addItem("June");
    monthBox.addItem("July");
    monthBox.addItem("August");
    monthBox.addItem("September");
    monthBox.addItem("October");
    monthBox.addItem("November");
    monthBox.addItem("December");

    JComboBox<String> dayBox = new JComboBox<String>();

Simply attach a ActionListener to the master JComboBox and within the actionPerformed method, use JComboBox#getSelectedItem to determine what action you should take. 只需将ActionListener附加到主JComboBox并在actionPerformed方法中,使用JComboBox#getSelectedItem来确定应采取的操作。

You could set up a Map of options, one for each entry in the master JCheckBox which would make it easier to switch out the values 您可以设置一个选项Map ,一个用于主JCheckBox中的每个条目,这样可以更容易地切换值

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class JoinedComboBoxes {

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

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

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            final JComboBox<String> monthBox = new JComboBox<String>();
            monthBox.addItem("January");
            monthBox.addItem("February");
            monthBox.addItem("March");
            monthBox.addItem("April");
            monthBox.addItem("May");
            monthBox.addItem("June");
            monthBox.addItem("July");
            monthBox.addItem("August");
            monthBox.addItem("September");
            monthBox.addItem("October");
            monthBox.addItem("November");
            monthBox.addItem("December");

            setLayout(new GridBagLayout());
            add(monthBox);

            final JComboBox<Integer> days = new JComboBox<>();
            days.setEnabled(false);
            add(days);

            monthBox.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Calendar cal = Calendar.getInstance();
                    String month = (String) monthBox.getSelectedItem();
                    days.setEnabled(month != null);
                    if (month != null) {
                        int calMonth = 0;
                        switch (month) {
                            case "January":
                                calMonth = Calendar.JANUARY;
                                break;
                            case "February":
                                calMonth = Calendar.FEBRUARY;
                                break;
                            case "March":
                                calMonth = Calendar.MARCH;
                                break;
                            case "April":
                                calMonth = Calendar.APRIL;
                                break;
                            case "May":
                                calMonth = Calendar.MAY;
                                break;
                            case "June":
                                calMonth = Calendar.JUNE;
                                break;
                            case "July":
                                calMonth = Calendar.JULY;
                                break;
                            case "August":
                                calMonth = Calendar.AUGUST;
                                break;
                            case "September":
                                calMonth = Calendar.SEPTEMBER;
                                break;
                            case "October":
                                calMonth = Calendar.OCTOBER;
                                break;
                            case "November":
                                calMonth = Calendar.NOVEMBER;
                                break;
                            case "December":
                                calMonth = Calendar.DECEMBER;
                                break;
                        }
                        days.removeAllItems();
                        cal.setTime(new Date()); // For the current year...
                        cal.set(Calendar.MONTH, calMonth);
                        int min = cal.getActualMinimum(Calendar.DATE);
                        int max = cal.getActualMaximum(Calendar.DATE);
                        for (int day = min; day <= max; day++) {
                            days.addItem(day);
                        }
                    }
                }
            });
            monthBox.setSelectedItem(null);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.dispose();
        }
    }
}

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

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