在 JTable 中,如何呈现带有脏标记的复选框列?

发布于 2024-08-24 10:57:25 字数 2706 浏览 4 评论 0原文

我试图在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

神经大条 2024-08-31 10:57:25

如果您坚持让“脏图标”显示在复选框后面,那么您将不得不使用面板。

为了防止列的布局被破坏,您应该始终呈现图标,即使它是透明的占位符图标。

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.

ま昔日黯然 2024-08-31 10:57:25

一种方便的方法是让 DirtyCheckboxRenderer 实现 Icon 接口并在构造函数中执行 setIcon(this)paintIcon() 方法为您提供对组件的引用,并且 xy 坐标将正确反映您的文本位置设置。

One convenient approach is to have DirtyCheckboxRenderer implement the Icon interface and do setIcon(this) in the constructor. The paintIcon() method gives you a reference to the component, and the x and y coordinates will correctly reflect your text position settings.

风轻花落早 2024-08-31 10:57:25

如果您在 TableModel 的 getColumnClass 方法中返回 Boolean,JTable 应该自动呈现一个 Checkbox。

JTable should render a Checkbox automatically, if you return Boolean in the getColumnClass method in the TableModel.

久光 2024-08-31 10:57:25

嗯,一个简单的方法是更改​​复选框的文本并在其前面加上星号。

Well, a simple way is to change the text of the checkbox and prefix it with an asterisk.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文