Failed to set column width after jtable becomes visible

I read the Oracle API hundreds of times, read countless articles, both here and elsewhere, and I still can't resize the columns after the jtable becomes visible. As you can deduce, I am also trying to set the visibility of columns using jcheckboxes. Using addColumn and removeColumn, as other articles have noted, does not return the columns to their original position. Earlier, I used an additional class to store column information, for example, the column itself, its identifier and the source index. It also became dirty, and the problem of adding / removing data after changing the visibility of the column. I noticed that the data was not in the correct column. As a result, ive chose to set the column to minWidth, preferredWidth and maxWidth.

Now, to my problem, the code below does not adjust the column width if setColumnVisible is set to true. According to Julius, Minera, why is this so?

import java.awt.*;
import java.awt.event.*;

import java.util.*;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.table.*;

class run
{
    public static void main(String args[])
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                Viewer viewer = new Viewer();

                JFrame jframe = new JFrame();
                jframe.add(viewer);
                jframe.createBufferStrategy(1); // required for dual monitor issues
                jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                jframe.setLocation(50,100);
                jframe.pack();
                jframe.setVisible(true);
            }
        });
    }
}

class Viewer extends JPanel
{
    static final long serialVersionUID = 0;

    DefaultTableModel defaultTableModel;
    JTable jtable;
    JCheckBox jcheckBox[];

    public Viewer()
    {
        this.setLayout(new BorderLayout());

        JPanel jpanel = new JPanel(new BorderLayout());

        String columnNames[] = {"a","b","c","d","e"};

        Object tableData[][] = 
        {
            {"a1","b1","c1","d1","e1"},
            {"a2","b2","c2","d2","e2"},
            {"a3","b3","c3","d3","e3"}
        };

        defaultTableModel = new DefaultTableModel(tableData,columnNames);

        jtable = new JTable(defaultTableModel);

        JScrollPane jscrollPane = new JScrollPane(jtable);

        jpanel.add(jscrollPane,BorderLayout.CENTER);

        TableColumnModel tableColumnModel = jtable.getColumnModel();

        int columnCount = tableColumnModel.getColumnCount();

        jcheckBox = new JCheckBox[columnCount];

        JPanel visibility = new JPanel(new GridLayout(1,columnCount));

        for (int column = 0; column < columnCount; column++)
        {
            TableColumn tableColumn = tableColumnModel.getColumn(column);

            String identifier = (String)tableColumn.getIdentifier();

            jcheckBox[column] = new JCheckBox(identifier,true);
            jcheckBox[column].setName(identifier);
            jcheckBox[column].addActionListener(new ColumnListener());

            visibility.add(jcheckBox[column]);
        }

        jpanel.add(visibility,BorderLayout.SOUTH);

        this.add(jpanel,BorderLayout.CENTER);
    }

    public void setColumnVisible(String identifier, boolean setVisible)
    {
        TableColumn tableColumn = jtable.getColumn(identifier);

        int minWidth = 0;
        int preferredWidth = 0;
        int maxWidth = 0;

        if (setVisible)
        {
            minWidth = 100;
            preferredWidth = 100;
            maxWidth = 100;
        }

        tableColumn.setMinWidth(minWidth);
        tableColumn.setPreferredWidth(preferredWidth);
        tableColumn.setMaxWidth(maxWidth);

        //jtable.doLayout(); does not work
        //jtable.validate(); does not work
        //jtable.getTableHeader().resizeAndRepaint(); does not work
    }

    class ColumnListener implements ActionListener
    {
        public void actionPerformed(ActionEvent actionEvent)
        {
            JCheckBox checkBox = (JCheckBox)actionEvent.getSource();

            boolean setVisible = checkBox.isSelected();

            String identifier = checkBox.getName();

            setColumnVisible(identifier,setVisible);
        }
    }
}



doLayout(), validate() resizeAndRepaint() , setAutoResizeMode.

: , , . , - , setMin, setPreferred setMax.

. , , . jtable, ( 1 , 2 , 3 (0-9) .

ive , , .

6 jlist, 3 , , 3 . jtable, ( column1, column2, 3, 4, 5, 6 7.

, . , , , . .. , , , .

+4
1

,

, , . , , .

. TableColumn TableModel TableColumn. TableColumn, 0. , , , .

, setMin, setPreferred setMax.

, . / , :

    tableColumn.setMinWidth(minWidth);
    tableColumn.setMaxWidth(maxWidth);
    tableColumn.setPreferredWidth(preferredWidth);

Edit:

. MCVE SSCCE.

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

public class Main
{
    public static void main(String[] args)
    {
        TableColumn tc = new TableColumn();
        tc.setMinWidth(1);
        tc.setMaxWidth(10);
        tc.setPreferredWidth(15);
        System.out.println( tc.getPreferredWidth() );
    }
}

API, , .

+6

All Articles