ListCellRenderer未显示所需的文本

时间:2013-12-09 02:56:54

标签: java jcombobox listcellrenderer

我试图只获取ArrayList元素中的第一个项目以显示在组合框中。有人建议使用ListCellRenderer可以帮助我实现这一目标。我已经尝试过,但是无法访问必要的getCourseNum()方法来显示。我的代码:

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

public class CourseCon extends JPanel {

private static JPanel editPanel;
private JComboBox<String> editComboLevel;
private JComboBox editCombo;
private static ArrayList<Course> course = new ArrayList<Course>();

    public CourseCon() {

        Integer[] intArray = new Integer[course.size()];
        for (int i = 0; i < course.size(); i++) {
            intArray[i] = new Integer(i);
        }
        editPanel = new JPanel();
        editPanel.setPreferredSize(new Dimension(100,70));
        editPanel.add(editCombo = new JComboBox(intArray));
        ComboBoxRenderer renderer= new ComboBoxRenderer();
        editCombo.setRenderer(renderer);
        editCombo.setSelectedIndex(0);
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("Example of Code Snippet");
        JComponent newContentPane = new CourseCon();
        newContentPane.setOpaque(true);
        frame.add(editPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setPreferredSize(new Dimension(120,80));
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                course.add(new Course("Course-1", "Description-1",
                            1, 2, "Level 1"));
                course.add(new Course("Course-2", "Description-2", 
                            3, 4, "Level 2"));
                createAndShowGUI();
                for(Course 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();
            //String course = courseArray[selectedIndex]; 
                //I couldn't get this to work

            if (isSelected) {
                setBackground(list.getSelectionBackground());
                setForeground(list.getSelectionForeground());
            } else {
                setBackground(list.getBackground());
                setForeground(list.getForeground());
            }
            //I put this in to test and it does show two times
            //makes me think if I could just access getCourseNum()
            //I'd have it. Correct me if I'm wrong please
            setText("Testing");

            return this;
        }
    }
}

以下是我正在使用的课程:

import java.util.*;

public class Course {

    private String courseNum, courseTitle, courseLevel;
    private int courseDur, courseFee;

    Course() {}

    Course(String courseNum, String courseTitle, int courseDur, 
                int courseFee, String courseLevel) {
        this.courseNum = courseNum;
        this.courseTitle = courseTitle;
        this.courseDur = courseDur;
        this.courseFee = courseFee;
        this.courseLevel = courseLevel;
    }
    @Override
    public String toString() {
        String courseInfo = this.getCourseNum()+ ", "+this.getCourseTitle()+", "
        +this.getCourseDur()+", "+this.getCourseFee()+", "+this.getCourseLevel();
        return courseInfo; 
    }
    public String getCourseNum() {
        return this.courseNum;          
    }
    public String getCourseTitle() {
        return this.courseTitle;
    }
    public int getCourseDur() {
        return this.courseDur;
    }
    public int getCourseFee() {
        return this.courseFee;
    }
    public String getCourseLevel() {
        return this.courseLevel;
    }
    public void setCourseNum(String courseNum) {
        this.courseNum = courseNum;
    }
    public void setCourseTitle(String courseTitle) {
        this.courseTitle = courseTitle; 
    }
    public void setCourseDur(int courseDur) {
        this.courseDur = courseDur; 
    }
    public void setCourseTitle(int courseFee) {
        this.courseFee = courseFee; 
    }
    public void setCourseLevel(String courseLevel) {
        this.courseLevel = courseLevel;
    }
}

我已经尝试了几天,但我在这里不知名的水域游泳。如何访问getCourseNum()方法,这是填充组合框的唯一方法?任何帮助将不胜感激。 注意:几天前我问了一个类似的问题,并建议我修改我的代码。这是我的新尝试。我是一名初学者,欢迎提出任何建议。干杯

1 个答案:

答案 0 :(得分:1)

首先,将所有Course添加到组合框中,这将使您的生活从长远来看变得更加轻松......

editPanel.add(editCombo = new JComboBox(course.toArray(new Course[course.size()])));

其次,修改你ComboBoxRenderer以接受Course而不是int ...

if (value instanceof Course) {
    Course course = (Course) value;
    value = course.getCourseNum();
}

最后,您可能需要考虑使用从DefaultListCellRenderer而不是JLabel进行扩展并实施ListCellRenderer,因为它会处理一大堆重要内容......比如选择突出....

enter image description here

import java.awt.Component;
import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.SwingUtilities;

public class CourseCon extends JPanel {

    private static JPanel editPanel;
    private JComboBox<String> editComboLevel;
    private JComboBox editCombo;
    private static ArrayList<Course> course = new ArrayList<Course>();

    public CourseCon() {

        editPanel = new JPanel();
        editPanel.setPreferredSize(new Dimension(100, 70));
        editPanel.add(editCombo = new JComboBox(course.toArray(new Course[course.size()])));
        ComboBoxRenderer renderer = new ComboBoxRenderer();
        editCombo.setRenderer(renderer);
        editCombo.setSelectedIndex(0);
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("Example of Code Snippet");
        JComponent newContentPane = new CourseCon();
        newContentPane.setOpaque(true);
        frame.add(editPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setPreferredSize(new Dimension(120, 80));
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                course.add(new Course("Course-1", "Description-1",
                        1, 2, "Level 1"));
                course.add(new Course("Course-2", "Description-2",
                        3, 4, "Level 2"));
                createAndShowGUI();
                for (Course item : course) {
                    System.out.println(item);
                }
            }
        });
    }

    class ComboBoxRenderer extends DefaultListCellRenderer {

        public Component getListCellRendererComponent(
                JList list,
                Object value,
                int index,
                boolean isSelected,
                boolean cellHasFocus) {

            if (value instanceof Course) {
                Course course = (Course) value;
                value = course.getCourseNum();
            }

            return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        }
    }

    public static class Course {

        private String courseNum, courseTitle, courseLevel;
        private int courseDur, courseFee;

        Course() {
        }

        Course(String courseNum, String courseTitle, int courseDur,
                int courseFee, String courseLevel) {
            this.courseNum = courseNum;
            this.courseTitle = courseTitle;
            this.courseDur = courseDur;
            this.courseFee = courseFee;
            this.courseLevel = courseLevel;
        }

        @Override
        public String toString() {
            String courseInfo = this.getCourseNum() + ", " + this.getCourseTitle() + ", "
                    + this.getCourseDur() + ", " + this.getCourseFee() + ", " + this.getCourseLevel();
            return courseInfo;
        }

        public String getCourseNum() {
            return this.courseNum;
        }

        public String getCourseTitle() {
            return this.courseTitle;
        }

        public int getCourseDur() {
            return this.courseDur;
        }

        public int getCourseFee() {
            return this.courseFee;
        }

        public String getCourseLevel() {
            return this.courseLevel;
        }

        public void setCourseNum(String courseNum) {
            this.courseNum = courseNum;
        }

        public void setCourseTitle(String courseTitle) {
            this.courseTitle = courseTitle;
        }

        public void setCourseDur(int courseDur) {
            this.courseDur = courseDur;
        }

        public void setCourseTitle(int courseFee) {
            this.courseFee = courseFee;
        }

        public void setCourseLevel(String courseLevel) {
            this.courseLevel = courseLevel;
        }
    }
}