Results 1 to 10 of 10
Thread: GridBagLayout problem!
- 03-05-2011, 11:38 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 5
- Rep Power
- 0
GridBagLayout problem!
I'm a beginner to java GUI programming. I started to learn java GUI with one of the introduction to JAVA GUI ... books. There is example about GridBagLayout layout manager, i tried to write the code, but it is not working as i expected. I provide the code that i test and the figure of desired layout. I appreciate your help.
Figure of desired layout:

Obtained wrong layout using the code that i write:

Java Code:import java.awt.*; import javax.swing.*; public class Exercise17_3 extends JFrame{ private GridBagLayout gbl = new GridBagLayout(); public Exercise17_3(){ Container contentPane = this.getContentPane(); contentPane.setLayout(gbl); JPanel panelA = new JPanel(); JPanel panelB = new JPanel(); JPanel panelC = new JPanel(); JPanel panelD = new JPanel(); JPanel panelE = new JPanel(); JPanel panelF = new JPanel(); JPanel panelG = new JPanel(); JPanel panelH = new JPanel(); JPanel panelJ = new JPanel(); panelA.setBorder(BorderFactory.createTitledBorder("panelA")); panelB.setBorder(BorderFactory.createTitledBorder("panelB")); panelC.setBorder(BorderFactory.createTitledBorder("panelC")); panelD.setBorder(BorderFactory.createTitledBorder("panelD")); panelE.setBorder(BorderFactory.createTitledBorder("panelE")); panelF.setBorder(BorderFactory.createTitledBorder("panelF")); panelG.setBorder(BorderFactory.createTitledBorder("panelG")); panelH.setBorder(BorderFactory.createTitledBorder("panelH")); panelJ.setBorder(BorderFactory.createTitledBorder("panelJ")); this.getContentPane().add(panelA); this.getContentPane().add(panelB); this.getContentPane().add(panelC); this.getContentPane().add(panelD); this.getContentPane().add(panelE); this.getContentPane().add(panelF); this.getContentPane().add(panelG); this.getContentPane().add(panelH); this.getContentPane().add(panelJ); easyConstraints(gbl,panelA,1,3,0,0,1.0,1.0); easyConstraints(gbl,panelB,1,2,1,0,1.0,1.0); easyConstraints(gbl,panelC,1,1,2,0,1.0,1.0); easyConstraints(gbl,panelD,1,2,3,0,1.0,1.0); easyConstraints(gbl,panelE,1,2,2,1,1.0,1.0); easyConstraints(gbl,panelF,1,1,1,2,3.0,2.0); easyConstraints(gbl,panelG,1,1,3,2,1.0,1.0); easyConstraints(gbl,panelH,2,1,0,3,1.0,1.0); easyConstraints(gbl,panelJ,2,1,2,3,1.0,1.0); } private void easyConstraints(GridBagLayout GBL, JComponent Comp, int w, int h, int x, int y, double wx, double wy){ GridBagConstraints constraints = new GridBagConstraints(); constraints.fill = GridBagConstraints.BOTH; constraints.gridwidth=w; constraints.gridheight=h; constraints.gridx=x; constraints.gridy=y; constraints.weightx=wx; constraints.weighty=wy; GBL.setConstraints(Comp,constraints); } public static void main(String[] args){ Exercise17_3 gridbagFrame = new Exercise17_3(); gridbagFrame.setTitle("Grid-Bag Layout"); gridbagFrame.setSize(300,250); gridbagFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gridbagFrame.setResizable(false); gridbagFrame.setLocation(680,0); gridbagFrame.setVisible(true); } }
-
This would probably be easier to do using another layout such as MiGLayout although you'd need to download the library.
- 03-05-2011, 12:17 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 5
- Rep Power
- 0
Is there a mistake in the code which results in wrong layout. I need a comment from a view of senior coder...
If i can not solve it would i will jump to another method, but leaving it unsolved and jumping to another method bothers me :)
- 03-05-2011, 12:47 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-05-2011, 01:09 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 5
- Rep Power
- 0
In the book it says weights will scale the cells, i tried to scale F cell (3.0,2.0) since it is comman but it doest work. Can you help me correcting the constraints.
Java Code:easyConstraints(gbl,panelA,1,3,0,0,1.0,1.0); easyConstraints(gbl,panelB,1,2,1,0,1.0,1.0); easyConstraints(gbl,panelC,1,1,2,0,1.0,1.0); easyConstraints(gbl,panelD,1,2,3,0,1.0,1.0); easyConstraints(gbl,panelE,1,2,2,1,1.0,1.0); easyConstraints(gbl,panelF,1,1,1,2,3.0,2.0); easyConstraints(gbl,panelG,1,1,3,2,1.0,1.0); easyConstraints(gbl,panelH,2,1,0,3,1.0,1.0); easyConstraints(gbl,panelJ,2,1,2,3,1.0,1.0);
- 03-05-2011, 01:45 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
- 03-05-2011, 02:46 PM #7
Member
- Join Date
- Mar 2011
- Posts
- 5
- Rep Power
- 0
i try the original Width X Height without weights :
and i obtained again wrong result:Java Code:easyConstraints(gbl,panelA,1,4,0,0,1.0,1.0); easyConstraints(gbl,panelB,3,2,1,0,1.0,1.0); easyConstraints(gbl,panelC,1,1,4,0,1.0,1.0); easyConstraints(gbl,panelD,1,2,5,0,1.0,1.0); easyConstraints(gbl,panelE,1,3,4,1,1.0,1.0); easyConstraints(gbl,panelF,3,2,1,2,1.0,1.0); easyConstraints(gbl,panelG,1,2,5,2,1.0,1.0); easyConstraints(gbl,panelH,4,1,0,4,1.0,1.0); easyConstraints(gbl,panelJ,2,1,4,4,1.0,1.0);
- 03-05-2011, 03:09 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
Set those weights to 0.0
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-05-2011, 04:29 PM #9
Weights don't 'scale the cells'. Weights determine how extra space will be allocated, after providing components their preferredSize. Read the API.
Problems such as yours arise out of not understanding how GridBagLayout determines rows and columns. The weights can only be correctly applied when there is at least one component that lies solely in that row (gridheight = 1) or solely in that column (gridwidth = 1).
One way of achieving this is a complex layout is to add 'dummy' components that are never seen, take up no space, but allow GBL to calculate its rows and columns the way you need. Example (reuses the same GBC, I leave it as an exercise for you to change the code to your style):And kudos to you for omitting 'I' as a panel identifier.Java Code:import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.*; public class GridBagAtoJ { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new GridBagAtoJ().makeUI(); } }); } public void makeUI() { JFrame frame = new JFrame(); frame.setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridy = 0; gbc.weightx = 1.0; gbc.weighty = 0; gbc.fill = GridBagConstraints.HORIZONTAL; for (int i = 1; i <= 6; i++) { gbc.gridx = i; frame.add(Box.createHorizontalGlue(), gbc); } gbc.gridx = 0; gbc.weightx = 0; gbc.weighty = 1.0; gbc.fill = GridBagConstraints.VERTICAL; for (int i = 1; i <= 4; i++) { gbc.gridy = i; frame.add(Box.createVerticalGlue(), gbc); } gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.fill = GridBagConstraints.BOTH; JPanel a = new BorderedPanel("A"); gbc.gridx = 1; gbc.gridy = 1; gbc.gridheight = 4; gbc.gridwidth = 1; frame.add(a, gbc); JPanel b = new BorderedPanel("B"); gbc.gridx = 2; gbc.gridheight = 2; gbc.gridwidth = 3; frame.add(b, gbc); JPanel c = new BorderedPanel("C"); gbc.gridx = 5; gbc.gridheight = 1; gbc.gridwidth = 1; frame.add(c, gbc); JPanel d = new BorderedPanel("D"); gbc.gridx = 6; gbc.gridheight = 2; frame.add(d, gbc); JPanel e = new BorderedPanel("E"); gbc.gridx = 5; gbc.gridy = 2; gbc.gridheight = 3; frame.add(e, gbc); JPanel f = new BorderedPanel("F"); gbc.gridx = 2; gbc.gridy = 3; gbc.gridheight = 2; gbc.gridwidth = 3; frame.add(f, gbc); JPanel g = new BorderedPanel("G"); gbc.gridx = 6; gbc.gridy = 3; gbc.gridheight = 2; gbc.gridwidth = 1; frame.add(g, gbc); JPanel h = new BorderedPanel("H"); gbc.gridx = 1; gbc.gridy = 5; gbc.gridheight = 1; gbc.gridwidth = 4; frame.add(h, gbc); JPanel j = new BorderedPanel("J"); gbc.gridx = 5; gbc.gridwidth = 2; frame.add(j, gbc); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 400); frame.setLocationRelativeTo(null); frame.setVisible(true); } } class BorderedPanel extends JPanel { public BorderedPanel(String title) { setBorder(BorderFactory.createTitledBorder("Panel " + title)); } }
db
- 03-06-2011, 11:35 PM #10
Member
- Join Date
- Mar 2011
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Problem with GridBagLayout
By ze snow in forum New To JavaReplies: 20Last Post: 02-28-2010, 06:08 AM -
GridBagLayout
By Moncleared in forum New To JavaReplies: 1Last Post: 10-18-2009, 10:12 PM -
GridBagLayout
By MuslimCoder in forum New To JavaReplies: 1Last Post: 01-15-2009, 08:54 PM -
Java Swing GridBagLayout Problem
By hemanthjava in forum AWT / SwingReplies: 1Last Post: 06-29-2008, 08:51 PM -
Problem with GridBagLayout
By Daniel in forum SWT / JFaceReplies: 2Last Post: 07-01-2007, 06:57 PM


LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks