JTable is empty after adding Components
I have problem with JTable, I am trying to put in each row JComboBoxes and JTextFields but when I load app, my table is empty. Functions getCmb* and getTxt* return JComboBox and JTextField and that works fine, I checked.
JTable tblCommands;
String[] columnTitles=new String[]{"Command","Offset","Type","Value","Units","Value Type","R/W"};
Object[][] data=new Object[20][7];
int row=0;
for(MessageCSVView message:messageContainer.getRows()){
data[row][0]=message.getCmbName();//this works
data[row][1]=message.getCmbOffset();//this works
data[row][2]=message.getTxtType();//this works
data[row][3]=message.getTxtValue();//this works
data[row][4]=message.getTxtUnit();//this works
data[row][5]=message开发者_StackOverflow.getTxtValueType();//this works
data[row][6]=message.getCmbRW();//this works
row++;
}
tblCommands=new JTable(data,columnTitles);
Can anybody tell me what I did wrong ?
You misunderstand the way input works with JTable
.
You'll need to create TableCellEditor
implementations and add them to each column of your table.
Take a look at Swing tutorial on JTable
for further information.
Example Snippet
public class JComboBoxCellEditor extends DefaultCellEditor {
JComboBox comboBox;
public JComboBoxCellEditor() {
super(new JComboBox());
comboBox = (JComboBox) getComponent();
}
}
Then include it like below,
TableColumn column = myTable.getColumnModel().getColumn(0);
column.setCellEditor(new JComboBoxCellEditor());
Further reading:
Here is your best bet, Swing tutorial for JTable.
精彩评论