在java中设置JTable中行的高度 [英] Setting the height of a row in a JTable in java
问题描述
我一直在寻找能够增加JTable中行高的解决方案。我一直在使用setRowHeight(int int)方法编译并运行OK,但没有增加row [s]。当我使用行的getRowHeight(int)方法设置高度时,它会打印出我将行增加到的大小,所以我不确定是什么问题。下面的代码粗略说明了我是如何解决它的。
I have been searching for a solution to be able to increase the height of a row in a JTable. I have been using the setRowHeight(int int) method which compiles and runs OK, but no row[s] have been increased. When I use the getRowHeight(int) method of the row I set the height to, it does print out the size I increased the row to, so I'm not sure what is wrong. The code below is a rough illustration how I am trying to solve it.
我的类扩展了JFrame。
My class extends JFrame.
String[] columnNames = {"Column 1", "Column 2", "Column 1 3"};
JTable table = new JTable(new DefaultTableModel(columnNames, people.size()));
DefaultTableModel model = (DefaultTableModel) table.getModel();
int count =1;
for(Person p: people)
{
model.insertRow(count,(new Object[]{count, p.getName(), p.getAge()+"",
p.getNationality}));
count++;
}
table.setRowHeight(1, 15);//Try set height to 15 (I've tried higher)
谁能告诉我哪里出错了?我试图将第1行的高度增加到15像素?
Can anyone tell me where I am going wrong? I am trying to increase the height of row 1 to 15 pixels?
推荐答案
不确定离开第一行的意图是什么在索引0为空。 JTable
中的行从索引0开始运行。最好能够发布一个完整的示例(即 SSCCE )展示了这些问题。比较这个工作正常的简单例子:
Not sure what is the intention of leaving the first row at index 0 empty. Rows in JTable
run from index 0. It is best if you could post a complete example (ie SSCCE) that demonstrates the issues. Compare to this simple example that works OK:
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class DemoTable {
private static void createAndShowGUI() {
JFrame frame = new JFrame("DemoTable");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DefaultTableModel model = new DefaultTableModel();
model.setColumnIdentifiers(new Object[] {
"Column 1", "Column 2", "Column 3" });
JTable table = new JTable(model);
for (int count = 0; count < 3; count++){
model.insertRow(count, new Object[] { count, "name", "age"});
}
table.setRowHeight(1, 30);
frame.add(new JScrollPane(table));
frame.setLocationByPlatform(true);
frame.pack();
frame.setVisible(true);
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
这篇关于在java中设置JTable中行的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!