Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (526)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (593)
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 2122 times)
0 Members and 1 Guest are viewing this topic.
Offline Eyesackery

Junior Devvie


Medals: 1



« Posted 2012-10-16 10: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
« Reply #1 - Posted 2012-10-16 12: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

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline Eyesackery

Junior Devvie


Medals: 1



« Reply #2 - Posted 2012-10-16 12: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!
Legends of Yore - The Casual Retro Roguelike
Offline meingrosserfreundjo

Senior Newbie





« Reply #3 - Posted 2012-10-16 16: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

JGO Kernel


Medals: 202



« Reply #4 - Posted 2012-10-16 17: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 17:42:06 »

Just use FormLayout or MigLayout.
GridBagLayout is crap.

Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 833
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2012-10-16 17: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
Offline Nate

« JGO Bitwise Duke »


Medals: 158
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #7 - Posted 2012-10-16 19: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: 19



« Reply #8 - Posted 2012-10-16 19: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 Bitwise Duke »


Medals: 158
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #9 - Posted 2012-10-16 20: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 09: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
« Reply #11 - Posted 2012-10-17 10:05:26 »

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

Incorrect!

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline BoBear2681

JGO Coder


Medals: 19



« Reply #12 - Posted 2012-10-17 19: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 Bitwise Duke »


Medals: 158
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #13 - Posted 2012-10-17 23: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.

 

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

The first screenshot will be displayed as a thumbnail.

toopeicgaming1999 (71 views)
2014-11-26 15:22:04

toopeicgaming1999 (60 views)
2014-11-26 15:20:36

toopeicgaming1999 (14 views)
2014-11-26 15:20:08

SHC (27 views)
2014-11-25 12:00:59

SHC (25 views)
2014-11-25 11:53:45

Norakomi (31 views)
2014-11-25 11:26:43

Gibbo3771 (25 views)
2014-11-24 19:59:16

trollwarrior1 (38 views)
2014-11-22 12:13:56

xFryIx (77 views)
2014-11-13 12:34:49

digdugdiggy (55 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!