【发布时间】:2014-07-16 19:18:02
【问题描述】:
很难描述会发生什么,所以这里是一个 SSCCE:
public class TableInScrollPaneTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TableInScrollPaneTest();
}
});
}
public TableInScrollPaneTest() {
JFrame frame = new JFrame();
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
LayoutManager layout;
layout = new MigLayout("wrap 1, fill", "fill, grow", "[fill, grow 1][fill, grow 2]");
//layout = new GridLayout(2,1);
JPanel panel = new JPanel(layout);
JPanel placeHolder = new JPanel();
JPanel placeHolder2 = new JPanel();
placeHolder.setBackground(Color.GRAY);
placeHolder2.setBackground(Color.LIGHT_GRAY);
JTable table = this.createTable();
JScrollPane tableScrollPane = new JScrollPane(table,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//table.setPreferredScrollableViewportSize(new Dimension(5000,200));
panel.add(placeHolder, "");
panel.add(tableScrollPane, "");
//panel.add(placeHolder2, "");
frame.setContentPane(panel);
frame.setVisible(true);
}
private JTable createTable() {
String[] columnNames = "Name 1,Name 2,Name 3,Name 4,Name 5".split(",");
int rows = 30;
int cols = columnNames.length;
String[][] data = new String[rows][cols];
for(int i=0; i<rows; i++) {
for(int j=0; j<cols; j++) {
data[i][j] = "R"+i+" C"+j;
}
}
JTable table = new JTable(data, columnNames);
return table;
}
}
当用户调整框架大小时,我希望 scrollPane 使用所有可用空间。但另外我也希望另一个面板(placeHolder)增长。例如框架的高度除以 3,placeHolder 得到 1 部分,scrollPane 得到 2 部分。 GridLayout 可以满足我的需要,但我正在寻找 MigLayout 的解决方案。
如果您取消注释 panel.add(placeHolder2, ""); 并注释上面的行,它会起作用。
编辑1:
我尝试使用 sizegroup 约束。现在 placeHolder 和 scrollPane 的大小相同,但我希望 scrollPane 占用两倍的空间。 (另外在我的程序中,我没有这样的占位符,而是有一些标签、复选框和文本字段。所以 sizegroup 可能不是我需要的,除非有更多的使用方法)
【问题讨论】:
标签: java swing jtable jscrollpane miglayout