1
votes


I got a JTable, which i applied AUTO_RESIZE_LAST_COLUMN to. It auto resizes last column when i drag the columns left or right..
However, the JTable are attached to a JPanel with a BorderLayout manager. When i resize the JFrame, the JPanel resize, and since the JTable fills the JPanel, the JTable resizes too. But when i resize the JFrame, the AUTO_RESIZE_LAST_COLUMN doesnt works, but instead it resizes all the columns.

I want it to only auto_resize the last column, when the JFrame changes size, instead of resize all columns.

code:

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

public class JTableResize extends JFrame {

    private JTable table;
    private JPanel panel;
    private JScrollPane pane;

    DefaultTableModel model = new DefaultTableModel();

    public JTableResize() {
        super("JTable - Resize Problem");

        setLayout(new BorderLayout());

        panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.setBackground(Color.red);
        add(panel, BorderLayout.CENTER);

        table = new JTable(model);
        //panel.add(table);
        model.addColumn("Resize");
        model.addColumn("Problem");
        model.addColumn("........");
        model.addColumn("This should resize");

        pane = new JScrollPane(table);
        panel.add(pane);

        //this is supposed to resize last column.. It works when you drag in the columns, but not when frame are resized
        table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
        table.getTableHeader().setReorderingAllowed(false);
        table.setShowVerticalLines(false);

        for (int i = 0; i <= 50; i++) {
            model.addRow(new Object[] {i, i, i, i});
        }
    }

    public static void main(String[] args) {
        JTableResize jtr = new JTableResize();
        jtr.setSize(500, 500);
        jtr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jtr.setVisible(true);
    }
}
1
I don't see anything wrong with your code..!!! it is working absolutely fine.Mac
@Mac Try to run it, and resize the window to the right (make the window bigger). You should see, that all the Columns in the JTable are resizing. But i want it, to only resize the last column (right-most). So when i make the window bigger, only the last column will get bigger (extending). So the 3 first columns should resize, when window get bigger :)Oliver Bak
Next time, please improve the original question rather than create a new one.Hovercraft Full Of Eels
@HovercraftFullOfEels I will keep that in mind =). I deleted the old one though.Oliver Bak

1 Answers

4
votes

Check out the API documentation from the doLayout() method of JTable.

Before the layout begins the method gets the resizingColumn of the tableHeader. 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.

So this behaviour is not support by default.

Overriding the doLayout() method of the JTable and setting the "resizing column" to the last column seems to do the trick:

@Override
public void doLayout()
{
    if (tableHeader != null)
    {
        TableColumn resizingColumn = tableHeader.getResizingColumn();
        //  Viewport size changed. Increase last columns width

        if (resizingColumn == null)
        {
            TableColumnModel tcm = getColumnModel();
            int lastColumn = tcm.getColumnCount() - 1;
            tableHeader.setResizingColumn( tcm.getColumn( lastColumn ) ) ;
        }
    }

    super.doLayout();
}