如何在JTable中对JComboBox的字符串进行排序?

时间:2015-02-28 23:13:40

标签: java sorting jtable jcombobox

我有以下问题:

我已经为我的JTable的第5列插入了JComboBox-Object的每一行。 一切都很好,直到我想用setAutoCreateRowSorter(true)对列进行rowort。在这种情况下,我得到以下异常:

ClassCastException: java.lang.String cannot be cast to javax.swing.JComboBox

以下是我用于JTable的类:

TableModel:

private class MyTableModel implements TableModel {

        @Override
        public void addTableModelListener(TableModelListener l) {
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {

            switch (columnIndex) {
            case 0:
                return String.class;
            case 1:
                return String.class;
            case 2:
                return String.class;
            case 3:
                return Number.class;
            case 4:
                return Boolean.class;
            case 5:
                return JComboBox.class;  // modifyed!

            default:
                return null;
            }

        }

        @Override
        public int getColumnCount() {
            return columnNames.length;
        }

        @Override
        public String getColumnName(int columnIndex) {
            return columnNames[columnIndex];
        }

        @Override
        public int getRowCount() {
            return data[0].length - 1;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            return data[rowIndex][columnIndex];

        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            switch (columnIndex) {
            case 5:
                return true;
            default:
                return false;
            }

        }

        @Override
        public void removeTableModelListener(TableModelListener l) {
        }

        @Override
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
            data[rowIndex][columnIndex] = aValue;

        }

    }

TableCellRenderer:

public class StringTableCellRenderer extends JLabel implements
        TableCellRenderer {

    private static final long serialVersionUID = 1L;

    public StringTableCellRenderer() {
        setOpaque(true);
        setLayout(new BorderLayout());
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        Font font = getFont().deriveFont(Font.BOLD, 15);

        if ((row % 2) == 0) {
            setBackground(new Color(240, 255, 255));
        } else {
            setBackground(new Color(191, 239, 255));
        }

        if (isSelected) {
            setBackground(new Color(0, 191, 255));
        }

        setHorizontalAlignment(JLabel.CENTER);
        setForeground(Color.BLACK);
        setFont(font);

        if (value instanceof JComboBox) {
            System.out.println("Renderer: "+column+" "+row+" "+((JComboBox<?>)value).getSelectedIndex());

            setText((String) ((JComboBox<?>) value).getSelectedItem());
        } else {
            setText(value.toString());
        }

        return this;
    }

}

TableCellEditor:

  public class MyTableCellEditor extends AbstractCellEditor implements
        TableCellEditor, ActionListener {

    private static final long serialVersionUID = 1L;
    private JComboBox<?>[] comboList = new JComboBox<?>[5];
    private int column = 0;

    @SuppressWarnings("unchecked")
    public MyTableCellEditor() {

        for (int i = 0; i < comboList.length; i++) {
            comboList[i] = new JComboBox<String>();
            setComboBox((JComboBox<String>) comboList[i]);
            comboList[i].setName("ComboBox_" + i);
        }

    }

    public void setComboBox(JComboBox<String> comboBox) {
        comboBox.addItem("Me");
        comboBox.addItem("You");
        comboBox.addItem("They");
        comboBox.addItem("Us");
        comboBox.addItem("We");
    }

    @Override
    public void actionPerformed(ActionEvent event) {
    }

    @Override
    public Object getCellEditorValue() {
        System.out.println("Editor: getCellEditorValue() "+comboList[column].getSelectedIndex());
        if (comboList[column].getSelectedIndex()==-1) {
            return (String) comboList[column].getItemAt(0);
        } else {
            return (String) comboList[column].getSelectedItem(); // returns String not JCombobox!
        }
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected, int row, int column) {

        String fieldValue = null;
        System.out.println("Editor: getTableCellEditorComponent");
        if (value instanceof JComboBox<?>) {
            fieldValue = (String) ((JComboBox<?>) value).getSelectedItem();
        }
        this.column = column - 1;
        comboList[column - 1].setSelectedItem(fieldValue);

        return comboList[column - 1]; // for each row of column 5 an own JComboBox object

    }

}

我自己解决了这个问题:

在TableModel中,第5列的getColumnClass() - 方法返回JCombobox.class - object。

在TableCellEditor中,第5列中的每一行一个自己的JComboBox - 对象getCellEditorValue() - 方法返回有关的getSelectedItem() JComboBox - 对象

因此Jtable确实有一个JComboBox - 对象(getValueAt() - 来自TableModel的第5列方法和getTableCellEditorComponent() - 来自TableCellEditor的方法)但是返回值仍然是String(getCellEditorValue) ()来自TableCellEditor和 getTableCellRendererComponent() - 来自TableCellRenderer的方法。)

希望这有助于任何有类似问题的人......

1 个答案:

答案 0 :(得分:1)

为什么要创建自定义Comparator? TableModel中的数据是String数据,而不是JCombobox。该表的RowSorter已经知道如何对String数据进行排序。

你绝不应该在TableModel中存储JComboBox。如果你实际上是在做这个,那就去除它。

然后摆脱你的自定义Comparator,它应该可以正常工作。

如果您尝试在不同的行中使用不同的组合框值,那么您应该尝试以下内容:how to add different cell editors for one column in JTable?