Определение выпадающего поля со списком в модели абстрактной таблицы в Java - PullRequest
1 голос
/ 29 декабря 2010

Может кто-нибудь сообщить мне, как определить выпадающее поле со списком как ячейку в строке в абстрактной табличной модели, новичок в Java, так что на самом деле не уверен, как это сделать.Это будет первая ячейка, как показано ниже. Например, поле со списком

Мой код выглядит следующим образом.

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)},
      }

Спасибо, Симон

Ответы [ 2 ]

2 голосов
/ 29 декабря 2010

Посмотрите на: 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] }
    };
0 голосов
/ 29 декабря 2010

Если вы используете 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
    }
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...