Results 1 to 3 of 3
Thread: GridBagLayout help
- 01-28-2017, 05:23 PM #1
Member
- Join Date
- Jan 2017
- Posts
- 38
- Rep Power
- 0
GridBagLayout help
I'm having problem understanding the concept. I was hoping to get a layout like this:
Java Code:public class SwingExample extends JPanel{ public static void main(String[] args) { JFrame theFrame = new JFrame("Grid Bag Example"); theFrame.setSize(900,600); theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel thePanel = new JPanel(); theFrame.add(thePanel); GridBagLayout gbl = new GridBagLayout(); thePanel.setLayout(gbl); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(10, 10, 10, 10); JButton button1 = new JButton("Button 1"); // these should give it initial position in cell 0,0 ? gbc.gridx = 0; gbc.gridy = 0; // this should give it size of 1x2 ? // why doesn't it do that? gbc.gridwidth = 1; gbc.gridheight = 2; gbc.fill = GridBagConstraints.BOTH; thePanel.add(button1,gbc); JButton button2 = new JButton("Button 2"); gbc.gridx = 1; gbc.gridy = 1; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.fill = GridBagConstraints.BOTH; thePanel.add(button2,gbc); JButton button3 = new JButton("Button 3"); // these should give it initial position in cell 2,2 ? gbc.gridx = 2; gbc.gridy = 2; // this should give it size of 3x1 ? // how do I know which direction it grows? // does it adds columns 4 and 5, or it goes to the left? gbc.gridwidth = 3; gbc.gridheight = 1; gbc.fill = GridBagConstraints.BOTH; thePanel.add(button3,gbc); theFrame.setVisible(true); } }
BTW, is there any way (or different Layout) I could give the table initial size like 7x5 and then add components to span from cell to cell?
For example if I want to add something that would span from 2nd to 5th column in 4th row, jut to type something simple like:
Java Code:gb2l.setsize(7,5); gb2c.set(2-5, 4); add(button1, gb2c);
- 01-28-2017, 08:14 PM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 13
Re: GridBagLayout help
I haven't used it in a long time. You may want to check the tutorials in my signature below. They show examples.
Also check out this post https://www.java-forums.org/new-java...t-problem.html
It references a free layout that is supposedly easier to use (never done it myself). It's called MigLayout
and there is a link.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 01-28-2017, 09:32 PM #3
Member
- Join Date
- Jan 2017
- Posts
- 38
- Rep Power
- 0
Similar Threads
-
GridBagLayout
By Joshua_Parsons in forum AWT / SwingReplies: 1Last Post: 10-29-2016, 10:32 PM -
Help with GridBagLayout please!
By iBowlerHat in forum AWT / SwingReplies: 2Last Post: 07-07-2013, 07:10 PM -
[AWT] GridBagLayout Help.
By Sandia_man in forum AWT / SwingReplies: 2Last Post: 05-23-2010, 09:54 PM -
help with gridbaglayout
By robertbob in forum AWT / SwingReplies: 5Last Post: 05-18-2010, 05:14 AM -
abt gridbaglayout
By pinky in forum AWT / SwingReplies: 1Last Post: 12-15-2008, 09:35 AM
Bookmarks