gpt4 book ai didi

java - 如何在 Java Swing 中对表格中选中的组合框组件进行颜色渲染?

转载 作者:行者123 更新时间:2023-11-30 06:20:59 25 4
gpt4 key购买 nike

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

enter image description here

最佳答案

这里有一个例子:

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);
}
}

所以,你需要做的是:

combo.setUI(new BasicComboBoxUI());
combo.setBackground(tbl.getSelectionBackground());
combo.setForeground(tbl.getSelectionForeground());

以下是相应程序的屏幕截图(适用于 Windows): Table example

关于java - 如何在 Java Swing 中对表格中选中的组合框组件进行颜色渲染?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48147789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com