我正在尝试构建一个包含 JComboBoxes 作为渲染器和编辑器组件的表。这大部分工作正常,但是有两件事我似乎无法解决。
- 单元格之间的选项卡应该使 JComboBox 处于活动状态
- 单击下拉箭头应立即打开选项列表
关于 1,可编辑组合应将焦点放在嵌入的文本字段内,固定组合应允许向下箭头打开选项列表。
关于 2,我发现这有时取决于当前处于活动状态的其他单元格,但其他时候我必须双击。我无法使这种行为保持一致。
为方便起见,我包含了一个清晰的示例,它(我相信)使用推荐的方法在 Jtables 中嵌入 JComboBoxes。
感谢您的建设性建议。
import java.awt.Component;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableCombos
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Frame");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
AbstractTableModel model = new DefaultTableModel()
{
String[] columnHeaders = new String[]{"label", "combo-edit", "combo-fixed"};
Class<?>[] columnClasses = new Class<?>[]{String.class, Double.class, Double.class};
List<Object[]> data = new ArrayList<>();
{
data.add(new Object[]{"row 1", 1.0d, 2.0d});
data.add(new Object[]{"row 2", 2.0d, 3.0d});
}
@Override
public int getColumnCount()
{
return columnHeaders.length;
}
@Override
public boolean isCellEditable(int row, int column)
{
return column != 0;
}
@Override
public int getRowCount()
{
if (data == null) // race condition
return 0;
return data.size();
}
@Override
public Object getValueAt(int row, int column)
{
return data.get(row)[column];
}
@Override
public void setValueAt(Object aValue, int row, int column)
{
data.get(row)[column] = aValue;
}
@Override
public Class<?> getColumnClass(int column)
{
return columnClasses[column];
}
@Override
public String getColumnName(int column)
{
return columnHeaders[column];
}
};
JTable table = new JTable(model);
table.setSurrendersFocusOnKeystroke(true);
TableColumn c1 = table.getColumnModel().getColumn(1);
TableColumn c2 = table.getColumnModel().getColumn(2);
JComboBox<Double> editorComboEditable = new JComboBox<>(new Double[]{1.0d, 2.0d, 3.0d});
editorComboEditable.setEditable(true);
c1.setCellEditor(new DefaultCellEditor(editorComboEditable));
c2.setCellEditor(new DefaultCellEditor(new JComboBox<>(new Double[]{1.0d, 2.0d, 3.0d})));
final JComboBox<Double> rendererComboEditable = new JComboBox<>(new Double[]{1.0d, 2.0d, 3.0d});
rendererComboEditable.setEditable(true);
final JComboBox<Double> rendererComboFixed = new JComboBox<>(new Double[]{1.0d, 2.0d, 3.0d});
c1.setCellRenderer(new TableCellRenderer()
{
@Override
public Component getTableCellRendererComponent(JTable t, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
rendererComboEditable.setSelectedItem(value);
return rendererComboEditable;
}
});
c2.setCellRenderer(new TableCellRenderer()
{
@Override
public Component getTableCellRendererComponent(JTable t, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
rendererComboFixed.setSelectedItem(value);
return rendererComboFixed;
}
});
frame.getContentPane().add(new JScrollPane(table));
frame.pack();
frame.setVisible(true);
}
}