Quick question regarding the GridBagLayout manager

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:

http://imageshack.us/a/img685/5589/gridbag.png

This is what I’m trying to create:

http://imageshack.us/a/img528/2870/gridbaggood.png

Here’s the code:

	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);

	}

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! >:(

Thank you for that link, that looks like it is going to solve all of my problems!

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.

UuLaxbFKAcc

Just use FormLayout or MigLayout.
GridBagLayout is crap.

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

With a complex layout manager, ‘not that far from what you want to’ is about as near as you get.

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:


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.

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?

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.

@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.

Incorrect!

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? :slight_smile:

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.