如何获得正确的JTable宽度? [英] How can I get the correct JTable width?

查看:113
本文介绍了如何获得正确的JTable宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得一个表格宽度。我试着用这段代码来做:

I want to get a table width. I try to do it with this code:

private static class ListenerForOk implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        handle();
        if (!dataIsNotInput()) {
            createDataTab();
            System.out.println(table.getWidth());
        }
    }

方法 createDataTab()完成添加JTable的所有工作。所以,在 createDataTab(); 之后,我的表位于框架上,我可以看到它。但是此代码 table.getWidth()返回零。我尝试使用此代码获取表格宽度的另一种方法:

Method createDataTab() does all the work to add the JTable. So, after createDataTab(); my table lies on frame and I can see it. But this code table.getWidth() returns zero. Another way I try to get table width with this code:

    double sum = 0.0;
    for (int i = 0; i < table.getColumnCount(); i++) {
        sum += table.getColumnModel().getColumn(i).getWidth();
    }
    System.out.println("sum: " + sum);

方法 table.getColumnModel()。getColumn(i).getWidth( )为所有列返回75。它是正确的值,我有一个正确的表宽度。但在我的班级表中,我重写了一个方法。这是我的代码:

The method table.getColumnModel().getColumn(i).getWidth() returns 75 for all columns. It is correct value and I have a correct table width. But in my class for table I override one method. Here is my code:

class MyTable extends JTable {

@Override
public Component prepareRenderer(TableCellRenderer renderer, int row,
        int column) {
    Component component = super.prepareRenderer(renderer, row, column);
    int rendererWidth = component.getPreferredSize().width;
    TableColumn tableColumn = getColumnModel().getColumn(column);
    tableColumn
            .setPreferredWidth(Math.max(rendererWidth
                    + getIntercellSpacing().width,
                    tableColumn.getPreferredWidth()));

    return component;
}

因此,如果我在任何列中输入一个长字符串,此列将相应地扩展带有文字。我做了,专栏扩大了。但在此之后,方法 table.getColumnModel()。getColumn(i).getWidth(); 再次为所有列返回75。那么,我怎样才能获得正确的表格宽度。

So, if I enter a long string in any column this column will expanded accordingly with text in it. I did it and the column was expanded. But after that the method table.getColumnModel().getColumn(i).getWidth(); returns 75 for all columns again. So, how can I get the correct table width.

推荐答案

调整列宽的基本代码是:

The basic code for adjusting column widths is:

JTable table = new JTable( ... );
table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );

for (int column = 0; column < table.getColumnCount(); column++)
{
    TableColumn tableColumn = table.getColumnModel().getColumn(column);
    int preferredWidth = tableColumn.getMinWidth();
    int maxWidth = tableColumn.getMaxWidth();

    for (int row = 0; row < table.getRowCount(); row++)
    {
        TableCellRenderer cellRenderer = table.getCellRenderer(row, column);
        Component c = table.prepareRenderer(cellRenderer, row, column);
        int width = c.getPreferredSize().width + table.getIntercellSpacing().width;
        preferredWidth = Math.max(preferredWidth, width);

        //  We've exceeded the maximum width, no need to check other rows

        if (preferredWidth >= maxWidth)
        {
            preferredWidth = maxWidth;
            break;
        }
    }

    tableColumn.setPreferredWidth( preferredWidth );
}

您还可以查看表格列调整器,用于包含上述代码的类,并添加额外功能,如自动列调整,因为数据已更改。

You can also check out Table Column Adjuster for a class that incorporates the above code and adds extra features like auto column adjustment as data is changed.

这篇关于如何获得正确的JTable宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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