简体   繁体   English

将JComboBox添加到JTable单元中。 所选项目未停留

[英]Adding a JComboBox to a JTable cell. Selected item doesn't stay

I looked through a few posts here but couldn't find an answer to this. 我在这里浏览了几篇文章,但找不到答案。

I have successfully added a JComboBox to a Jtable cell. 我已成功将JComboBox添加到Jtable单元中。 However the selected item is not being "remembered". 但是,所选项目没有被“记住”。

More specifically: 进一步来说:

1) Select an item from a combo box of row A 1)从A行的组合框中选择一个项目 第1步

2) Go to select a combo box of row B, but the displayed item in row A disappears (anytime the focus is lost, the row A combo box selection disappears) 2)转到选择行B的组合框,但行A中显示的项目消失(只要失去焦点,行A组合框的选择就会消失) 第2步

3) Go back to row A, the choice is remembered. 3)返回A行,该选择被记住。 第三步

Just to be clear the information is not being lost. 只是为了清楚起见,信息并没有丢失。 Although the combo box does not visually show what equipment was chosen, the selection is saved. 尽管组合框无法直观显示选择了什么设备,但是选择已保存。 However, I would like users to be able to visually see what they just selected without having to select the row again. 但是,我希望用户能够直观地看到他们刚刚选择的内容,而不必再次选择该行。

This leads me to believe there must be an error with the custom renderer I have used 这使我相信我使用的自定义渲染器一定存在错误

Here is the following relevant code. 这是以下相关代码。

TableModel (Please note that Level2Area is equivalent to Equipment): TableModel (请注意,Level2Area等效于Equipment):

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: ;
    }
}

Creating Table: 创建表:

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{
                ...
            }
        } 

Renderer: 渲染器:

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;
  }


}

Does anyone have any reasonable suggestions, or any insight into this? 是否有人对此有任何合理的建议或见解?

Normally you should use the default cell renderer to display your cell contents and only show the combobox when editting. 通常,您应该使用默认的单元格渲染器来显示单元格内容,并且仅在编辑时显示组合框。

A key concept to understand with JTable cell renderers is that a single cell renderer is generally used to draw all of the cells that contain the same type of data . 使用JTable单元格渲染器要理解的一个关键概念是, 通常使用单个单元格渲染器来绘制包含相同类型数据的所有单元格

If you particularly want a combobox to be used for rendering cells, the reason that your cells are blank is that the custom renderer combobox does not have the items added to it. 如果您特别希望使用组合框来渲染单元格,则单元格为空白的原因是自定义渲染器组合框没有添加项目。 You should populate the cell renderer combobox with the same values as your cell editor combobox. 您应该使用与单元格编辑器组合框相同的值填充单元格渲染器组合框。

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

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