如果相关列使用组合作为编辑器,我需要使用JTable
在JComboBox
中呈现选定/聚焦的单元格。这样做的目的是向用户提示,使用组合而不是JTextField
编辑列中的单元格。
我的问题是,对于其他LookAndFeels
而不是Metal,无法将表格的选择背景设置为组合框。如果单击或使用键盘导航到“组合列”,则选定的单元格将由具有默认背景的组合进行渲染。
Metal L& F按照我的意愿工作,但这不是一个选择,因为我们的客户使用Substance / Nimbus / Windows L& F并且它不适用于任何一个。
希望我很清楚,任何帮助都表示赞赏!
在此SSCCE的“组合列”中选择单元格以查看我的意思:
import java.awt.Component;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
public class RenderSelectedCellWithComboSSCCE extends JFrame
{
RenderSelectedCellWithComboSSCCE()
{
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
add(new JScrollPane(createTable()));
pack();
}
private JTable createTable()
{
TableModel model = new DefaultTableModel(
new Object [][] {{"A", "Item 0"},
{"B", "Item 1"},
{"C", "Item 2"},
{"D", "Item 3"},
{"E", "Item 4"}},
new String [] {"TextField", "Combo"});
JTable table = new JTable(model);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
DefaultCellEditor editor = new DefaultCellEditor(new JComboBox(new Object[]{"Item 0", "Item 1", "Item 2", "Item 3", "Item 4"}));
editor.setClickCountToStart(2);
table.getColumnModel().getColumn(1).setCellEditor(editor);
table.getColumnModel().getColumn(1).setCellRenderer(new ComboCellRenderer());
return table;
}
/**
* Renderer that demonstrates problem rendering selected cells with a combo with selection background.
*/
private static class ComboCellRenderer implements TableCellRenderer
{
final TableCellRenderer defaultRenderer;
final JComboBox combo;
ComboCellRenderer()
{
defaultRenderer = new DefaultTableCellRenderer();
combo = new JComboBox();
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
if (hasFocus && table != null && table.isCellEditable(row, column))
{
combo.setModel(new DefaultComboBoxModel(new Object[]{value}));
combo.setSelectedItem(value);
combo.setBackground(table.getSelectionBackground()); // This only have desired effect for Metal L&F
return combo;
}
else
{
return defaultRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
}
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
setLookAndFeel();
new RenderSelectedCellWithComboSSCCE().setVisible(true);
}
private void setLookAndFeel()
{
try
{
//UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); // Metal works fine
UIManager.setLookAndFeel(new NimbusLookAndFeel());
//UIManager.setLookAndFeel(new WindowsLookAndFeel());
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//UIManager.setLookAndFeel("org.jvnet.substance.SubstanceLookAndFeel");
}
catch (Exception e)
{
throw new RuntimeException("Failed to set LookAndFeel", e);
}
}
});
}
}
答案 0 :(得分:3)
ComboBox渲染确实会带来问题,通常LAF对于他们想要如何显示组合本身有很特别的想法,许多都是风格化的,有圆角......它对所有人来说都是不可能的。在玩Stani的建议的同时,我遇到了所有令人讨厌的(并且很快被遗忘的:-)细节,比如
另一方面,你不需要一个功能齐全的JComboBox,你需要的只是一个可以打开的东西的视觉线索。因此,您可能会使用自定义组件 - 最简单的带有箭头的JLabel - 嘲笑JComboBox,类似于:
public static class MockCombo extends JLabel {
private JButton arrow;
public MockCombo() {
JComboBox box = new JComboBox();
box.setEditable(true);
arrow = (JButton) box.getComponent(0);
setLayout(new BorderLayout());
add(arrow, BorderLayout.LINE_END);
setOpaque(true);
}
}
还需要由LAF进行微调,但比调整JComboBox要容易得多的任务
答案 1 :(得分:2)
您可以指定组合渲染器并在那里使用所需的颜色。渲染组件的渲染器类型。