开发者

Defining a drop down combo box within an abstract table model in java

Can somebody please inform me as to how to define a drop down combo box as a cell within a row in an abstract table model, new to Java so really not sure how to do this. This will be be the first Cell as shown below E.g Combo Box

My code is as follows.

public static Object[][] data
  开发者_Python百科      = {
           {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)},
      }

Thanks Simon


Have a look at : 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] }
    };


Assuming you're using 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
    }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜