문제

I want to add combobox into a cell of JTable.

model=new DefaultTableModel(data,col);
JTableHeader head=new JTableHeader();
head.setBackground(Color.BLUE);
table=new JTable(model);
table.add(head);
JComboBox combo = new JComboBox();
combo.addItem("Names");
combo.addItem("Antony");
combo.addItem("Victor");
combo.addItem("Ramkumar");
table.add(combo);

But i cant get the combobox in the cell. Is it possible to set combo box?

도움이 되었습니까?

해결책

You need to set the TableCellEditor of the JTable. It's better to search the Java Tutorials, but here is a short explain.

JTable uses three main classes to work:

1) TableModel: it's function is to say how many rows and columns the table has and to serve the data of the Table, it's main methods are getValue(row,col) and setValue(value, row,col). And fire events to notify the JTable repaints.

2) TableCellRenderer: it's main purpose it's to draw components in the JTable's cells. This components are only painted: NOT WORK! if you draw a JComboBox it won't desplegate if you click on it or if you draw a JCheckbox it wont't select/unselect.

3) TableCellEditor: it's main purpose it's to draw a component within a JTableCell to edit the value of the cell. It receives events and decide when to start the editing, then it's getTableCellEditorComponent method is called to return the editor component. The component returned has to launch events so that the TableCellEditor knows when to stop the editing and get the value and use it to call the TableModel.setvalue... or cancel the editing.

So that to show JComboBox in a JTable you must create your own TableCellEditor, not an easy task if you haven't done it before.

다른 팁

Take alook at this Java tutorial and search in this page for "Using a Combo Box as an Editor"

시도 :

  1. 단계별 단계별 블록 정의를 목록 정의에서 제외하여 정확히 작동하지 않는 것을 찾아야합니다.
  2. 또 다른 recoMendation, 필드 정의의 이름 속성에서 갭을 사용하지 마십시오. _x2000과 같은 _x2000과 같은 것으로 변경 될 수 있습니다.
  3. 필드 ref의 ID, 이름 또는 showID * ...와 같은 필요한 속성 만 사용해보십시오. DisplayName과 같은 속성은 어쨌든 복사됩니다.
  4. 인터페이스를 통해 목록을 만들려고했고 작동하는 경우 SharePoint Manager를 통해 생성 된 목록 정의를 열고 차이점을 식별하십시오.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top