Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在JavaSwing中为表中的选中组合框组件着色?_Java_Swing_Combobox - Fatal编程技术网

如何在JavaSwing中为表中的选中组合框组件着色?

如何在JavaSwing中为表中的选中组合框组件着色?,java,swing,combobox,Java,Swing,Combobox,我有一个组合框,里面有一个复选框,放在一个表中。每当我选择行时,颜色渲染仅应用于表的其余单元格。如何呈现“checkedcombobox”,以便它也可以获得与表行中其他单元格的选择颜色匹配 以下是一个示例: import javax.swing.DefaultCellEditor; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.

我有一个组合框,里面有一个复选框,放在一个表中。每当我选择行时,颜色渲染仅应用于表的其余单元格。如何呈现“checkedcombobox”,以便它也可以获得与表行中其他单元格的选择颜色匹配


以下是一个示例:

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.plaf.basic.BasicComboBoxUI;

public class TableEditorExample {

    private static final String[] COLS = {"Number", "Type", "Name"};

    private static final Object[][] DATA = {{"1", "Book", "Brave new world"}, {"2", "Music", "Smells like a teen spirit"},
            {"3", "Film", "Star Wars"}};

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            // Nothing
        }
        JFrame frm = new JFrame("Test combo renderer");
        JTable tbl = new JTable(DATA, COLS);
        tbl.setRowHeight(20);
        JComboBox<String> combo = new JComboBox<>(new String[] {"Book", "Music", "Film"});
        combo.setUI(new BasicComboBoxUI());
        combo.setBackground(tbl.getSelectionBackground());
        combo.setForeground(tbl.getSelectionForeground());
        tbl.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(combo));
        frm.add(new JScrollPane(tbl));
        frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frm.pack();
        frm.setLocationRelativeTo(null);
        frm.setVisible(true);
    }
}
以下是相应程序(适用于Windows)的屏幕截图:

您好,如果我只有组合框,就可以完美地渲染它。我在这里面临的问题是,我在组合框中实现了复选框,并且还添加了标签,以显示在其顶部选择了多少复选框值。@GouruNagaraju Hi。请提供一个可运行的代码示例,这样我也可以重现和调试您的问题。
combo.setUI(new BasicComboBoxUI());
combo.setBackground(tbl.getSelectionBackground());
combo.setForeground(tbl.getSelectionForeground());