Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the correct JTable width?

Tags:

java

swing

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

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

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

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.

like image 815
Александр Елизаров Avatar asked Nov 29 '25 14:11

Александр Елизаров


1 Answers

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.

like image 194
camickr Avatar answered Dec 01 '25 06:12

camickr