[英]Increasing height of jtable dynamically upon insertion of rows
I am having a problem on adjusting the height of a jtable
whenever I insert rows. 每当插入行时,我在调整
jtable
的高度时遇到问题。 I have tried using setsize()
and setPreferredScrollableViewportsize()
for both table and scrollpane
of the table. 我尝试对表和表的
scrollpane
使用setsize()
和setPreferredScrollableViewportsize()
。 Could it be a problem in layout manager? 在布局管理器中可能有问题吗?
I also tried increasing the size of jpanel
too upon inserting each row. 在插入每一行时,我也尝试增加
jpanel
的大小。 BTW, the table lies in a panel and that panel lies in a jDialog
. 顺便说一句,表格位于面板中,而该面板位于
jDialog
。 I am using free design in NetBeans for UI building. 我在NetBeans中使用免费设计进行UI构建。
Try the following... But make sure you editable option is false! 请尝试以下操作...但请确保您的editable选项为false!
DefaultTableModel model = (DefaultTableModel)table.getModel();
table.setModel(model);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
final TableColumnModel colModel = table.getColumnModel();
for(int column=0; column<table.getColumnCount();column++){
int width = 15;
for(int row=0; row<table.getRowCount();row++){
TableCellRenderer render = table.getCellRenderer(row, column);
Component component = table.prepareRenderer(render, row, column);
width = Math.max(component.getPreferredSize().width+1, width);
}
if(width>300){
width = 300;
colModel.getColumn(column).setPreferredWidth(width);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.