简体   繁体   English

修改JTable单元格中的文本对齐方式

[英]Modify text alignment in a JTable cell

I have found a way to allocate the cell in a table, which seems to be a Component 我找到了一种分配表中单元格的方法,该表似乎是一个Component

JTable table = new JTable(...)
{
    public Component prepareRenderer(
        TableCellRenderer renderer, int row, int column)
    {
        Component c = super.prepareRenderer(renderer, row, column);

        //  add custom rendering here

        return c;
    }
};

In the code, c is the Component, but I cannot find a method in Component to modify text alignment. 在代码中, c是Component,但是我无法在Component找到用于修改文本对齐方式的方法。 Anything wrong about this approach? 这种方法有什么问题吗?

Use DefaultTableCellRenderer for that purposes, it has setHorizontalAlignment() method : 为此使用DefaultTableCellRenderer ,它具有setHorizontalAlignment()方法:

DefaultTableCellRenderer renderer = new DefaultTableCellRenderer(){
    @Override
    public Component getTableCellRendererComponent(JTable arg0,Object arg1, boolean arg2, boolean arg3, int arg4, int arg5) {
         Component tableCellRendererComponent = super.getTableCellRendererComponent(arg0, arg1, arg2, arg3, arg4, arg5);
         int align = DefaultTableCellRenderer.CENTER;
         if(condition){
             align = DefaultTableCellRenderer.LEFT;
         }
        ((DefaultTableCellRenderer)tableCellRendererComponent).setHorizontalAlignment(align);
         return tableCellRendererComponent;
    }
};
t.getColumnModel().getColumn(COLUMN).setCellRenderer(renderer);

COLUMN is target column, condition is condition for switching. COLUMN是目标列, condition是切换条件。

(J)Component havent this method(s) implemented in API (J)Component没有此方法在API中实现

there are two ways 有两种方法

  1. by casting (J)Component to JLabel 通过将(J)ComponentJLabel

  2. set this value for class managed by getColumnClass getColumnClass管理的class设置此值

.

DefaultTableCellRenderer stringRenderer = (DefaultTableCellRenderer)
     table.getDefaultRenderer(String.class);
stringRenderer.setHorizontalAlignment(SwingConstants.CENTER);

I am going to make a conclusion. 我将得出一个结论。

For this specifiec problem, we can divide it into two: 对于此特定问题,我们可以将其分为两个部分:

  1. allocate the cell 分配单元
  2. agjust its alignment 调整其对齐方式

To allocate the cell, my originally way and the way alex2410 should be both useful. 要分配单元,我最初的方式和alex2410的方式都应该有用。

JTable table = new JTable(...)
{
    public Component prepareRenderer(
        TableCellRenderer renderer, int row, int column)
    {
        Component c = super.prepareRenderer(renderer, row, column);

        //  add custom rendering here

        return c;
    }
};

another 另一个

DefaultTableCellRenderer renderer = new DefaultTableCellRenderer(){
    @Override
    public Component getTableCellRendererComponent(
             JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
         Component c = super.getTableCellRendererComponent(arg0, arg1, arg2, arg3, arg4, arg5);

         //  add custom rendering here

         return c;
    }
};
t.getColumnModel().getColumn(COLUMN).setCellRenderer(renderer);

And for alignment, the only method I found useful is: 对于对齐,我发现唯一有用的方法是:

((DefaultTableCellRenderer)c).setHorizontalAlignment(SwingConstants.center);

In mKorbel's suggestion, you can cast the Component c to JLabel , but I failed to to that. 在mKorbel的建议中,您可以将Component c强制转换为JLabel ,但我未能做到这一点。

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

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