Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (404)
games submitted by our members
Games in WIP (289)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Quick question regarding the GridBagLayout manager  (Read 1294 times)
0 Members and 1 Guest are viewing this topic.
Offline Eyesackery

Junior Member


Medals: 1



« Posted 2012-10-16 12:46:18 »

I'm pulling my hair out trying to learn these layout managers.

Does anyone know what may be the problem here?

This is what I'm getting:


This is what I'm trying to create:


Here's the code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
   public void createPanel() {
      this.setVisible(true);
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.insets = new Insets(4, 4, 4, 4);
     
      JTable puzzleLabel = new JTable(null);
     
      JLabel usersLabel = new JLabel("Sudoku");
     
      JPanel southPanel = new JPanel();
      JPanel westPanel = new JPanel();

      JPanel detailsPanel = new JPanel();
      detailsPanel.setLayout(new GridBagLayout());
     
      //Button panel button set
     
      gbc.gridx = 0;
      gbc.gridy = 0;
      detailsPanel.add(new JButton("Button"), gbc);
     
      gbc.gridx = 0;
      gbc.gridy = 1;
      detailsPanel.add(new JButton("Button"), gbc);
     
      gbc.gridx = 0;
      gbc.gridy = 2;
      detailsPanel.add(new JButton("Button"), gbc);
     
      /////////////////////////
     
      gbc.gridx = 0;
      gbc.gridy = 0;
      this.add(westPanel, gbc);
     
      gbc.gridx = 1;
      gbc.gridy = 0;
      gbc.anchor = GridBagConstraints.WEST;
      this.add(usersLabel, gbc);
     
      gbc.gridx = 1;
      gbc.gridy = 1;
      this.add(new JScrollPane(puzzleLabel), gbc);
     
      gbc.gridx = 1;
      gbc.gridy = 2;
      this.add(southPanel, gbc);
     
      gbc.gridx = 2;
      gbc.gridy = 1;
      this.add(detailsPanel, gbc);

   }
Offline nsigma

JGO Knight


Medals: 37



« Reply #1 - Posted 2012-10-16 14:32:00 »

At a guess, because the JTable inside your JScrollPane has no size?  Try setMinimumSize() or setPreferredSize()?  And why are you calling a table a label?!

If you want to hand-code layouts I'd recommend MigLayout, or quite frankly anything that isn't GridBagLayout!  Wink

Offline Eyesackery

Junior Member


Medals: 1



« Reply #2 - Posted 2012-10-16 14:41:26 »

At a guess, because the JTable inside your JScrollPane has no size?  Try setMinimumSize() or setPreferredSize()?  And why are you calling a table a label?!

If you want to hand-code layouts I'd recommend MigLayout, or quite frankly anything that isn't GridBagLayout!  Wink

You have no idea how many times I have contemplated throwing my laptop out of the window in frustration trying to get Layout managers to behave. The last two days have been a nightmare!  Angry

Thank you for that link, that looks like it is going to solve all of my problems!
Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline meingrosserfreundjo

Senior Newbie





« Reply #3 - Posted 2012-10-16 18:08:59 »

I fail to see the Problem everybody is having with GridBagLayout.

Took me 3 minutes to get this running and it seems not that far from what you want to do.

http://pastebin.java-gaming.org/8eeae3e8c29

Maybe if you explained your intended use for south- and westPanel a bit more i could help you even further.
Offline sproingie
« Reply #4 - Posted 2012-10-16 19:37:38 »

<a href="http://www.youtube.com/v/UuLaxbFKAcc?version=3&amp;hl=en_US&amp;start=" target="_blank">http://www.youtube.com/v/UuLaxbFKAcc?version=3&amp;hl=en_US&amp;start=</a>
Offline 65K
« Reply #5 - Posted 2012-10-16 19:42:06 »

Just use FormLayout or MigLayout.
GridBagLayout is crap.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 437
Projects: 4


Hand over your head.


« Reply #6 - Posted 2012-10-16 19:43:54 »

I do all my UI coding using nested panels with different (simple!) layout managers.


Took me 3 minutes to get this running and it seems not that far from what you want to do.
With a complex layout manager, 'not that far from what you want to' is about as near as you get.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline Nate

JGO Wizard


Medals: 81
Projects: 3


Esoteric Software


« Reply #7 - Posted 2012-10-16 21:40:43 »

IMO, having many simple layout managers is a poor approach. As soon as you need a little bit more flexibility than they can provide, then you're stuck and have to switch to a more complex layout manager anyway, or write your own which is time consuming and error prone.

I'd like to suggest my own layout manager, TableLayout. It works for Swing, Android, libgdx, and TWL. It only has about 8 constraints, and a couple of those are not used very often. With a minimal learning curve you can easily layout almost anything.

I did it real quick:



1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
public class Test extends JFrame {
   public Test () {
      super("Test");

      Table root = new Table();
      setContentPane(root);

      JScrollPane scroll = new JScrollPane();

      Table buttons = new Table();
      buttons.defaults().space(6);
      buttons.addCell(new JButton("Button1"));
      buttons.row();
      buttons.addCell(new JButton("Button2"));
      buttons.row();
      buttons.addCell(new JButton("Button3"));

      root.pad(6).defaults().space(6);
      root.addCell("Sudoku").colspan(2).left();
      root.row();
      root.addCell(scroll).expand().fill();
      root.addCell(buttons);

      setSize(640, 480);
      setLocationRelativeTo(null);
      setVisible(true);
   }

   public static void main (String[] args) throws Exception {
      new Test();
   }
}


I always create the widgets separately from defining the layout, as above. This makes it so much easier to read, especially with complex layouts.

Offline BoBear2681

JGO Coder


Medals: 18



« Reply #8 - Posted 2012-10-16 21:56:31 »

I agree with Riven here.  You can get a lot farther than you think with just BorderLayout, BoxLayout, and BorderFactory.createEmptyBorder().  Maybe a couple of other basic LayoutManagers thrown in here and there.  Swing newbies tend to get frustrated (rightfully so) because of the steep learning curve, and start mucking with minimum/preferred sizes, which is almost always the wrong approach.

@Nate, TableLayout looks nice, but it looks like it doesn't support RTL locales?
Offline Nate

JGO Wizard


Medals: 81
Projects: 3


Esoteric Software


« Reply #9 - Posted 2012-10-16 22:10:15 »

Correct, no RTL stuff. To be fair, BoxLayout, etc also has no RTL support. It probably only makes sense in certain contexts, eg buttons on a dialog should be shown in reverse order. I don't think you generally want a layout manager to change the order of your widgets.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline meingrosserfreundjo

Senior Newbie





« Reply #10 - Posted 2012-10-17 11:10:12 »

@Riven: I did not want to state that GridBagLayout is the optimal solution for Eyesackerys Problem. Only that a complex LayoutManager isn't necessarily hard to use.
Offline nsigma

JGO Knight


Medals: 37



« Reply #11 - Posted 2012-10-17 12:05:26 »

To be fair, BoxLayout, etc also has no RTL support.

Incorrect!

Offline BoBear2681

JGO Coder


Medals: 18



« Reply #12 - Posted 2012-10-17 21:21:21 »

Actually, it's been awhile, but I think most (?) built-in LayoutManagers support RTL in some way (SpringLayout comes to mind as one that doesn't).  At least, BorderLayout, BoxLayout, and GridLayout do I know.  With table-ish layouts, I'd think it would be as easy as reversing the concept of "left" and "right", perhaps using constants like BoxLayout has with LINE_AXIS.

But if nobody asks you for that feature, why bloat the library with it?  Smiley
Offline Nate

JGO Wizard


Medals: 81
Projects: 3


Esoteric Software


« Reply #13 - Posted 2012-10-18 01:09:45 »

Oh, my fault. I'm not that familiar with RTL, obviously. I guess it is something that happens automatically. Not sure that is a good idea, it seems like some layouts would not want to be reversed even for RTL.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (32 views)
2013-05-17 21:29:12

alaslipknot (40 views)
2013-05-16 21:24:48

gouessej (70 views)
2013-05-16 00:53:38

gouessej (69 views)
2013-05-16 00:17:58

theagentd (78 views)
2013-05-15 15:01:13

theagentd (73 views)
2013-05-15 15:00:54

StreetDoggy (113 views)
2013-05-14 15:56:26

kutucuk (137 views)
2013-05-12 17:10:36

kutucuk (136 views)
2013-05-12 15:36:09

UnluckyDevil (145 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.113 seconds with 21 queries.