重写JTable的DefaultTableCellRenderer以使JTable中的所有单元居中 [英] Overriding JTable's DefaultTableCellRenderer to center all the cells in a JTable

查看:123
本文介绍了重写JTable的DefaultTableCellRenderer以使JTable中的所有单元居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个我无法摆脱的问题。

I've got a problem I can't get rid of.

你知道吗,我对使用JTables很新,所以答案可能就是很简单,但我找不到解决方案:/

Just so you know, I'm fairly new to using JTables, so the answer might be simple, but I can't find a solution :/

所以,我有一个使用AbstractTableModel的JTable,它覆盖了

So, I've got a JTable using an AbstractTableModel, which overrides the

    public Class<?> getColumnClass(int columnIndex_p) 

方法,告诉要显示的每列的类型。其中一个是布尔值。

method, to tell the type of each column to be displayed. One of them is a Boolean.

当我创建一个简单的JTable时,使用

When I create a simple JTable, using

    table_l = new JTable(new MyTableModel());

一切正常,使用复选框(开/关)正确显示布尔值。

everything is fine, and Boolean values are correctly displayed using checkboxes (on/off).

现在,我想将文本放在每个单元格的中心(稍后可能会有更多选项)。

Now, I'd like to center the text on each cell (and, possibly more options later).

所以我为每一列定义了一个新的DefaultTableCellRenderer,如下所示:

So I define a new DefaultTableCellRenderer for each column, like this :

    DefaultTableCellRenderer cellRenderer_l = new DefaultTableCellRenderer() {
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
          // delegate the rendering part to the default renderer (am i right ???)
          Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
          return comp;
        }
    }

然后我只设置此CellRender的水平对齐方式with:

and then I just set the horizontal alignment of this CellRender with :

    cellRenderer_l.setHorizontalAlignment(JLabel.CENTER);

然后我在JTable的每一列上安装这个新的CellRenderer:

Then I install this new CellRenderer on each column of the JTable :

    for (int i = 0; i < table_l.getColumnCount(); ++i) {
        table_l.getColumnModel().getColumn(i).setCellRenderer(cellRenderer_l);
    }

但是,使用新的CellRenderer,显示的JTable没有使用getColumnClass ()我的TableModel的方法,因此只在布尔值上显示true / false字符串。

But, with the new CellRenderer, the displayed JTable isn't using the getColumnClass() method of my TableModel anymore, and thus just display "true/false" String on Boolean values.

我不知道如何让它仍然使用getColumnClass()和以前一样。

I don't know how to get it to still use the getColumnClass() as before.

如果有人有答案...
谢谢

If someone has the answer... Thank you

编辑:
感谢您的所有澄清。
实际上,我真正的问题是:如何影响JTable的所有DefaultRenderer,使它们的结果集中在JTable的单元格中

thanks for all the clarifications you made. In fact, my real question was : "how to affect all DefaultRenderer of a JTable to make them center their result in the JTable's cells"

推荐答案

为了对所有渲染组件应用相同的视觉装饰(如果那是你真正想要的那些,小心,它可能会有可用性惩罚!)你可以覆盖JTable的preparedRenderer方法:

For applying the same visual decoration to all rendering components (if that's what you really want, be careful, it might have an usability penalty!) you can override the JTable's preparedRenderer method:

@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
    Component comp = super.prepareRenderer(...);
    if (comp instanceof JLabel) {
        ((JLabel) comp).setHorizontalAlignment(...);
    }
    return comp;
}

BTW:这种做法违反了不子类JSomething满足应用程序需求。您可以考虑使用 SwingX ,它正式支持可视化装饰渲染组件。因此,不是子类化,而是使用表格注册荧光笔:

BTW: this approach is violating the rule to not subclass JSomething for application needs. You might consider using SwingX which formally supports visually decorating rendering components. So instead of subclassing, you would register a Highlighter with the table:

JXTable table = ...
table.addHighlighter(new AlignmentHighlighter(CENTER), HighlightPredicate.ALWAYS);   

这篇关于重写JTable的DefaultTableCellRenderer以使JTable中的所有单元居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆