java - Component in JScrollView in GridBagLayout drawn too small -
I have a custom component, which is derived from JComponent
. I'm putting it in JScrollPane
and it's in the container using GridBagLayout
. Now if the container becomes too small, that is to start displaying scroll bars, then the component small
But after shaping the frame a bit, it becomes:
Here is the code:
Import java.awt *; Import javax.swing *; Square SO26736343 Expands Jeppel {@ Override Public Wide Paint Component (Graphics G) {G. Set color (color. Yellow); G.fillRect (0, 0, getWidth (), getHeight ()); } @ Override Get Public DimensionsCert () {Return New Dimension (300, 300); } Public static zero main (string [] args) {JFrame frm = new JFrame (); Frm.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Container cp = frm.getContentPane (); Cp.setLayout (new gridoutout ()); Gridbag Consultants GBC = New Gridbag Consultants (); Gbc.weightx = 1; Gbc.weighty = 1; Gbc.fill = GridBagConstraints.BOTH; Cp.add (new pocket ("nw"), gbc); Cp.add (new pocket ("n", gbc); Gbc.gridwidth = GridBagConstraints.REMAINDER; Cp.add (new pocket ("NE"), GBC); Gbc.gridwidth = 1; Cp.add (new pocket ("W"), GBC); Cp.add (new JScrollPane (new SO26736343 ())); Gbc.gridwidth = GridBagConstraints.REMAINDER; Cp.add (new pocket ("e"), gbc); Gbc.gridwidth = 1; Cp.add (new pocket ("SW"), GBC); Cp.add (new pocket ("s"), gbc); Cp.add (new pocket ("SE"), GBC); Frm.setSize (500, 500); Frm.setVisible (true); }}
I have executed it on OS X 10.8.5 with Java 1.7.0_25.
I think there may be something wrong with my component, but I do not know what layout does not try to show that component as bigger, even if it appears on its preferred size Can not be done?
I think you forgot to set up constraints while adding your custom panel and thus behave unspecified (Or at least unexpected):
gbc.gridwidth = 1; Cp.add (new pocket ("W"), GBC); Cp.add (new JScrollPane (new SO26736343 ())); // GBC is missing
If you want this panel to fill all available space, then both the weightx
and weighted
properties Set to a value greater than 0 and add your panel using this constraint:
gbc.weightx = 1; Gbc.weighty = 1; Cp.add (new JScrollPane (new SO26736343 ()), GBC;
screenshot
Comments
Post a Comment