开发者

How to add a JComboBox to a JTable cell?

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));
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜