开发者

Put JTextComponent and JComboBox in JTable

I have List and List and I need to create JTable with theese two columns. I am confused with mo开发者_开发技巧del, can anybofy show me how to do that please, I am new to swing and Java ?


Please check out my answer to some other question. Where I have presented a simple table model often use.

In your case you would create data in a following way:

//I assumed here list 1 and 2 have the same sizes
List<Object> list1 = getList1();
List<Object> list2 = getList2();
int rNo = list1.size();
List<List<Object>> data = new ArrayList<List<Object>>(rNo);
int cNo = 2;
for(int i = 0; i < rNo; i++)
{
     List<Object> r = new ArrayList<Object>(cNo);
     r.add(list1.get(i));
     r.add(list2.get(i));
     data.add(r);
}
tm.setData(data);


No worries, just set your desired component as a cell editor for that column. Simple ain't it.

Example Snippet

public class JTextFieldCellEditor extends DefaultCellEditor {    
    JTextField textField;    
    public JTextFieldCellEditor() {
        super(new JTextField());
        textField = (JTextField) getComponent();   
    }
}

Then include it like below,

TableColumn column = myTable.getColumnModel().getColumn(0);
column.setCellEditor(new JTextFieldCellEditor());

Further reading:

Here is your best bet, Swing tutorial for JTable.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜