About JTable column width in JScrollPane

I want to set each JTable column in a JScrollPane with a fixed width of num, but I cannot do it using infoTable.getColumnModel().getColumn(0).setPreferredWidth(10); I use the default JScrollPane layout and add the table using scrollPane setViewPortView (infoTable) in j2se 1.4. How can i do this? 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. thanks for the answer I need a column that can be dragged to resize, so I can not set the maximum and minimum size

+3
source share
5 answers

, . , :

min = 15  
preferred = 75  
max = Integer.MAX_VALUE

, 10, .

, 25 1, scrollpane - 500. 100 (25 + 75). 400 , 225 (25 + 200) 275 (75 + 200). 25 , 475.

, , :

table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
+5

, setPreferredWidth.

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

, setWidth() " JTable, setPreferredWidth() ." , . . setPreferredSize() , .

, .

http://i38.tinypic.com/7281tt.png 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();
            }
        });
    }
}
+1

:

setWidth(int i) 

:

setMaxWidth(int i)
setMinWidth(int i)

, , , .

setPreferredWidth(10) - .

0

As said by other participants, you should set your preferred, minimum and maximum width. Here you can find an example in which column 0 grows and column 1 has a fixed width:

table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
final int COLUMN_0_WIDTH = 650;
final int COLUMN_1_WIDTH = 200;
table.getColumnModel().getColumn(0).setPreferredWidth(COLUMN_0_WIDTH);
table.getColumnModel().getColumn(0).setMinWidth(COLUMN_0_WIDTH);
table.getColumnModel().getColumn(0).setMaxWidth(COLUMN_0_WIDTH*3);

table.getColumnModel().getColumn(1).setPreferredWidth(COLUMN_1_WIDTH);
table.getColumnModel().getColumn(1).setMinWidth(COLUMN_1_WIDTH);
table.getColumnModel().getColumn(1).setMaxWidth(COLUMN_1_WIDTH);
0
source

Source: https://habr.com/ru/post/1758892/


All Articles