设置JTable中JCheckBox单元格的颜色 [英] Setting the color for a JCheckBox cell in a JTable
本文介绍了设置JTable中JCheckBox单元格的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个JCheckBox在JTable的最后一列工作。但是当我将颜色设置为该列中的单元格时,它似乎覆盖了渲染的对象(JCheckBox)。
I have a JCheckBox working in the last column in a JTable. But when I set the color to the cells in that column, it seem to overwrite the rendered object (JCheckBox).
下面的代码段是我想要做的:
The snippet of code below is what I am trying to do:
//Overriding these methods using the DefaultTableModel constructor works .
DefaultTableModel model = new DefaultTableModel(data, columnNames)
{
@Override
public Class getColumnClass(int col)
{
return getValueAt(1, col).getClass();
}
@Override
public boolean isCellEditable(int rowIndex, int colIndex)
{
return (colIndex == CHECK_COL);
}
};
JTable table = new JTable(model);
//Constructing and setting a render background and foreground color
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setBackground(Color.BLACK);
renderer.setForeground(new Color(255, 0, 255));
TableColumn column = table.getColumnModel().getColumn(4);
column.setCellRenderer(centerRenderer);
//Now the last column contains just Boolean values, rather than JCheckBox's when I try set the colors.
任何人都能知道如何克服这个问题?
谢谢。非常感谢
Can anyone figure out how I can overcome this? Thanks. Much appreciated
推荐答案
-
DefaultTableCellRenderer
在JLabel
,因此它将永远不会呈现JCheckBox
foreground
/ 背景
值可能会被重写为渲染过程的一部分,这意味着它们会为每个单元格您提供的默认值。 DefaultTableCellRenderer
is based on aJLabel
, so it will never renderer aJCheckBox
- The
foreground
/background
values are likely to be overridden as part of the rendering process, meaning they will change for each cell, losing there default values you have supplied.
您需要提供一个单元格渲染器...
You need to supply a cell renderer that...
- 基于
JCheckBox
- 可以恢复默认
>
这是一个演示基本概念的基本示例...
This is a basic example which demonstrates the basic concept...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.UIResource;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
public class BooleanCellEditor {
public static void main(String[] args) {
new BooleanCellEditor();
}
public BooleanCellEditor() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
TableModel model = new AbstractTableModel() {
@Override
public Class<?> getColumnClass(int columnIndex) {
return Boolean.class;
}
@Override
public int getRowCount() {
return 4;
}
@Override
public int getColumnCount() {
return 1;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return true;
}
};
JTable table = new JTable(model);
table.setDefaultRenderer(Boolean.class, new BooleanRenderer());
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public static class BooleanRenderer extends JCheckBox implements TableCellRenderer, UIResource {
private static final Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
public BooleanRenderer() {
super();
setHorizontalAlignment(JLabel.CENTER);
setBorderPainted(true);
setOpaque(true);
setText("Hello");
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
} else {
setBackground(Color.BLACK);
setForeground(new Color(255, 0, 255));
}
setSelected((value != null && ((Boolean) value).booleanValue()));
if (hasFocus) {
setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
} else {
setBorder(noFocusBorder);
}
return this;
}
}
}
注意, code> BooleanRenderer 来自 JTable
;)中的默认实现
Note, I stole the BooleanRenderer
from the default implementation within JTable
;)
这篇关于设置JTable中JCheckBox单元格的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文