我有一个BoxLayout (在一个BorderLayout的面板中),在其中垂直放置一个JTable和一个按钮。我希望桌子有一个固定的宽度和高度应该调整自己的大小,以填补面板。目前,我对显示的宽度没有意见,但并不是说它填满了整个面板的高度。例如,如果表中的数据为零,我希望只显示列名。我尝试过像setViewportView()和setPreferredSize()这样的东西,但实际上无法让它工作。有什么建议吗?它是在布局还是滚动窗格中?
String[] columnNames = { "Date", "Type"
};
Object[][] data = {
};
JTable myTable = new JTable(data, columnNames);
JButton okButton = new JButton("OK");
JPanel panWest = new JPanel();
panWest.setLayout(new BoxLayout(panWest, BoxLayout.PAGE_AXIS));
JScrollPane scrollPane = new JScrollPane(myTable);
panWest.add(scrollPane);
panWest.add(okButton);编辑--这就是最终起作用的原因:
Dimension dimension = new Dimension();
dimension.height = (myTable.getRowCount()*myTable.getRowHeight())+
myTable.getTableHeader().getPreferredSize().height;
dimension.width = myTable.getColumnModel().getTotalColumnWidth();
scrollPane.setMaximumSize(dimension);发布于 2013-10-31 11:33:55
我希望桌子有一个固定的宽度和高度应该调整自己的大小,以填补面板。
和
我有一个BoxLayout (在一个BorderLayout的面板中),在其中垂直放置一个JTable和一个按钮。
有三种(只提到简单的)方法
LayoutManager for JPanel - FlowLayout (FLowLayout.CENTER),1. override `FLowLayout.LEFT` or `RIGHT` in the case that you want to aling,
2. `JComponent` laid by `FlowLayout` never will be resizable without programatically to change `XxxSize` and notified by `revalidate` & `repaint`,
3. set proper size for JScrollPane by override `JTable.setPreferredScrollableViewportSize(new Dimension(int, int));`,
4. JScrollPane accepts this Dimension as initial size, required is usage of `JFrame.pack(`), before `JFrame.setVisible(true)`
BoxLayout接受min, max and preferred size,覆盖JScrollPane的这三种大小half solution, but most comfortable)将JPanel的LayoutManager改为BorderLayout,1. put `JScrollPane` with `JTable` to `BorderLayout.EAST/WEST` area,
2. then override `JTable.setPreferredScrollableViewportSize(new Dimension(int, int));`,
3. `JScrollPane` will occupy whole area for `BorderLayout.EAST/WEST` and will be resizeable only on `heights` coordinates
MigLayout,以下是关于MigLayout和JScrollPane的几篇文章(与JTextArea、JTable)https://stackoverflow.com/questions/19704328
复制相似问题