How to get maximum column width using TableColumnAdjuster on JTable

TableColumnAdjusteris a great tool, but I just can't get the table to show the full width of each of the columns. I do not see the whole title, which can sometimes be quite long.

Here is what I have:

table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    TableColumnAdjuster tca = new TableColumnAdjuster(table);
    tca.adjustColumns();
    tca.setColumnHeaderIncluded(true);
    tca.setColumnDataIncluded(true);
    tca.setOnlyAdjustLarger( true );
    tca.setDynamicAdjustment( false );

but the column heading is partially hidden unless manually resized. Any ideas on how to show the full width of a column so that I can see the whole heading?

0
source share
2 answers
setDynamicAdjustment(true); //false will work for the static data
you might wann change it to `true`.

call adjustColumns()at the very end after setting the properties.

+1
source

TableColumnAdjuster , . - .

SSCCE, 5 , TableColumnAdjuster:

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

public class SSCCE extends JPanel
{
    public SSCCE()
    {
        setLayout( new BorderLayout() );

        String[] columnNames = {"Column1", "Column with big header text", "Column3"};
        DefaultTableModel model = new DefaultTableModel(columnNames, 3);
        model.setValueAt("column1 data", 0, 0);
        model.setValueAt("column2 data", 1, 1);
        model.setValueAt("column3 long data", 2, 2);

        JTable table = new JTable( model );
        add( new JScrollPane( table ) );

        //  use default behaviour

        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        TableColumnAdjuster tca = new TableColumnAdjuster(table);
        tca.adjustColumns();
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new SSCCE());
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater( () -> createAndShowGUI() );
/*
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
*/
    }
}

, , ?

? ?

, , ?

/ ?

SSCCE, .

+1

All Articles