JTable set column size problem
I am having this problem of setting column width of JTable.
The code below works fine:
TableColumn a =shipsAndOwnHitsTable.getColumnModel().getColumn(0);
a.setPreferredWidth(800);
It changes the width of the first column.
However when placed in a while or a for loop, nothing happens:
int index = 0;
while (index < columnNum){
TableColumn a =shipsAndOwnHitsTable.getColumnModel().getColumn(index);
a.setPreferredWidth(800);
index+=1;
}
This code doesn't work, nothing happens to the column sizes, can someone explain why? If not can someone tell me how to set the row and column width to be the same, i.e I want all cells in my table to be square, 开发者_StackOverflow中文版regardless of table size(rows and columns).?
Thanks
The following code works fine. According to my comments, note that
setAutoResizeMode(JTable.AUTO_RESIZE_OFF)
was called- the table component is embedded into a
JScrollPane
to allow arbitrary size
Compare it with your code and you might spot your issue quite soon.
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.TableColumn;
public class TableColumnSizeTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// prepare table
JTable table = new JTable(new String[][] {
{ "Row 1 Col A", "Row 1 Col B" },
{ "Row 2 Col A", "Row 2 Col B" } },
new String[] { "ColA", "ColB" });
// add into scroll pane
f.getContentPane().add(new JScrollPane(table));
// turn off auto resize
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
// set preferred column widths
int index = 0;
while (index < 2) {
TableColumn a = table.getColumnModel().getColumn(index);
a.setPreferredWidth(10);
index++;
}
f.pack();
f.setVisible(true);
}
});
}
}
Try to set also Minimal and Maximum widths to 800
. And other widths if you find them.
I believe the column widths are relative. All 800 should be the same as all 100, but try setting them to different values. To make them all bigger, make the table itself wider.
Why not use:
int index = 0;
while (index < columnNum){
shipsAndOwnHitsTable.getColumnModel().getColumn(index).setPreferredWidth(800);
index+=1;
}
Also why not change your loop to:
for(int index = 0; index < columnNum; index++){
shipsAndOwnHitsTable.getColumnModel().getColumn(index).setPreferredWidth(800);// two lines of code less
}
Remember you use (recommended) the while-loop
if you don't know the number of iterations beforehand...
If the jTable is in jScrollPane you also need to increase the size(width) of the jScrollPane too...
sample code,
int index = 0;
int columnNum = 2;
while (index < columnNum){
TableColumn a=jTable1.getColumnModel().getColumn(index);
Dimension d = jTable1.getPreferredSize();
d.width = d.width + 500;
jTable1.setPreferredSize(d);
jScrollPane1.setPreferredSize(d);
index+=1;
}
I used a existing table's size and increased the size of the pane...
Hope this helps...
精彩评论