java 关于 JScrollPane 中 JTable 的列宽

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3448030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:55:01  来源:igfitidea点击:

about column's width of a JTable in a JScrollPane

javaswingjtablejscrollpanejtableheader

提问by user415726

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

我想在具有固定宽度的 JScrollPane 中设置 JTable 的每一列,但是我无法infoTable.getColumnModel().getColumn(0).setPreferredWidth(10);使用 JScrollPane 的默认布局并在 j2se 1.4 中添加带有 scrollPane 的 setViewPortView(infoTable) 的表格。我怎样才能做到这一点?这是我的代码

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

附:感谢回复我需要可以拖动列来调整大小,所以我无法设置最大和最小大小

回答by YoK

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

还设置列的最小和最大宽度与 setPreferredWidth 设置的相同。

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

回答by camickr

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.

所以你不能使用 10 除非你也改变了最小宽度。

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.

让我们举一个简单的例子,第 1 列的首选宽度为 25,滚动窗格为 500。因此,两列的首选宽度为 100 (25 + 75)。额外的 400 个像素在两列之间平均分配,因此列将显示为 225 (25 + 200) 和 275 (75 + 200)。因此,默认情况下,您不能仅以 25 像素显示第一列,除非您还将第二列设置为 475。

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

如果您希望所有列以其首选宽度显示,则需要使用:

table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );

回答by trashgod

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.

请注意setWidth()“不应用于设置 JTable 中列的宽度,setPreferredWidth()而应使用”。此外,避免多个相互冲突的约束。太多会混淆你的意图。通常,单个setPreferredSize()就足够了,如下所示。

Note some features changed for emphasis.

请注意,某些功能已更改以强调重点。

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();
            }
        });
    }
}

回答by Ido Weinstein

Use:

利用:

setWidth(int i) 

in conjunction with:

和这个结合:

setMaxWidth(int i)
setMinWidth(int i)

Use this only in case you want to set a fixed width for the column and want it to stay like that even if the table changes size or location.

仅当您想为列设置固定宽度并希望它保持不变时才使用此选项,即使表格更改了大小或位置。

setPreferredWidth(10)is only a recommendation to the layout manager.

setPreferredWidth(10)只是对布局经理的建议。

回答by pedromateo

As said by the other contributors, you have to set preferred, min and max widths. Here you can find an example in which column 0 grows, and column 1 has a fixed width:

正如其他贡献者所说,您必须设置首选、最小和最大宽度。在这里您可以找到一个示例,其中第 0 列增长,第 1 列具有固定宽度:

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);