java JTable设置列大小问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5775602/
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
JTable set column size problem
提问by Saher Ahwal
I am having this problem of setting column width of JTable.
我在设置 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:
但是,当放置在 while 或 for 循环中时,没有任何反应:
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, regardless of table size(rows and columns).?
此代码不起作用,列大小没有任何变化,有人可以解释原因吗?如果不是,有人可以告诉我如何将行和列的宽度设置为相同,即我希望表格中的所有单元格都是正方形,无论表格大小(行和列)如何。?
Thanks
谢谢
回答by Howard
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
setAutoResizeMode(JTable.AUTO_RESIZE_OFF)
被称为- table 组件嵌入到 a 中
JScrollPane
以允许任意大小
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);
}
});
}
}
回答by xappymah
Try to set also Minimal and Maximum widths to 800
. And other widths if you find them.
尝试将最小和最大宽度也设置为800
。如果你找到它们,还有其他宽度。
回答by Bill K
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.
我相信列宽是相对的。所有 800 应该与所有 100 相同,但尝试将它们设置为不同的值。为了使它们都更大,请使桌子本身更宽。
回答by Abhilash
If the jTable is in jScrollPane you also need to increase the size(width) of the jScrollPane too...
如果 jTable 在 jScrollPane 中,您还需要增加 jScrollPane 的大小(宽度)...
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;
}
我使用了现有表格的大小并增加了窗格的大小...Hope this helps...
希望这可以帮助...
回答by user276002
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...
请记住,while-loop
如果您事先不知道迭代次数,请使用(推荐)...