Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Make a combobox appear in just one cell in a JTable column

843806Nov 14 2008 — edited Dec 5 2008
I am trying to make a combobox appear in just one row in a column.

The lines
TableColumn tableColumn = table.getColumnModel().getColumn(1);
tableColumn.setCellEditor(new DefaultCellEditor(comboBox));
...make the combobox to appear in all cells in the specified column.

I have also tried
table.getModel().setValueAt(new DefaultCellEditor(comboBox),1,1);
...but it doesn´t make the comboxo appear in the specified row.

How do I get the combobox to appear in just one row in a column?

The whole code is as follows:
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumn;

import javax.swing.table.DefaultTableModel;
import java.awt.GridLayout;

class TableCell extends JFrame
{
	JComboBox comboBox = new JComboBox();
	DefaultTableModel model = new DefaultTableModel();
	JTable table;



	TableCell()
	{
		comboBox.addItem("Tennis");
		comboBox.addItem("Soccer");
		comboBox.addItem("Baseball");
		comboBox.addItem("Basketball");

		model.addColumn("First Name");
    	model.addColumn("Last Name");


		String[] row1 = { "Thomas", "Aquinas", "1225-1274" };
		String[] row2 = { "Mother", "Teresa", "1225-1274" };
   		model.addRow(row1);
   		model.addRow(row2);
		table = new JTable(model);
		TableColumn tableColumn = table.getColumnModel().getColumn(1);

		add(new JScrollPane(table));

		tableColumn.setCellEditor(new DefaultCellEditor(comboBox));

		//DefaultCellEditor dce = new DefaultCellEditor(comboBox);

		table.getModel().setValueAt(new DefaultCellEditor(comboBox),1,1);

		//Display the window.
		setSize(200,200);
        setVisible(true);
	}

	public static void main(String[] args)
	{
		new TableCell();
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 2 2009
Added on Nov 14 2008
17 comments
1,015 views