JTable单元内的JComboBox

时间:2011-10-26 11:48:58

标签: java swing jtable jcombobox

我无法将javax.swing.JComboBox放入org.jdesktop.swingx.JXTable单元格中。我浏览了之前关于这个问题的问题,但没有帮助。

我试过了:

colModel.getColumnExt(HDTableAdapter.STATUS).setCellEditor(editorCB);  
colModel.getColumnExt(HDTableAdapter.STATUS).setCellRenderer(rendererCB);

class comboBoxRenderer extends JComboBox implements TableCellRenderer {

    public comboBoxRenderer(String[] items) {
        super(items);
    }

    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        if (isSelected) {
            setForeground(table.getSelectionForeground());
            super.setBackground(table.getSelectionBackground());
        } else {
            setForeground(table.getForeground());
            setBackground(table.getBackground());
        }

        setSelectedItem(value);
        return this;
    }
}

class comboBoxEditor extends DefaultCellEditor {
    public comboBoxEditor(String[] items) {
        super(new JComboBox(items));
    }
}

但是在这种情况下,我在单元格内部得到了一个不可编辑的ComboBox。

2 个答案:

答案 0 :(得分:1)

您的ComboxEditor看起来不对..请改用:

class ComboBoxEditor extends DefaultCellEditor {

    public ComboBoxEditor(String[] items) {
    super(new JComboBox(items));
    JCombobxToolTipRenderer renderer = new JCombobxToolTipRenderer();
    ((JComboBox)editorComponent).setRenderer(renderer);
}

public ComboBoxEditor(ComboBoxModel aModel) {
    super(new JComboBox(aModel));
    JCombobxToolTipRenderer renderer = new JCombobxToolTipRenderer();
    ((JComboBox)editorComponent).setRenderer(renderer);
}
}

class JCombobxToolTipRenderer extends DefaultListCellRenderer {

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

    JComponent comp = (JComponent) super.getListCellRendererComponent(list,
            value, index, isSelected, cellHasFocus);

    if (-1 < index && null != value) {
        list.setToolTipText((String) value);
    }
    return comp;
}
}

答案 1 :(得分:0)

试试这个,

class FinCellEditor extends AbstractCellEditor implements TableCellEditor{

    Component component;

    private JTextField text;
    private JComboBox combo;
    private int rowIndex;

       public FinCellEditor(){
                text = new JTextField();
              combo =new JComboBox();
       }

    @Override
        public Object getCellEditorValue() {
               Object select = null;
               if(rowIndex==9||rowIndex==10){
               select = combo.getSelectedItem(); 
               return select;
               }else{    
               if(select!=null){
                  text.selectAll();
               } 
               select = text.getText();
               return select;
               }
        }
    @Override
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
              if (row == 7) {      
                  date.setEnabled(true);
                  rowIndex=row;
                  return date;
              }else if (row == 9) {
                 combo.removeAllItems();
                 combo.addItem("Male");
                 combo.addItem("Female");
                 rowIndex=row;
                 return combo;
            }else if (row == 10) {
                 combo.removeAllItems();
                 combo.addItem("Married");
                 combo.addItem("UnMarried");
                 rowIndex=row;
                 return combo;

           } else {
                text.setEnabled(true);
                rowIndex=row;
                return text;
           }
       }