有没有办法在 JList 中使用枚举?

Is there a way of using Enum in a JList?

我知道 JListStringDefaultListModel 的构造函数。我想找到一种方法来使用 enum 类型的列表,例如 JList<SomeEnum> jlist

有办法实现吗?

这是一个小 运行 示例:

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

public class Example implements Runnable {
    private JPanel jPanel;
    private JFrame jFrame;
    private JList<Algorithm> diagnosisList;
    public static void main(String[] args) {
        EventQueue.invokeLater(new Example());
    }
    @Override
    public void run() {
        jFrame = new JFrame();
        jPanel = new JPanel();
        jPanel.setLayout(new FlowLayout());
        jFrame.getContentPane().add(jPanel);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jPanel.setPreferredSize(new Dimension(500, 300));
        DefaultListModel defaultListModel = new DefaultListModel();
        defaultListModel.add(0, "Decision Tree");
        defaultListModel.add(1, "Random Forest");
        diagnosisList = getJList(defaultListModel);

        jPanel.add(diagnosisList);

        jFrame.pack();
        jFrame.setVisible(true);
    }
    // I want to use Algorithm enum for parameter instaed of DefaultListModel
    private JList<Algorithm> getJList(DefaultListModel algorithms) {
        JList<Algorithm> jlist = new JList<Algorithm>(algorithms);
        return jlist;
    }


    enum Algorithm {
        DECISION_TREE_CLASSIFIER_DIAGNOSTIC("Decision Tree Classifier (D)", "Diagnostic"),
        RANDOM_FOREST_DIAGNOSTIC("Random Forest (D)", "Diagnostic"),
        LOGISTIC_REGRESSION_DIAGNOSTIC("Logistic Regression (D)", "Diagnostic"),
        K_MEANS_DIAGNOSTIC("KMeans (D)", "Diagnostic");

        private final String name;
        private final String type;

        Algorithm(final String name, final String type) {
            this.name = name;
            this.type = type;
        }

        @Override public String toString() {
            return name;
        }
    }
}

您可以将 enum 的值添加到 JList 中,就像任何其他数组一样。例如:

public class Example implements Runnable {
    private JPanel jPanel;
    private JFrame jFrame;
    private JList<Algorithm> diagnosisList;
    private DefaultListModel<Algorithm> model;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Example());
    }

    @Override
    public void run() {
        jFrame = new JFrame();
        jPanel = new JPanel();
        jPanel.setLayout(new FlowLayout());
        jFrame.getContentPane().add(jPanel);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jPanel.setPreferredSize(new Dimension(500, 300));
        model = new DefaultListModel<>();
        diagnosisList = new JList<Algorithm>(model);

        for (Algorithm a : Algorithm.values())
            model.addElement(a);

        jPanel.add(diagnosisList);

        jFrame.pack();
        jFrame.setVisible(true);
    }

    public static enum Algorithm {
        BFS, DFS, A_STAR
    }
}

如果您不喜欢它的呈现方式,您可以随时添加自己的 ListCellRenderer:

diagnosisList.setCellRenderer(new DefaultListCellRenderer() {
    private static final long serialVersionUID = 1L;

    @Override
    public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected,
            boolean cellHasFocus) {
        JLabel renderer = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        renderer.setText(renderer.getText().toLowerCase()); //something
        return renderer;
    }
});