我在TableCellRenderer
中使用了以下JTable
。当我点击JComboBox
时,我会看到一个值列表作为单个字符串(如:[Text1, Text2]
),但不是单独的项目。错误在哪里?
TableColumn columnComboBox = table.getColumnModel().getColumn(5);
columnComboBox.setCellRenderer(getRendererComboBox());
private TableCellRenderer getRendererComboBox() {
return new TableCellRenderer() {
private JComboBox<String> box = new JComboBox<String>();
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column)
{
box.removeAllItems();
for (String q : employees[row].getQualification())
box.addItem(q);
box.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
box.setForeground(isSelected ? table.getSelectionForeground() : table.getForeground());
return box;
}
};
}
答案 0 :(得分:0)
如果您想在表格中使用下拉字段,则可以像完成here一样进行操作。 然后,这将是您创建JComboBox的部分:
public void setUpSportColumn(JTable table,
TableColumn sportColumn) {
//Set up the editor for the sport cells.
JComboBox comboBox = new JComboBox();
comboBox.addItem("Snowboarding");
comboBox.addItem("Rowing");
comboBox.addItem("Knitting");
comboBox.addItem("Speed reading");
comboBox.addItem("Pool");
comboBox.addItem("None of the above");
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
//Set up tool tips for the sport cells.
DefaultTableCellRenderer renderer =
new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
sportColumn.setCellRenderer(renderer);
}
如果这不是您想要的,但只是当您将鼠标悬停在单元格上时,下拉单元格的内容以及列中的所有元素都很好,您只需在字符串中使用html-tags你传递给setToolTipText方法。 像这样:
//Set up tool tips for the sport cells.
DefaultTableCellRenderer renderer =
new DefaultTableCellRenderer();
String newLine = "<br>";
String toolTipText = "<html>Snowboarding"+newLine+"Rowing"+newLine+"Knitting"+newLine+"Speed reading"+newLine+"Pool"+newLine+"None of the above</html>";
renderer.setToolTipText(toolTipText);
sportColumn.setCellRenderer(renderer);
我在这里找到答案:Multi-line tooltips in Java?
答案 1 :(得分:0)
公共类平板电脑扩展了JFrame { / ** * * / private static final long serialVersionUID = 1L;
public tablet() {
JTable table = new JTable();
DefaultTableModel model = new DefaultTableModel(new Object[][] { { "item1", "item2" } }, new Object[] { "A", "B" });
table.setModel(model);
table.setPreferredScrollableViewportSize(new Dimension(400, 250));
table.setRowHeight(25);
JScrollPane scrollPane = new JScrollPane(table);
// These are the combobox values
String[] values = new String[] { "item1", "item2", "item3" };
// Set the combobox editor on the 1st visible column
//combo editor
JComboBox box = new JComboBox(values);
TableColumn column1 = table.getColumnModel().getColumn(0);
column1.setCellEditor(new DefaultCellEditor(box));
//combo renderer
column1.setCellRenderer(new MyComboBoxRenderer(values));
getContentPane().add(scrollPane);
}
class MyComboBoxRenderer extends JComboBox implements TableCellRenderer {
/**
*
*/
private static final long serialVersionUID = 1L;
public MyComboBoxRenderer(String[] items) {
super(items);
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if(hasFocus)
{
setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
}
else
{
setForeground(table.getForeground());
setBackground(table.getBackground());
}
// Select the current value
setSelectedItem(value);
return this;
}
}
public static void main(String args[]) {
tablet aTable = new tablet();
aTable.pack();
aTable.setVisible(true);
}
}