我有这个小组:
public class MyPanel extends JPanel {
public MyPanel() {
super();
this.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
JTable leftTable = new JTable();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 0.1;
gbc.weighty = 1.0;
add(leftTable, gbc);
JTextPane textPane = new JTextPane();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 1;
gbc.gridy = 0;
gbc.weightx = 0.4;
gbc.weighty = 1.0;
add(textPane, gbc);
JTable rightTable = new JTable();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 2;
gbc.gridy = 0;
gbc.weightx = 0.5;
gbc.weighty = 1.0;
add(rightTable, gbc);
}
}
然后当我在JTextPane中输入一些字符时,它会自动调整大小。在同一行(文本窗格)上有200个字符,2个JTable不再可见。
如何修复JTextPane的宽度?
感谢。
答案 0 :(得分:3)
只需将gbc.fill = GridBagConstraints.NONE;
用于JTextPane
答案 1 :(得分:1)
最后,我使用了来自FormLayout的JGoodies。
private void initItemsPanel() {
JPanel itemsPanel = new JPanel();
itemsPanel.setLayout(new FormLayout("left:pref, fill:400px, pref:grow", "fill:20px, fill:4px, fill:max(20px;pref)"));
CellConstraints cc = new CellConstraints();
// the left table
JTable leftTable = new JTable();
itemsPanel.add(leftTable.getTableHeader(), cc.xywh(1, 1, 1, 2));
itemsPanel.add(leftTable, cc.xywh(1, 3, 1, 1));
// the text pane
itemsPanel.add(new JTextPane(), cc.xywh(2, 2, 1, 2));
// the right table
JTable rightTable = new JTable();
rightTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
itemsPanel.add(rightTable.getTableHeader(), cc.xywh(3, 1, 1, 2));
itemsPanel.add(rightTable, cc.xywh(3, 3, 1, 1));
// Adding the items panel to dialog panel
JScrollPane itemsScrollPane = new JScrollPane(itemsPanel);
itemsScrollPane.getVerticalScrollBar().setUnitIncrement(3);
dialogPanel.add(itemsScrollPane, BorderLayout.CENTER); // with dialogPanel previously instantiated
}
对于我测试的所有情况(调整大小,空/填充组件等),它更容易预测。
请注意,您必须明确调用getTableHeader()
,因为JTable不直接位于JScrollPane中。
感谢。