Put JTextComponent and JComboBox in JTable

I have a list and a list, and I need to create a JTable with these two columns. I am confused with the model, can anybofy show me how to do this, please, I am new to swing and Java?

+3
source share
2 answers

Please check my answer to another question . In those cases when I presented a simple table, it is often used.

In your case, you will create the data as follows:

//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);
+2
source

Don’t worry, just set the desired component as the cell editor for this column. Simple is not that.

Fragment Example

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

Then turn it on as below,

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

Further reading:

, Swing JTable.

+1

All Articles