可以让JComboBox下拉出来的列表显示两列吗?

解决方案 »

  1.   

    那位高手能帮帮俺,jComboBox的构造器有一个是带model参数的,是不是这个可以进行设置?
    不会是需要我继承JComponent再自己写个控件吧?
      

  2.   

    import java.awt.*;import javax.swing.*;public class T {
    public static void main(String[] args) {
    try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
    e.printStackTrace();
    } JComboBox combobox = new JComboBox();
    combobox.addItem(new String[] {"Good", "Morning"});
    combobox.addItem(new String[] {"Good", "Afternoon"});
    combobox.addItem(new String[] {"Good", "Night"});

    combobox.setRenderer(new TwoColumnsRenderer()); JFrame f = new JFrame();
    f.getContentPane().add(combobox, BorderLayout.CENTER);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    }
    }class TwoColumnsRenderer extends JPanel implements ListCellRenderer {
    private DefaultListCellRenderer column1 = new DefaultListCellRenderer();
    private DefaultListCellRenderer column2 = new DefaultListCellRenderer();

    public TwoColumnsRenderer() {
    super(new GridLayout(1, 0, 1, 0));
    add(column1);
    add(column2);
    }

    public Component getListCellRendererComponent(
    JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    String array[] = (String[]) value;
    column1.getListCellRendererComponent(list, array[0], index, isSelected, cellHasFocus);
    column2.getListCellRendererComponent(list, array[1], index, isSelected, cellHasFocus); return this;
    }
    }