在 JTable 中,如何呈现带有脏标记的复选框列?
我试图在 JTable 中的复选框列上显示一个标记,以指示该值是脏的。
我无法想出渲染标记的方法。我尝试在 JCheckbox 上设置图标,但这只是呈现图标而不是复选框。我尝试过使用面板,但它弄乱了布局。
有谁知道最好的方法是什么?
谢谢
这是我迄今为止尝试过的事情:
import java.awt.Component;
import javax.swing.Icon;
import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableCellRenderer;
public class DirtyCheckboxRenderer extends JCheckBox implements TableCellRenderer {
private final Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
public DirtyCheckboxRenderer() {
setHorizontalAlignment(SwingConstants.CENTER);
setBorderPainted(true);
}
@Override
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
setForegroundColor(table, isSelected);
setBackgroundColor(table, isSelected);
setCheckboxState(value);
setBorder(hasFocus);
setDirtyMarkerIcon();
return this;
}
private void setCheckboxState(Object value) {
boolean checked = value != null && ((Boolean) value).booleanValue();
setSelected(checked);
}
private void setBorder(boolean hasFocus) {
if (hasFocus) {
setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
} else {
setBorder(this.noFocusBorder);
}
}
private void setForegroundColor(JTable table, boolean isSelected) {
if (isSelected) {
setForeground(table.getSelectionForeground());
} else {
setForeground(table.getForeground());
}
}
private void setBackgroundColor(JTable table, boolean isSelected) {
if (isSelected) {
setBackground(table.getSelectionBackground());
} else {
setBackground(table.getBackground());
}
}
private void setDirtyMarkerIcon() {
boolean columnIsDirty = true; //TODO
if (columnIsDirty) {
Icon icon = getDirtyMarkerIcon();
setHorizontalTextPosition(SwingConstants.TRAILING);
setIcon(icon);
} else {
setIcon(null);
}
}
private Icon getDirtyMarkerIcon() {
//TODO
return null; //
}
}
I'm trying to show a marker on checkbox columns in a JTable to indicate that the value is dirty.
I'm having trouble coming up with a way to render the marker. I've tried setting an icon on the JCheckbox but this just renders the icon instead of the checkbox. I've tried using a Panel but it messes up the layout.
Does anyone have an idea what is the best way to do this?
Thanks
This is the sort of thing I've tried so far:
import java.awt.Component;
import javax.swing.Icon;
import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableCellRenderer;
public class DirtyCheckboxRenderer extends JCheckBox implements TableCellRenderer {
private final Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
public DirtyCheckboxRenderer() {
setHorizontalAlignment(SwingConstants.CENTER);
setBorderPainted(true);
}
@Override
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
setForegroundColor(table, isSelected);
setBackgroundColor(table, isSelected);
setCheckboxState(value);
setBorder(hasFocus);
setDirtyMarkerIcon();
return this;
}
private void setCheckboxState(Object value) {
boolean checked = value != null && ((Boolean) value).booleanValue();
setSelected(checked);
}
private void setBorder(boolean hasFocus) {
if (hasFocus) {
setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
} else {
setBorder(this.noFocusBorder);
}
}
private void setForegroundColor(JTable table, boolean isSelected) {
if (isSelected) {
setForeground(table.getSelectionForeground());
} else {
setForeground(table.getForeground());
}
}
private void setBackgroundColor(JTable table, boolean isSelected) {
if (isSelected) {
setBackground(table.getSelectionBackground());
} else {
setBackground(table.getBackground());
}
}
private void setDirtyMarkerIcon() {
boolean columnIsDirty = true; //TODO
if (columnIsDirty) {
Icon icon = getDirtyMarkerIcon();
setHorizontalTextPosition(SwingConstants.TRAILING);
setIcon(icon);
} else {
setIcon(null);
}
}
private Icon getDirtyMarkerIcon() {
//TODO
return null; //
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您坚持让“脏图标”显示在复选框后面,那么您将不得不使用面板。
为了防止列的布局被破坏,您应该始终呈现图标,即使它是透明的占位符图标。
If you insist on having the 'dirty icon' show up trailing the checkbox, then you will have to use a panel.
To keep your column from having a broken layout, you should always render the icon, even if it is a transparent placeholder icon.
一种方便的方法是让
DirtyCheckboxRenderer
实现Icon
接口并在构造函数中执行setIcon(this)
。paintIcon()
方法为您提供对组件的引用,并且x
和y
坐标将正确反映您的文本位置设置。One convenient approach is to have
DirtyCheckboxRenderer
implement theIcon
interface and dosetIcon(this)
in the constructor. ThepaintIcon()
method gives you a reference to the component, and thex
andy
coordinates will correctly reflect your text position settings.如果您在 TableModel 的 getColumnClass 方法中返回 Boolean,JTable 应该自动呈现一个 Checkbox。
JTable should render a Checkbox automatically, if you return Boolean in the getColumnClass method in the TableModel.
嗯,一个简单的方法是更改复选框的文本并在其前面加上星号。
Well, a simple way is to change the text of the checkbox and prefix it with an asterisk.