I am t开发者_高级运维rying to add JComponents to JTable Cells. Do I Implement CellRenderer or CellEditor?
What you need is a custom editor which will return the JComboBox (or whatever component you want to use). You should check the Sun tutorial for JTable, it contains an example on how to use a JComboBox as an editor. If you want to use JComboBox as a renderer as well, the tutorial applies to that too.
You could also do it with the DefaultCellEditor by passing in an instance of a JComboBox (or JCheckBox or JTextField) to the constructor.
1- Create a JCombobox
and insert into it the information you want, like this:
JComboBox<String> sport = new JComboBox<String>();
sport.addItem("foot");
sport.addItem("hand bool");
sport.addItem("****");
2- Create a JTable
and set a Table Mode to this table, something like:
Vector<String> title = new Vector<String>
title.add("id");
title.add("sport");
Vector<Vector<String>> rows = new Vector<Vector<String>>();
rows.addItem("1");
rows.addItem("2");
JTable table = new JTable(rows, title);
3- You put the JComboBox in JTable Cells like this:
table.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(sport));
精彩评论