7

I am trying to add JComponents to JTable Cells. Do I Implement CellRenderer or CellEditor?

Valentin Rocher
  • 11,667
  • 45
  • 59

3 Answers3

7

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.

Carlos
  • 2,503
  • 26
  • 29
  • 1
    yep, just keep in mind that renderer is for display and editor is for when editing a cell. You may only need to implement Editor :) – Arthur Thomas Jan 13 '10 at 18:40
1

You could also do it with the DefaultCellEditor by passing in an instance of a JComboBox (or JCheckBox or JTextField) to the constructor.

willcodejavaforfood
  • 43,223
  • 17
  • 81
  • 111
0

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));
Walid Bousseta
  • 1,329
  • 2
  • 18
  • 33