在java中的抽象表模型中定义下拉组合框

时间:2010-12-29 18:13:17

标签: java swing

有人可以告诉我如何将下拉组合框定义为其中的单元格 抽象表模型中的一行,是Java新手,所以真的不知道如何做到这一点。这将 成为第一个Cell,如下所示:E.g Combo Box

我的代码如下。

public static Object[][] data
        = {
           {Combo Box, new Double(5), new Double(5)},
           {Combo Box,new Double(5), new Double(5)},
           {Combo Box, new Double(5), new Double(5)},
           {Combo Box, new Double(5), new Double(5)},
      }

谢谢Simon

2 个答案:

答案 0 :(得分:2)

查看:http://www.java2s.com/Code/Java/Swing-Components/ComboBoxTable.htm

protected Object[][] data = new Object[][] {
      { "Core Java Volume 1", validStates[0] },
      { "Core Java Volume 2", validStates[0] },
      { "Core Web Programming", validStates[0] },
      { "Core Visual Basic 5", validStates[0] },
      { "Core Java Foundation Classes", validStates[0] }
    };

答案 1 :(得分:0)

假设您正在使用SWT。

Table table = new Table(parent, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
TableViewer tableViewer = new TableViewer(table);

// these properties are used to denote a column in the cell modifier
tableViewer.setColumnProperties(new String[] { "column1", "column2" });

tableViewer.setContentProvider(new IStructuredContentProvider(){
    public Object[] getElements(Object input) {
        // return an array of objects that represent row data. 
    }
    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        // update with modified value
    }
});

tableViewer.setCellEditors(new CellEditor[] { 
    new ComboBoxCellEditor(table, new String[]{"label 1", "label 2"}),
    new TextCellEditor(table), 
    new TextCellEditor(table) 
});

tableViewer.setCellModifier(new ICellModifier() {
    public boolean canModify(Object pDataElement, String property) {
        return true;
    }
    public Object getValue(Object pRowElement, String property) {
        // return value from row element that corresponds to the column property
        // column1... etc..
        // confusingly, if the column is a combo box, 
        // the returned value needs to be the index of the item selected, 
        // not the value  
    }
    public void modify(Object pDataElement, String property, Object value) {
        // similar to get value, this will give new values to update the model.  
        // if the cell is a combo box editor, value will be an index, not the 
        // selected label
    }
});