Java JTable动态改变行高
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3454691/
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 change the row height dynamically
提问by kohlerfc
i am having trouble changing the height of the my rows dynamically, is there a method i need to overload?
我在动态更改行的高度时遇到问题,是否有需要重载的方法?
--Edit--
- 编辑 -
Sorry for the short post it was my first ....My problems was really to do with changing the row height depending on the content. So what i have done so far is made a inner class that implements TabelCellRenderer.
抱歉,这是我的第一篇短文……我的问题实际上与根据内容更改行高有关。所以到目前为止我所做的是一个实现 TabelCellRenderer 的内部类。
This is what i am doing at the moment for my row height calculations.
这就是我目前为行高计算所做的事情。
private static class TextAreaRenderer extends JTextPane implements TableCellRenderer
{
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected,
boolean hasFocus, int row,
int column)
{
/* Setup Code here */
this.setText(((String)value).getEntityName());
int height = new Double(this.getPreferredSize().getHeight()).intValue();
if (table.getRowHeight(row) < height)
table.setRowHeight(row, height);
/* some more code */
return this;
}
}
}
Would this be the correct way to do this ? Thanks.
这是正确的方法吗?谢谢。
回答by Paul Jowett
You simply call setRowHeight(row, height);
. For example:
您只需调用setRowHeight(row, height);
. 例如:
JTable t = new JTable(5, 5);
t.setRowHeight(2, 30);
will set the 3rd row to 30 high.
将第 3 行设置为 30 高。
Is your question more complicated? You haven't said much about the problem.
你的问题比较复杂?你没有说太多关于这个问题。