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;
}
}
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;
}
}