about column's width of a JTable in a JScrollPane

14,760

Solution 1

In general you only need to set the preferred width. But to accomplish your goal you first you need to know the default widths and how they are used to determine the actual default size:

min = 15  
preferred = 75  
max = Integer.MAX_VALUE

So you can't use 10 unless you also change the minimum width.

Lets take a simple case where your preferred width is 25 for column 1 and the scrollpane is 500. Therefore the preferred width of the two columns is 100 (25 + 75). The extra 400 pixels is allocated evenly between the two columns so the columns will be displayed at 225 (25 + 200) and 275 (75 + 200). Therefore by default you can't just display the first column at 25 pixels unless you also set the second column to 475.

If you want all columns to be displayed at their preferred widths then you need to use:

table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );

Solution 2

Also set min and max width of column same as set with setPreferredWidth.

infoTable.getColumnModel().getColumn(0).setMinWidth(10);
infoTable.getColumnModel().getColumn(0).setMaxWidth(10);
infoTable.getColumnModel().getColumn(0).setPreferredWidth(10);

Solution 3

Note that setWidth() "should not be used to set the widths of columns in the JTable, use setPreferredWidth() instead." Also, avoid multiple, conflicting constraints. Too many will confound your intent. Often, a single setPreferredSize() will suffice, as suggested below.

Note some features changed for emphasis.

http://i38.tinypic.com/7281tt.png

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

/** @see http://stackoverflow.com/questions/3448030 */
public class TablePanel extends JPanel {

    private JButton button = new JButton("Button");
    private JTable table = new JTable(new DefaultTableModel(100, 2));
    private javax.swing.JScrollPane jsp = new JScrollPane(table);

    public TablePanel() {
        super(new BorderLayout());
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println(e);
            }
        });
        JPanel panel = new JPanel();
        panel.add(button);
        this.add(panel, BorderLayout.WEST);

        jsp.setBorder(BorderFactory.createLineBorder(Color.red));
        jsp.getViewport().setPreferredSize(new Dimension(500, 500));
        this.add(jsp, BorderLayout.CENTER);
        table.getColumnModel().getColumn(0).setPreferredWidth(6);
        table.setGridColor(Color.gray);
    }

    private void display() {
        JFrame f = new JFrame("TablePanel");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TablePanel().display();
            }
        });
    }
}
Share:
14,760
user415726
Author by

user415726

Updated on June 05, 2022

Comments

  • user415726
    user415726 almost 2 years

    i want to set each column of a JTable in a JScrollPane with a fixed num width, but i cannot do it with infoTable.getColumnModel().getColumn(0).setPreferredWidth(10); i use the default layout of JScrollPane and add the table with the scrollPane's setViewPortView(infoTable) in j2se 1.4. How can i get this done? here is my code

    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    public TableFrame() {
    
        jScrollPane1 = new javax.swing.JScrollPane();
        jButton1 = new javax.swing.JButton();
    
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
        jScrollPane1.setBorder(javax.swing.BorderFactory.createLineBorder(
            new java.awt.Color(0, 0, 0)));
    
        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
    
        this.setSize(new Dimension(600, 600));
        this.jScrollPane1.setSize(500, 500);
        this.getContentPane().add(this.jScrollPane1, BorderLayout.CENTER);
        this.getContentPane().add(this.jButton1, BorderLayout.WEST);
        table = new JTable(new javax.swing.table.DefaultTableModel(100, 2));
        this.jScrollPane1.setViewportView(table);
        table.getColumnModel().getColumn(0).setWidth(10);
    }
    

    ps. Thx for replying I need the column can be dragged to resize, so i cannot set the max and min size