繁体   English   中英

JTable中的JComboBox

[英]JComboBox in JTable

我在JTable的第3和第4列中有一个JComboBox,但我不知道如何获取它的项目...问题不是获取项目的方法,而是演员表

JComboBox combo=(JComboBox) jTable1.getColumnModel().getColumn(3).getCellEditor();

你能帮我吗?

JComboBox包含在CellEditor 您必须检索包装的组件,例如在使用DefaultCellEditor

DefaultCellEditor editor = (DefaultCellEditor)table.getColumnModel().getColumn(3).getCellEditor();
JComboBox combo = (JComboBox)editor.getComponent();

阅读本教程,了解如何在JTable中使用JCombobox作为编辑器。
http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#combobox

尝试这样的事情:

 public void example(){  

      TableColumn tmpColum =table.getColumnModel().getColumn(1);
      String[] DATA = { "Data 1", "Data 2", "Data 3", "Data 4" };
      JComboBox comboBox = new JComboBox(DATA);

      DefaultCellEditor defaultCellEditor=new DefaultCellEditor(comboBox);
      tmpColum.setCellEditor(defaultCellEditor);
      tmpColum.setCellRenderer(new CheckBoxCellRenderer(comboBox));
      table.repaint();
   }


/**
   Custom class for adding elements in the JComboBox.
*/
class CheckBoxCellRenderer implements TableCellRenderer {
        JComboBox combo;
        public CheckBoxCellRenderer(JComboBox comboBox) {
            this.combo = new JComboBox();
            for (int i=0; i<comboBox.getItemCount(); i++){
                combo.addItem(comboBox.getItemAt(i));
            }
        }
        public Component getTableCellRendererComponent(JTable jtable, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            combo.setSelectedItem(value);
            return combo;
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM