I have a JComboBox renderer (and cell editor) inside of a JTable cell.
我在JTable单元格内部有一个JComboBox渲染器(和单元编辑器)。
My JComboBox has two items ("a" and "b")
我的JComboBox有两个项目(“a”和“b”)
I would like to set the JComboBox selected index to 1 (corresponding to "b").
我想将JComboBox选择的索引设置为1(对应于“b”)。
I've tried unsuccessfully to get the renderer component, and calling "setSelectedIndex(1")
我尝试获取渲染器组件并调用“setSelectedIndex(1”)失败了
2 个解决方案
#1
0
When you work with render, it can be right to work with JComboBox model:
使用render时,使用JComboBox模型是正确的:
String[] vls = new String[]{"a","b"};
JComboBox<String> comboBox1;
...
comboBox1.setModel(new DefaultComboBoxModel(vls));
You can use, when you work with ComboBox model:
使用ComboBox模型时可以使用:
comboBox1.getModel().setSelectedItem("b");
or, when you work without ComboBox model:
或者,当您在没有ComboBox模型的情况下工作时:
comboBox1.setSelectedIndex(1);
#2
0
I would like to set the JComboBox selected index to 1 (corresponding to "b").
我想将JComboBox选择的索引设置为1(对应于“b”)。
You don't set the index.
您没有设置索引。
The editor is shared by all rows in the table. The combo box item is selected when the cell starts editing. This is done automatically.
编辑器由表中的所有行共享。单元格开始编辑时,将选择组合框项目。这是自动完成的。
So all you need to do is add the proper data to the TableModel.
所以你需要做的就是将适当的数据添加到TableModel中。
So in your case the value "b" needs to be added to the TableModel for the row when you create the TableModel.
因此,在您创建TableModel时,需要将值“b”添加到行的TableModel中。
Edit:
编辑:
my JComboBox is a renderer within a JTable.
我的JComboBox是JTable中的渲染器。
The custom renderer would look something like:
自定义渲染器看起来像:
class ComboBoxRenderer extends JComboBox implements TableCellRenderer
{
public ComboBoxRenderer()
{
setBorder(null);
}
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
removeAllItems();
addItem( value );
return this;
}
}
#1
0
When you work with render, it can be right to work with JComboBox model:
使用render时,使用JComboBox模型是正确的:
String[] vls = new String[]{"a","b"};
JComboBox<String> comboBox1;
...
comboBox1.setModel(new DefaultComboBoxModel(vls));
You can use, when you work with ComboBox model:
使用ComboBox模型时可以使用:
comboBox1.getModel().setSelectedItem("b");
or, when you work without ComboBox model:
或者,当您在没有ComboBox模型的情况下工作时:
comboBox1.setSelectedIndex(1);
#2
0
I would like to set the JComboBox selected index to 1 (corresponding to "b").
我想将JComboBox选择的索引设置为1(对应于“b”)。
You don't set the index.
您没有设置索引。
The editor is shared by all rows in the table. The combo box item is selected when the cell starts editing. This is done automatically.
编辑器由表中的所有行共享。单元格开始编辑时,将选择组合框项目。这是自动完成的。
So all you need to do is add the proper data to the TableModel.
所以你需要做的就是将适当的数据添加到TableModel中。
So in your case the value "b" needs to be added to the TableModel for the row when you create the TableModel.
因此,在您创建TableModel时,需要将值“b”添加到行的TableModel中。
Edit:
编辑:
my JComboBox is a renderer within a JTable.
我的JComboBox是JTable中的渲染器。
The custom renderer would look something like:
自定义渲染器看起来像:
class ComboBoxRenderer extends JComboBox implements TableCellRenderer
{
public ComboBoxRenderer()
{
setBorder(null);
}
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
removeAllItems();
addItem( value );
return this;
}
}