When I resize JFrame, the table (or more specifically the scrollPane) squishes into a little rectangle (with remains the same, but height is more like 5-10 pixels).
Also I noticed that there's a specific height threshold of JFrame, and when height of JFrame drops below this, all GridBagLayouts behave correctly (e.g. correct width & height of all panels). When the height is higher than this threshold, both right panels gain additional width and the rightTopPanel loses its height much faster than the rightBottomPanel when decreasing height of JFrame.
The threshold seems to be point when rightTopPanel gets minimum height (about 5-10 pixels).
JPanel panel = new JPanel(new GridBagLayout());
JPanel rightTopPanel = new JPanel(new GridBagLayout());
panel.add(rightTopPanel, Helper.createGridBagConstraints(1, 0, 1, 1, 0.5, 0.5, GridBagConstraints.BOTH));
JPanel rightBottomPanel = new JPanel(new GridLayout(1, 1));
tableModel = new DefaultTableModel(0, 2);
JTable table = new JTable(tableMode开发者_JS百科l);
JScrollPane scrollPane = new JScrollPane(table);
rightBottomPanel.add(scrollPane);
panel.add(rightBottomPanel, Helper.createGridBagConstraints(1, 1, 1, 1, 0.5, 0.5, GridBagConstraints.BOTH));
JPanel leftPanel = new JPanel();
panel.add(leftPanel, Helper.createGridBagConstraints(0, 0, 1, 2, 0.5, 1.0));
add(panel);
Helper.createGridBagConstraints is just a helper method to create GridBagConstraints, with numerous "optional" parameters.
GridBagConstraints createGridBagConstraints(int gridx, int gridy)
GridBagConstraints createGridBagConstraints(int gridx, int gridy, int gridwidth, int gridheight)
GridBagConstraints createGridBagConstraints(int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty)
GridBagConstraints createGridBagConstraints(int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int fill)
Edit: It seems that this is a problem within JScrollPane. If I don't add it, remaining panels resize correctly.
Edit 2: Since, so far no one understands what's my problem, here are screenshots: http://img155.imageshack.us/img155/8847/badgridbaglayout1.png
http://img17.imageshack.us/img17/8779/badgridbaglayout2.png
http://img28.imageshack.us/img28/9336/badgridbaglayout3.png
the table (or more specifically the scrollPane) squishes into a little rectangle (with remains the same, but height is more like 5-10 pixels).
A GridBagLayout
will attempt to paint components at their preferred size. When the frame shrinks, the component will be be painted at is "minimum size". So sometimes is a component has a 0 minimum size it will only display as a small rectangle.
You can try setting the minimum size of the scroll pane equal to the preferred size, or you can use the constraints you use you can control this behaviour.
Read the section from the Swing tutorial on How to Use GridBagLayout for more information.
If you still have problems then post your SSCCE that demonstrates the problem.
TRy to specify ipadx and ipady values for the GridBagConstraints.
I created this program -
package pkg;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class TestFrame extends JFrame {
public TestFrame() {
JPanel panel = new JPanel(new GridBagLayout());
JPanel rightTopPanel = new JPanel(new GridBagLayout());
panel.add(rightTopPanel, new GridBagConstraints(1, 0, 1, 1, 0.5, 0.5, GridBagConstraints.NORTH,
GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
JPanel rightBottomPanel = new JPanel(new GridLayout(1, 1));
DefaultTableModel tableModel = new DefaultTableModel(0, 2);
JTable table = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(table);
rightBottomPanel.add(scrollPane);
panel.add(rightBottomPanel, new GridBagConstraints(1, 1, 1, 1, 0.5, 0.5, GridBagConstraints.NORTH,
GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
JPanel leftPanel = new JPanel();
panel.add(leftPanel, new GridBagConstraints(0, 0, 1, 2, 0.5, 1.0, GridBagConstraints.NORTH,
GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
add(panel);
setSize(800, 600);
setVisible(true);
}
public static void main(String[] args) {
new TestFrame();
}
}
and it seems to work well. What are you putting in your Help.createGridBagConstraints() methods. It seems that there may be where the problem lies.
The only solution is to NOT use any layout at all and position/resize components manually, because none of the builtin layouts in Swing are implemented properly.
精彩评论