about column's width of a JTable in a JScrollPane
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 actio开发者_运维知识库nPerformed(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
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);
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 );
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.
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();
}
});
}
}
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.
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:
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);
精彩评论