我正在使用这种JTable
方法创建一个以 JComboBox 作为其呈现外观的单元格。
public void addComboBoxToColumn(String[] options, int column_index){
ComboTableCellRenderer renderer = new ComboTableCellRenderer();
JComboBox<String> combo = new JComboBox<String>(options);
TableCellEditor combo_editor = new DefaultCellEditor(combo);
TableColumn column = getColumnModel().getColumn(column_index);
column.setCellRenderer(renderer);
column.setCellEditor(combo_editor);
}
...
public class ComboTableCellRenderer implements ListCellRenderer, TableCellRenderer
{
DefaultListCellRenderer listRenderer = new DefaultListCellRenderer();
DefaultTableCellRenderer tableRenderer = new DefaultTableCellRenderer();
private void configureRenderer(JLabel renderer, Object value)
{
if (value != null)
renderer.setText((String)value);
}
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
listRenderer = (DefaultListCellRenderer)listRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
configureRenderer(listRenderer, value);
return listRenderer;
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
tableRenderer = (DefaultTableCellRenderer)tableRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
configureRenderer(tableRenderer, value);
return tableRenderer;
}
}
我的问题是组合框始终是单元格的大小。我不要那个。是否有可能使组合框更大?组合框中的某些选项太大且半隐藏。