Jcomboboxes in jtable select the same value..

6

6e

Hi,

Ive created a JTable that has one column that is full of JCombobox
objects.

The weird thing is that when I select one value in.. say row 3.., that
value is used not only in row 3, but also row 4,5, and 6 and all the
way to the bottom-1 of the table.

I created a custom celleditor and cellrenderer, code I included below
for your perusal...

--------main app code-------------
String[] values = new String[]{" ", "Heading", "Body"};

table.getColumnModel().getColumn(table.convertColumnIndexToView(iFORMAT)).setCellEditor(new
ComboBoxEditor(values));

table.getColumnModel().getColumn(table.convertColumnIndexToView(iFORMAT)).setCellRenderer(new
ComboBoxRenderer(values));



------------combobox editor---------------
public class ComboBoxEditor extends DefaultCellEditor {
public ComboBoxEditor(String[] items) {
super(new JComboBox(items));
}
}


--------------Combobox renderer----------
ublic class ComboBoxRenderer extends JComboBox implements
TableCellRenderer {
public ComboBoxRenderer(String[] items) {
super(items);
}

public Component getTableCellRendererComponent(JTable table, Object
value,
boolean isSelected, boolean hasFocus, int row, int column)
{
if (isSelected) {
setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}

// Select the current value
setSelectedItem(value);
return this;
}
}
 
J

jan V

The weird thing is that when I select one value in.. say row 3.., that
value is used not only in row 3, but also row 4,5, and 6 and all the
way to the bottom-1 of the table.

Sounds like your combo boxes are sharing underlying data models...
I created a custom celleditor and cellrenderer, code I included below
for your perusal...

--------main app code-------------
String[] values = new String[]{" ", "Heading", "Body"};

table.getColumnModel().getColumn(table.convertColumnIndexToView(iFORMAT)).se
tCellEditor(new
ComboBoxEditor(values));
table.getColumnModel().getColumn(table.convertColumnIndexToView(iFORMAT)).se
tCellRenderer(new
ComboBoxRenderer(values));

You may get happier results if your renderers don't share the values
array...
 
6

6e

I appreciate the idea, I explored it previously by
new ComboBoxEditor( new String[]{" ", "Heading", "Body"}; ... but it
didn't seem to work...

unless I just don't understand what you're trying to tell me... If
this is the case, please explain further.. or let me know if you have
any other ideas?..

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,928
Messages
2,570,068
Members
46,513
Latest member
JacklynMcC

Latest Threads

Top