无法获取ListCellRenderer正常工作 [英] Can't get ListCellRenderer to function properly

查看:191
本文介绍了无法获取ListCellRenderer正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我张贴的问题今天早些时候,被导演MadProgrammer使用ListCellRenderer来达到理想的效果。我有几乎工作,但它显示在组合框中两次相同的项目,我不知道为什么。请帮我解决了这个谜。在code:

I posted a question earlier today and was directed by MadProgrammer to use ListCellRenderer to achieve the desired results. I have it almost working, but it is showing the same entry twice in the combobox and I don't know why. Please help me solve this riddle. The code:

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

public class NotWorking extends JPanel {
    private JPanel editPanel;
    private JComboBox<String> editComboLevel;
    private JComboBox editCombo;
    private String[] levels = {"Level 1", "Level 2", "Level 3"}; 
    private static ArrayList<NotWorking> course = new ArrayList<NotWorking>();
    public static String courseNum, courseTitle, courseLevel;

    public JPanel createContentPane (){

        Integer[] intArray = new Integer[course.size()];
        for (int i = 0; i < course.size(); i++) {
            intArray[i] = new Integer(i);
        }

        editPanel = new JPanel(new GridLayout(4,2));
        editPanel.setPreferredSize(new Dimension(100,75));
        editPanel.add(editCombo = new JComboBox(intArray));
        ComboBoxRenderer renderer= new ComboBoxRenderer();
        editCombo.setRenderer(renderer);

        return editPanel;
        }

        NotWorking() {}
        NotWorking(String courseNum, String courseTitle, String courseLevel) {
            this.courseNum = courseNum;
            this.courseTitle = courseTitle;
            this.courseLevel = courseLevel;
        }
        @Override
        public String toString() {
            String courseInfo = getCourseNum() + ", " + getCourseTitle() + ", " + getCourseLevel();
            return courseInfo; 
        }
        public String getCourseNum() {
            return this.courseNum;          
        }
        public String getCourseTitle() {
            return this.courseTitle;
        }
        public String getCourseLevel() {
            return this.courseLevel;
        }
        public void setCourseNum(String courseNum) {
            this.courseNum = courseNum;
        }
        public void setCourseTitle(String courseTitle) {
            this.courseTitle = courseTitle; 
        }
        public void setCourseLevel(String courseLevel) {
            this.courseLevel = courseLevel;
        }

        private static void createAndShowGUI() {
            JFrame frame = new JFrame("Example of Code Snippet");
            NotWorking myCourse = new NotWorking();
            frame.setContentPane(myCourse.createContentPane());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.pack();
            frame.setVisible(true);
        }

        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
            course.add(new NotWorking("Course1", "Course1 desc", "Level 1"));
            course.add(new NotWorking("Course2", "Course2 desc", "Level 2"));
            createAndShowGUI();
            for(NotWorking item : course)
                System.out.println(item);
            }
        });
    }
    class ComboBoxRenderer extends JLabel
                           implements ListCellRenderer {
        public ComboBoxRenderer() {
            setOpaque(true);
            setHorizontalAlignment(CENTER);
            setVerticalAlignment(CENTER);
        }

        public Component getListCellRendererComponent(
                                           JList list,
                                           Object value,
                                           int index,
                                           boolean isSelected,
                                           boolean cellHasFocus) {
            int selectedIndex = ((Integer)value).intValue();

            if (isSelected) {
                setBackground(list.getSelectionBackground());
                setForeground(list.getSelectionForeground());
            } else {
                setBackground(list.getBackground());
                setForeground(list.getForeground());
            }

            setText(getCourseNum());

            return this;
        }
    }
}

正如你可以看到有2添加到ArrayList。我限制了组合框,只显示课程编号显示,但Course2正显示出了两次,当我打印出来的ArrayList的内容我看到所有的Course2为Course1两次,并没有显示的细节。任何帮助将大大AP preciated。干杯

As you can see there are 2 additions to the ArrayList. I am limiting the display in the combobox to only show the course number, but Course2 is showing up twice and when I print out the contents of the ArrayList I see all the details for Course2 shown twice and none for Course1. Any help would be greatly appreciated. Cheers

推荐答案

使用自定义渲染只是解决方案的一半。自定义渲染器将使用组合框的键盘打破了默认项选择。请参见组合框自定义呈现了解更多信息和更完整的解决方案。

Using a custom renderer is only half of the solution. A custom renderer will break the default items selection by using the keyboard of a combo box. See Combo Box With Custom Renderer for more information and a more complete solution.

与code的主要问题是NotWorking类。这个类不应该扩展JPanel。它只是用来保持类的3个属性的类。该课程数量,名称和级别不应该静态变量。应该没有参考Swing组件。

The main problem with your code is the NotWorking class. This class should NOT extend JPanel. It is just a class that is used to hold the 3 properties of the class. The course number, title and level should NOT be static variables. There should be no reference to Swing components.

您的设计应该是NotWorking类一类和其他类来创建GUI。

Your design should be one class for the NotWorking class and another class to create the GUI.

与开始从Swing教程如何使用组合框获取一个更好的设计。然后从教程定制 ComboBoxDemo.java 例如code你NotWorking类添加到组合框,而不是添加字符串数据。

Start with the section from the Swing tutorial on How to Use Combo Boxes for a better design. Then customize the ComboBoxDemo.java example code from the tutorial to add your NotWorking class to the combo box instead of adding String data.

这篇关于无法获取ListCellRenderer正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆