English 中文(简体)
将JTable s 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.

就如你所知,我对使用便携式导弹说得很新,因此答案可能简单,但我无法找到解决办法。

因此,I ve got a JTable using an 摘要TableModel, which overes the

    public Class<?> getColumnClass(int columnIndex_p) 

说明每个栏目的类型的方法。 其中一人为蓝.。

当我使用简便可调时

    table_l = new JTable(new MyTableModel());

一切都属于罚款,而使用检查箱(在/起飞)正确展示诱骗的价值观。

现在,我想把案文放在每个囚室上(以及以后可能有更多的选择)。

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

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

    cellRenderer_l.setHorizontalAlignment(JLabel.CENTER);

然后,我把这个新单元安装在表一栏:

    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.

我不知道如何让它像以前那样继续使用“ColumnClass”。

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"

最佳回答

对于所有提供部件(如果你希望的话 /em>的话,需要谨慎处理的话,它可能会有可使用的惩罚!) 您可以推翻准备的黄麻制品 招标方法:

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

BTW:这一办法违反了以下规则:not子级联合数据集,以满足申请需要。 您可考虑使用SwingX,正式支持直观分解部件。 因此,你将不作次分类,而是在表格中登记一位主要人物:

JXTable table = ...
table.addHighlighter(new AlignmentHighlighter(CENTER), HighlightPredicate.ALWAYS);   
问题回答

renderer here所示,已经对类别数值作了调整。 如果这还不够,请问你列入一个,表明你遇到任何问题。

Addendum: If you need to further customize a DefaultTableCellRenderer, specify the renderer for the applicable type using setDefaultRenderer(), as shown here.

table.setDefaultRenderer(Boolean.class, yourCellRenderer); 

“image”/





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签