根据Java Swing中的选定复选框更改组合框值

时间:2018-11-27 10:48:02

标签: java swing jcombobox jcheckbox beans-binding

我正在尝试将JComboBox绑定到一组JCheckBox es。

有7个复选框(每周的每一天)。这些被捆绑在JPanel中,该Map<DayOfWeek, JComboBox> checkBoxes具有私有字段JComboBox,同时具有getter和setter。另一个组件是简单的enum,其值来自enum,表示重复类型(例如,用于在日历中设置重复事件)。假设此DAILY的值为WEEKLYEVERY_OTHER_WEEK DAILY

当所有7个复选框都被选中时,我需要组合框更改为WEEKLY,而当所选复选框的数量从7个减少时,将组合框更改为DAILY类型为DAILY,否则保持不变。

目前,我试图在选择全部7个时将组合框更改为JDialog。当实例化容器(DAILY并选择所有7天时,组合框选择的值将设置为convertForward()(执行下面的DAILY方法)。但是,无论我选择/取消选中哪个复选框,该方法都不会再执行。

添加最小的应用示例。我希望在选中两个复选框后,组合框的值将更改为package com.application; public enum RecurrenceType { NOT_SPECIFIED, DAILY, WEEKLY, EVERY_OTHER_WEEK; }

RecurrenceType.java

package com.application;

import java.time.DayOfWeek;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JCheckBox;
import javax.swing.JPanel;

public class DaysSelectionPanel extends JPanel {

    private Map<DayOfWeek, JCheckBox> checkBoxes;

    public DaysSelectionPanel() {
        initComponents();
        this.checkBoxes = new HashMap<DayOfWeek, JCheckBox>() {{
            put(DayOfWeek.MONDAY, mondayCheckBox);
            put(DayOfWeek.TUESDAY, tuesdayCheckbox);
        }};
    }

    public Map<DayOfWeek, JCheckBox> getCheckBoxes() {
        return checkBoxes;
    }

    private JCheckBox mondayCheckBox;
    private JCheckBox tuesdayCheckbox;

    private void initComponents() {
        mondayCheckBox = new JCheckBox();
        tuesdayCheckbox = new JCheckBox();
        add(mondayCheckBox);
        add(tuesdayCheckbox);
    }
}

DaysSelectionPanel.java

package com.application;

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.Binding;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.Bindings;
import org.jdesktop.beansbinding.Converter;
import java.time.DayOfWeek;
import java.util.Map;
import javax.swing.JCheckBox;

public class JavaApplication extends JFrame {
    public JavaApplication() {
        init();
    }

    private void init() {
        setTitle("JavaApplication");
        setSize(300, 300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        initComponents();
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            JavaApplication app = new JavaApplication();
            app.setVisible(true);
        });
    }

    private DaysSelectionPanel daysSelectionPanel;
    private JComboBox recurrenceTypeComboBox;
    private BindingGroup bindingGroup;

    private void initComponents() {
        bindingGroup = new BindingGroup();
        daysSelectionPanel = new DaysSelectionPanel();
        recurrenceTypeComboBox = new JComboBox();

        recurrenceTypeComboBox.setModel(new javax.swing.DefaultComboBoxModel(RecurrenceType.values()));

        Binding binding = Bindings.createAutoBinding(
            AutoBinding.UpdateStrategy.READ,
            daysSelectionPanel,
            recurrenceTypeComboBox,
            BeanProperty.create("selectedItem"));
        binding.setConverter(new Converter<DaysSelectionPanel, RecurrenceType>() {
            @Override
            public DaysSelectionPanel convertReverse(RecurrenceType value) {
                  return daysSelectionPanel;
            } 

            @Override
            public RecurrenceType convertForward(DaysSelectionPanel value) {
                for(Map.Entry<DayOfWeek, JCheckBox> entry : value.getCheckBoxes().entrySet()) {
                    if(!entry.getValue().isSelected()) {
                        return (RecurrenceType) recurrenceTypeComboBox.getSelectedItem();
                    }
                 }
                 return RecurrenceType.DAILY;
            }
        });
        bindingGroup.addBinding(binding);

        setLayout(new GridBagLayout());

        GridBagConstraints gridBagConstraints;
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridheight = 1;
        add(daysSelectionPanel, gridBagConstraints);

        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridheight = 1;
        add(recurrenceTypeComboBox, gridBagConstraints);
    }
}

JavaApplication.java (主类)

AutoBinding

我还尝试创建createAutoBinding(),并添加sourceProperty方法重载,其中包括ELProperty.create("${checkBoxes}"),并将其设置为 computed: { myObjectSorted: function () { return Object.keys(this.myObject).sort(...); // Do your custom sorting here } } 。行为保持不变。

关于以上代码中我出了什么问题的任何想法?

0 个答案:

没有答案