java - 如何扩展JComboBox类?

标签 java swing jcombobox class-extensions

我正在编写一个 Java GUI。我有一些预设的 JComboBoxes 并且能够将它们彼此区分开来,我想扩展该类并添加一个 enum 变量来帮助我区分它们其他。

这是两个标准 JComboBox 的 MCVE:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class XComboBoxMCVE extends JPanel {

    private JComboBox tfComboBox;
    private JComboBox ynComboBox;
    private JComponent[] components;

    public XComboBoxMCVE() {
        setLayout(new BorderLayout());
    
        JPanel comboPanel = new JPanel(new GridLayout(0, 1, 5, 5));

        Boolean[] trueFalse = { true, false };
        DefaultComboBoxModel tfModel = new DefaultComboBoxModel(trueFalse);
        tfComboBox = new JComboBox(tfModel);

        String[] yesNo = { "Yes", "No" };
        DefaultComboBoxModel ynModel = new DefaultComboBoxModel(yesNo);
        ynComboBox = new JComboBox(ynModel);

        components = new JComponent[] { tfComboBox, ynComboBox };
    
        JButton printSelection = new JButton("Print Type");
        printSelection.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                for (JComponent component : components) {
                    // I also have other components in component in program,
                    // therefore this usage..
                    if (component instanceof JComboBox) {
                        JComboBox temp = (JComboBox) component;
                        System.out.println("Printing selection: " + temp.getSelectedItem().toString());
                        // if (temp.getBoxType() == BoxType.Company){
                        // System.out.println("Companies say: " +
                        // temp.getSelectedItem().toString());
                        // } else if(temp.getBoxType() == BoxType.Device){
                        // System.out.println("Devices are: " +
                        // temp.getSelectedItem().toString());
                        // }
                    }
                }
            }
        });

        JPanel buttonPane = new JPanel(new GridLayout(0, 1, 5, 5));
        buttonPane.add(printSelection);

        comboPanel.add(tfComboBox);
        comboPanel.add(ynComboBox);

        add(comboPanel, BorderLayout.CENTER);
        add(buttonPane, BorderLayout.PAGE_END);
    }

    public static void createAndShowGUI(){
        JFrame frame = new JFrame("MCVE");
        frame.setLayout(new BorderLayout());
    
        XComboBoxMCVE pane = new XComboBoxMCVE();
        
        frame.add(pane, BorderLayout.CENTER);
        frame.setResizable(false);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

class XComboBox extends JComboBox {
    BoxType type;

    public XComboBox(BoxType type) {
        this.type = type;
    }

    public void setBoxType(BoxType type) {
        this.type = type;
    }

    public BoxType getBoxType() {
        return this.type;
    }

    public enum BoxType {
        Model, Company, Device
    }
}

如上面的示例所示,当用户单击按钮时,我无法区分两个 JComboBox。使用 BoxType 的一个示例是,一个 BoxType 将打印一种类型的消息,而另一个 BoxType 将打印另一种消息。例如:

if(temp.getBoxType() == BoxType.Device){
    System.out.println("The devices are: " + temp.getSelectedItem().toString());
} else if(temp.getBoxType() == BoxType.Company){
    System.out.println("The companies say: " + temp.getSelectedItem().toString());
}

但是,我偶然发现了构造函数的问题,我尝试使用类似于使用 JComboBox 所做的操作并输入 DefaultComboBoxModel,但我没有输入该构造函数尚未在 XComboBox 类中实现。

问题

如何修改 XComboBox 类,以便在构造元素时为其提供 DefaultComboBoxModel

我希望能够做到这一点:

ynComboBox = new XComboBox(tfModel);
ynComboBox.setBoxType(BoxType.Device);

感谢您的帮助和/或指导!

最佳答案

您需要将 JComboBox 与 BoxType 关联起来,关联两个对象的最佳方法之一是使用 Map,这里是 Map<BoxType, JComboBox> 。完成此操作后,您可以轻松地使用组合框提取其中的选项。再次强调,我会避免扩展 JCombo。例如,我的 MCVE:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class XComboBoxMCVE extends JPanel {
    private static final long serialVersionUID = 1L;
    private Map<BoxType, JComboBox<String>> comboMap = new EnumMap<>(BoxType.class);

    public XComboBoxMCVE() {
        setLayout(new BorderLayout());
        JPanel comboPanel = new JPanel(new GridLayout(0, 1, 5, 5));

        // just for example
        String[] modelItemsArray = {"Model Item A", "Model Item B", "Model Item C", "Model Item D"};
        BoxItems modelItems = new BoxItems(BoxType.MODEL, modelItemsArray);
        String[] CompanyItemsArray = {"Company Item A", "Company Item B", "Company Item C", "Company Item D"};
        BoxItems companyItems = new BoxItems(BoxType.COMPANY, CompanyItemsArray);
        String[] deviceItemsArray = {"Device Item A", "Device Item B", "Device Item C", "Device Item D"};
        BoxItems deviceItems = new BoxItems(BoxType.DEVICE, deviceItemsArray);

        createAndPlaceComboBox(BoxType.MODEL, modelItems, comboPanel);
        createAndPlaceComboBox(BoxType.COMPANY, companyItems, comboPanel);
        createAndPlaceComboBox(BoxType.DEVICE, deviceItems, comboPanel);

        JButton printSelection = new JButton("Print Type");
        printSelection.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                for (BoxType boxType : BoxType.values()) {
                    // use the Map to get the appropriate JComboBox
                    JComboBox<String> combo = comboMap.get(boxType);
                    String selection = (String) combo.getSelectedItem();
                    if (selection == null) {
                        selection = "NONE";
                    }
                    String text = String.format("Selection for box type %s: %s: ", 
                            boxType.getText(), selection);
                    System.out.println(text);
                }
            }
        });

        JPanel buttonPane = new JPanel(new GridLayout(0, 1, 5, 5));
        buttonPane.add(printSelection);

        add(comboPanel, BorderLayout.CENTER);
        add(buttonPane, BorderLayout.PAGE_END);
    }

    private void createAndPlaceComboBox(BoxType boxType, BoxItems boxItems, JPanel panel) {
        String[] items = boxItems.getItems().toArray(new String[] {});
        DefaultComboBoxModel<String> comboModel = new DefaultComboBoxModel<>(items); // create model
        JComboBox<String> combo = new JComboBox<>(comboModel); // crteate combo
        comboMap.put(boxType, combo);  // put combo into Map

        JPanel wrapPanel = new JPanel(new BorderLayout());  // wrapper panel that has a title border 
        wrapPanel.add(combo);
        wrapPanel.setBorder(BorderFactory.createTitledBorder(boxType.getText() + " Items"));
        panel.add(wrapPanel);
    }

    public static void createAndShowGUI() {
        JFrame frame = new JFrame("MCVE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        XComboBoxMCVE pane = new XComboBoxMCVE();
        frame.add(pane, BorderLayout.CENTER);
        frame.setResizable(false);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

// A better enum, one not inside of an unnecessary class
enum BoxType {
    MODEL("Model"), COMPANY("Company"), DEVICE("Device");
    private String text;

    private BoxType(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }
}

// class to associate BoxType with possible items associated with it
class BoxItems {
    private BoxType boxType;
    private List<String> items = new ArrayList<>();

    public BoxItems(BoxType boxType) {
        this.boxType = boxType;
    }

    public BoxItems(BoxType boxType, String[] itemsArray) {
        this(boxType);
        for (String item : itemsArray) {
            items.add(item);
        }
    }

    public void addItem(String item) {
        items.add(item);
    }

    public BoxType getBoxType() {
        return boxType;
    }

    public List<String> getItems() {
        // unmodifiable so that can't be changed outside of this class
        return Collections.unmodifiableList(items);
    }
}

关于java - 如何扩展JComboBox类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38398449/

相关文章:

java - do-while 循环跳过代码

java - 在 java 中如何将一个 JPanel 添加到另一个 JPanel

java - 如何自定义 JComboBox 以便弹出窗口是 JTree(而不是列表)?

java - 使用 TableModelListener 设置 JComboBox 编辑器

java - 在 Spring MessageSource 中使用文件夹模式

java - 如何在 Java 中显示 65687x70562 Tif 文件

java - 使用 Java Swing 在 JTable 上获取字符串值而不是复选框?

java - 如何通过单击 JButton 最小化 JFrame

java - JTable 单元格内的 JComboBox

java - 获取请求的自定义 header (Java HTTP)