简体   繁体   English

将JTextComponent和JComboBox放在JTable中

[英]Put JTextComponent and JComboBox in JTable

I have List and List and I need to create JTable with theese two columns. 我有List和List,我需要用the​​ese两列创建JTable。 I am confused with model, can anybofy show me how to do that please, I am new to swing and Java ? 我对模型很困惑,可以任意告诉我该怎么做,我是摇摆和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 . 这是你最好的选择, JTable的Swing教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM