将JComboBox添加到JTable单元。所选项目不会停留

时间:2015-02-24 21:50:26

标签: java swing jtable jcombobox selecteditem

我在这里查看了一些帖子,但无法找到答案。

我已成功将JComboBox添加到Jtable单元格。但是,所选项目并未被记住"。

更具体地说:

1)从A行的组合框中选择一个项目 Step 1

2)转到选择行B的组合框,但行A中显示的项目消失(任何时候焦点丢失,行A组合框选择消失) Step 2

3)回到A行,记住选择。 Step 3

要明确信息丢失。虽然组合框无法直观显示所选设备,但会保存选择。但是,我希望用户能够直观地看到他们刚刚选择的内容,而无需再次选择该行。

这让我相信我使用过的自定义渲染器一定有错误

以下是相关代码。

TableModel (请注意,Level2Area等同于设备):

public class PreDefJobPlanDialogDataTable extends CygnusAbstractTableModel{

 private static final long serialVersionUID = 1344977933386754731L;

 static final ColumnData columns[] = {
    new ColumnData( "Sel." , 25, 10000, JLabel.LEFT),
    new ColumnData( "Type", 25, 10000, JLabel.LEFT),
    new ColumnData( "Status", 25,  10000,JLabel.LEFT ),
    new ColumnData( "Title", 100,  10000,JLabel.LEFT ),
    new ColumnData( "Equipment Class", 50, 10000, JLabel.LEFT),
    new ColumnData( "Equipment", 100, 10000, JLabel.LEFT)
};

...

public boolean isCellEditable(int rowIndex, int columnIndex) {
    boolean ret = false;
    if( columnIndex == 0 || columnIndex == 5 ) {
        ret = true;             
    }
    return ret;
}

...

public Object getValueAt(int rowIndex, int columnIndex) {
    Object ret = " ";

    if( rowIndex >= 0 && rowIndex < getRowCount() ) {
        PreDefJobPlan obj = (PreDefJobPlan)modelData.get( rowIndex );
        switch( columnIndex ) {

            case 0: ret = obj.getGroupFlag();
            break;
            case 1: ret = obj.getType();
            break;
            case 2: ret = obj.getStatus();
            break;
            case 3: ret = obj.getTitle();
            break;
            case 4: ret = obj.getLevel1Area();
            break;
            case 5: ret = obj.getLevel2Area();
            break;
            case 9: ret = obj.getPreDefJobPlanID();
            break;
            default: ret = " ";
        }
    }
    return ret;
}

...

public void setValueAt( Object obj, int row, int col ) {
    // get the object
    PreDefJobPlan o = (PreDefJobPlan) this.modelData.get( row );
    switch( col ) {
        case 0:
            o.setGroupFlag((Boolean) obj);
        break;
        case 5:
            o.setLevel2Area((Equipment) obj);
        break;
        default: ;
    }
}

创建表格

protected void createTable() {
    PreDefJobPlanDialogDataTable dataModel = new PreDefJobPlanDialogDataTable();
    dataModel.setTimeOffset( 0 );

    this.view.getOrdersTable().setHighlighters(HighlighterFactory.createAlternateStriping());
    this.view.getOrdersTable().setAutoCreateColumnsFromModel( false );
    this.view.getOrdersTable().setModel( dataModel );
    this.view.getOrdersTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    this.view.getOrdersTable().setTransferHandler(new TableRowTransferHandler(this.view.getOrdersTable())); 

  for( int i = 0; i < dataModel.getColumnCount(); i++ ) {   
            if( i== 0 ) {
                ...
            }
            else if (i == 5){

                TableCellRenderer renderer = new ComboCellRenderer(); 
                ca.cygnusconsulting.utilities.ColumnData col = dataModel.getColumn( i );
                JComboBox tableCheckBox = new JComboBox();
                tableCheckBox.setModel( lvl1Model );
                tableCheckBox.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
                tableCheckBox.setBackground(this.view.getOrdersTable().getBackground());
                TableCellEditor editor = new ComboBoxCellEditor(tableCheckBox);
                TableColumnExt column = new TableColumnExt( i, col.getWidth(), renderer, editor );
                column.setMaxWidth(col.getMaxWidth());
                this.view.getOrdersTable().addColumn( column ); 
            }
            else{
                ...
            }
        } 

渲染器:

public class ComboCellRenderer extends JComboBox implements TableCellRenderer {

protected static Border noFocusBorder = new EmptyBorder(1,1,1,1);
protected static Border focusBorder = UIManager.getBorder("Table.focusCellHighlightBorder");

 public ComboCellRenderer() {
  super();
  setOpaque(true);

 }

  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
  boolean hasFocus, int row, int column) {

    setBackground(isSelected && !hasFocus ? table.getSelectionBackground() : table.getBackground());
    setForeground(isSelected && !hasFocus ? table.getSelectionForeground() : table.getForeground());
    setFont(table.getFont());
    setSelectedItem(value);

  return this;
  }


}

有没有人有任何合理的建议或对此有任何见解?

1 个答案:

答案 0 :(得分:1)

通常您应该使用默认的单元格渲染器来显示您的单元格内容,并仅在编辑时显示组合框。

使用JTable单元格渲染器理解的关键概念是a single cell renderer is generally used to draw all of the cells that contain the same type of data

如果您特别希望使用组合框来渲染单元格,则单元格为空的原因是自定义渲染器组合框没有添加项目。您应该使用与单元格编辑器组合框相同的值填充单元格渲染器组合框。