根据值渲染器列对齐

时间:2015-10-26 20:55:09

标签: java swing jtable alignment jtableheader

我已经查看了有关设置JTable标题对齐的一些问题/答案,但它们似乎并不能涵盖这种情况。我希望我的JTable头与它们下面的数据具有相同的对齐方式;将它们全部放在中心看起来很奇怪,特别是对于偶尔有长字符串空间但通常只包含短字符串的列 - 标题本身就位于空间的中心。

我见过的许多解决方案都依赖于DefaultCellTableRenderer - 是否所有表头都使用?我不想设置默认渲染器,因为这会改变所有列;我想编写代码来计算列中数据的水平对齐,并设置标题以在水平对齐方面做同样的事情。

我是否必须从渲染器获取组件(使用特定的行,列和值)并在组件上设置对齐?我是否必须为此标题编写自定义渲染器?什么是最干净的整体方法?

- 编辑

先生。 Camick的解决方案很优雅,几乎可以满足我的需求;它拦截列标题的表格单元格渲染器的创建。不知何故,我所做的事情消除了标题的正常格式,但是我的标题上没有阴影和边框(尽管它们按照我的意愿重新对齐!)。

我目前有以下内容:我在哪里获取表格标题的渲染器,所以我可以更改它的对齐方式?

import java.awt.Component;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

public class TableHeaderRenderer implements TableCellRenderer
{
    TableCellRenderer olderRenderer = null;

    public TableHeaderRenderer(TableCellRenderer givenRenderer)
    {
        olderRenderer = givenRenderer;
    }

//  // get the default renderer for the table header.
//  JTableHeader header = table.getTableHeader();
//  TableCellRenderer defaultRenderer = header.getDefaultRenderer();

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int column)
    {
        try // if the casts fail, we end up in the catch below
    {
        // get the renderer for the first row
        DefaultTableCellRenderer firstRowTableCellRenderer = (DefaultTableCellRenderer)table.getCellRenderer(0, column);

        // get the renderer for this column header
        JTableHeader tableHeader = table.getTableHeader();
        TableColumnModel columnModel = tableHeader.getColumnModel();
        TableColumn tableColumn = columnModel.getColumn(column);
        DefaultTableCellRenderer headerRenderer = (DefaultTableCellRenderer)tableColumn.getCellRenderer();

        // if the renderer for the column is null, create one
        if (headerRenderer == null) 
        { headerRenderer =  new DefaultTableCellRenderer(); 
        }

        // finally -- set the header's horizontal alignment to be the same as
        // that for the first row, then get the component 
        headerRenderer.setHorizontalAlignment(firstRowTableCellRenderer.getHorizontalAlignment());
        Component result = headerRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        return result;
    }
        catch (ClassCastException cce)
        {
            // either the first row or the header has a renderer that isn't a DefaultTableCellRenderer
            // just return the component we already have
            return olderRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        }
    }

}

-

好的,编辑2:

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;

@SuppressWarnings("serial")
public class TableHeaderRendererDemo extends JFrame
{
    Object[][]data = { {new Integer(1), "two", "three"},
                   { new Integer(4), "five", "six" }
                     };
    Object[] columnNames = { "first", "second", "third" };

    public static void main(String ... arguments) 
    { new TableHeaderRendererDemo().go(); 
    }

    public void go()
    {
        JTable table = new JTable(data, columnNames);
        JScrollPane scrollPane = new JScrollPane(table);

        // set our own default renderer for the headers
        JTableHeader header = table.getTableHeader();
        try 
        { DefaultTableCellRenderer defaultRenderer = (DefaultTableCellRenderer)header.getDefaultRenderer();
            header.setDefaultRenderer(new TableHeaderRenderer(defaultRenderer));
        }
        catch (ClassCastException e)
        {
            System.err.println("Could not cast default renderer; table headers not aligned");
        }

        add(scrollPane, BorderLayout.CENTER);
        pack();
        setVisible(true);
    }
}

你可以看到我所说的标题问题;我希望整数会默认为正确的,但由于某种原因他们不会。但是你可以看到我刚刚遇到的问题。

2 个答案:

答案 0 :(得分:2)

以下是自定义标题渲染器的示例,它只是简单地使所选列的文本变为粗体:

class MyTableHeaderRenderer implements TableCellRenderer
{
    private TableCellRenderer tableCellRenderer;

    public MyTableHeaderRenderer(TableCellRenderer tableCellRenderer)
    {
        this.tableCellRenderer = tableCellRenderer;
    }

    public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int column)
    {
        Component c = tableCellRenderer.getTableCellRendererComponent(
            table, value, isSelected, hasFocus, row, column);

        if (column == table.getSelectedColumn())
            c.setFont(getFont().deriveFont(Font.BOLD));

        return c;
    }
}

您可以将渲染器用于以下代码:

JTableHeader header = table.getTableHeader();
DefaultTableCellRenderer defaultRenderer = (DefaultTableCellRenderer)header.getDefaultRenderer();
header.setDefaultRenderer( new MyTableHeaderRenderer(defaultRenderer) );

在您的情况下,您需要更改对齐方式,而不是字体。

您可以使用以下代码:

TableCellRenderer tcr = table.getCellRenderer(0, column);
Component renderer = table.prepareRenderer(tcr, 0, column);
defaultRenderer.setAlignment( renderer.getAlignment() ); // whatever the alignment method is.

修改

  

不知怎的,我所做的事情消除了标题的正常格式

您正在使用:

tableColumn.getCellRenderer(); 

不应该是:

tableColumn.getHeaderRenderer();

很少人会按列为表标题指定特殊的渲染器。因此,如果没有为TableColumn指定标题渲染器,您应该只使用JTableHeader的默认渲染器,不要创建DefaultTableCellRenderer,这不是表标题使用的渲染器。

答案 1 :(得分:0)

到处都是,直到我终于来到这里;某种程度上,Boolean的默认渲染器不是DefaultTableCellRenderer,所以我无法通过这种方式获得它的对齐方式。我尝试在第一行获取组件并获得ITS对齐,但如果表中没有数据则会失败。这对我现在想要的东西来说足够好了。在我看来,列的对齐必须存在于某个地方,我仍然希望我能以某种方式获得它的值,但到目前为止,如果列数据使用DefaultTableCellRenderer,我所管理的是一种获取它的方法。 / p>

感谢Messrs.camickr和MadProgrammer。

import java.awt.Component;

import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;

/**
 * Renderer that aligns the table's column header with the column's data;
 * if the renderer for the first row of the column data cannot be cast to
 * DefaultTableCellRenderer, this just sets the alignment to CENTER.
 * <P>Use:
 * <pre>
 *      // set our own default renderer for the headers
        JTableHeader header = table.getTableHeader();
        try 
        { DefaultTableCellRenderer defaultRenderer = (DefaultTableCellRenderer)header.getDefaultRenderer();
            header.setDefaultRenderer(new TableHeaderRenderer(defaultRenderer));
        }
        catch (ClassCastException e)
        {
            System.err.println("Could not cast default renderer; table headers not aligned");
        }
 *
 */
@SuppressWarnings("serial")
public class TableHeaderRenderer extends DefaultTableCellRenderer implements TableCellRenderer
{
    DefaultTableCellRenderer innerRenderer = null;

    public TableHeaderRenderer(TableCellRenderer givenRenderer)
    {
        // save the given renderer, normally the default for the header
        innerRenderer = (DefaultTableCellRenderer)givenRenderer;
    }

    @Override
    public Component getTableCellRendererComponent
    (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
        try
            {
                // get the renderer for the first row
                DefaultTableCellRenderer firstRowTableCellRenderer = (DefaultTableCellRenderer)table.getCellRenderer(0, column);
                int firstRowAlignment = firstRowTableCellRenderer.getHorizontalAlignment();
                // set the alignment of this header to that of the first row
                innerRenderer.setHorizontalAlignment(firstRowAlignment);
                return innerRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, firstRowAlignment, column);
            }
        catch (ClassCastException cce)
        {
          // the first row has a renderer that isn't a DefaultTableCellRenderer
          // just set the alignment to CENTER
            innerRenderer.setHorizontalAlignment(SwingConstants.CENTER);
          return innerRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        }
    }

}