简体   繁体   English

重写JTable的DefaultTableCellRenderer以使JTable中的所有单元居中

[英]Overriding JTable's DefaultTableCellRenderer to center all the cells in a JTable

I've got a problem I can't get rid of. 我有一个问题,我无法摆脱。

Just so you know, I'm fairly new to using JTables, so the answer might be simple, but I can't find a solution :/ 你知道,我对使用JTables相当新,所以答案可能很简单,但我找不到解决方案:/

So, I've got a JTable using an AbstractTableModel, which overrides the 所以,我有一个使用AbstractTableModel的JTable,它覆盖了

    public Class<?> getColumnClass(int columnIndex_p) 

method, to tell the type of each column to be displayed. 方法,告诉要显示的每列的类型。 One of them is a Boolean. 其中一个是布尔值。

When I create a simple JTable, using 当我创建一个简单的JTable时,使用

    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). 现在,我想将文本集中在每个单元格上(以后可能会有更多选项)。

So I define a new DefaultTableCellRenderer for each column, like this : 所以我为每一列定义了一个新的DefaultTableCellRenderer,如下所示:

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

and then I just set the horizontal alignment of this CellRender with : 然后我只需将此CellRender的水平对齐设置为:

    cellRenderer_l.setHorizontalAlignment(JLabel.CENTER);

Then I install this new CellRenderer on each column of the JTable : 然后我在JTable的每一列上安装这个新的CellRenderer:

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

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. 但是,使用新的CellRenderer,显示的JTable不再使用我的TableModel的getColumnClass()方法,因此只在布尔值上显示“true / false”字符串。

I don't know how to get it to still use the getColumnClass() as before. 我不知道如何使它仍然像以前一样使用getColumnClass()。

If someone has the answer... Thank you 如果有人有答案......谢谢

EDIT: 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的所有DefaultRenderer,使它们的结果集中在JTable的单元格中”

The default cell renderer already does this for values of type Boolean.class , as shown here . 默认的单元格渲染器已经这样做了类型的值Boolean.class ,如图所示这里 If this is not sufficient, please edit your question to include an sscce that exhibits any problems you encounter. 如果这还不够,请编辑您的问题以包含展示您遇到的任何问题的sscce

Addendum: If you need to further customize a DefaultTableCellRenderer , specify the renderer for the applicable type using setDefaultRenderer() , as shown here . 附录:如果您需要进一步自定义DefaultTableCellRenderer ,使用指定的渲染器适用型setDefaultRenderer()如图所示这里

table.setDefaultRenderer(Boolean.class, yourCellRenderer); 

图片

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: 要将相同的视觉装饰应用于所有渲染组件(如果这是您真正想要的,请注意,它可能会有可用性惩罚!)您可以覆盖JTable的preparedRenderer方法:

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

BTW: this approach is violating the rule to not subclass JSomething for application needs. 顺便说一句:这种方法违反了规则, 没有将 JSomething子类化为应用程序需求。 You might consider using SwingX which formally supports visually decorating rendering components. 您可以考虑使用正式支持可视化装饰渲染组件的SwingX So instead of subclassing, you would register a Highlighter with the table: 因此,您可以使用表格注册荧光笔,而不是子类化:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM