1

For a JTable that's inside a panel, if the panel is made larger, how can I distribute the extra space to only certain columns (in my case, to the last column, though it may be nice to offer "columns 3,4, and 8 will get the extra space).

I want to allow the user to change column sizes for all the columns manually.

I tried table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); That distributes the extra space to all the columns that are resizeable when the JTable is resized. In the the JavaDoc for JTable, for doLayout(), says:

When the method is called as a result of the resizing of an enclosing window, the resizingColumn is null. This means that resizing has taken place "outside" the JTable and the change - or "delta" - should be distributed to all of the columns regardless of this JTable's automatic resize mode.

There may be a way to intercept a "JTable is resizing" event, and do something custom, though I haven't been able to figure it out.

Another solution is presented in How to exclude columns from auto-resizing in a JTable Though it doesn't really tell me how to do it in swing.

4

2 回答 2

3
  • see constant table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

  • default widht is 80px

  • can to prepare initial size, more in Oracle tutorial Setting and Changing Column Widths

于 2013-05-03T22:32:59.110 回答
3

Override the doLayout() method to intercept the layout. This gives the basics. You will need to determine what you want to do when the last column width is about to go negative.

import javax.swing.*;
import javax.swing.table.*;

public class Test2Table
{
    private static Object[][] data = new Object[][]
    {
            { "a", "b", "c" },
            { "d", "e", "f" }
    };
    private static Object[] colNames = new Object[] { "1", "2", "3" };

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run() {
                JTable table = new JTable(data, colNames)
                {
                    @Override
                    public void doLayout()
                    {
                        //  Viewport size changed. Change last column width

                        if (tableHeader != null
                        &&  tableHeader.getResizingColumn() == null)
                        {
                            TableColumnModel tcm = getColumnModel();
                            int delta = getParent().getWidth() - tcm.getTotalColumnWidth();
                            TableColumn last = tcm.getColumn(tcm.getColumnCount() - 1);
                            last.setPreferredWidth(last.getPreferredWidth() + delta);
                            last.setWidth(last.getPreferredWidth());
                        }
                        else
                        {
                            super.doLayout();
                        }
                    }

                };

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}
于 2013-05-04T00:44:50.197 回答