-1

Like the title suggests, I want to resize grid components so that if there is a small component inside of a grid it will remove excess space.

Here's an example of what it currently looks like:

What it currently looks like.

Instead it should look like this:

This.

I'm using a GridLayout. Which looks kinda like:

grid = new JPanel(new GridLayout(1, 3));
grid.add(JPanel1);
grid.add(Jpanel2);
grid.add(JTextField);`

Any ideas?

EDIT: I fixed the problem by using a MigLayout(Thanks @Blip) and overriding the preferred size. Blip then told me not to override, rather use the MigLayout's additional functionality to get the same results. While that would work, I also decided to change the height of the JTextField. Otherwise I would use the other solution. But thanks everyone for the help!

JediBurrell
  • 1,069
  • 10
  • 24
  • why don't you use a different layout manager? – Blip May 31 '15 at 13:20
  • @Blip all the other ones shrink the JTextField to 1 px wide. Unless you know of one that doesn't. I've tried other ones with `setMinimumSize()` but that doesn't do anything. – JediBurrell May 31 '15 at 13:22
  • have you tried **MigLayout**? – Blip May 31 '15 at 13:23
  • 1
    Use GridBagLayout. With its combination of fill and weight parameters for a component specified by the constraints you give it, you should get what you're looking for. – swingMan May 31 '15 at 13:30
  • Use an appropriate constructor to establish the desired initial size of text components, for [example](http://stackoverflow.com/a/26524195/230513). – trashgod May 31 '15 at 13:31
  • @Blip MigLayout works great, but now the JTextField isn't showing up. Do you know what the problem could be? – JediBurrell May 31 '15 at 13:39
  • could you post the code you used? – Blip May 31 '15 at 13:41
  • @Blip `grid = new JPanel(new MigLayout()); grid.add(JLabel1); grid.add(JLabel2); grid.add(JTextField1);` – JediBurrell May 31 '15 at 13:42
  • it would be `grid = new JPanel(new MigLayout("fill", "[][][grow]", "[]"));` and it should work correctly. – Blip May 31 '15 at 13:46
  • @blip Fixed it by overriding the `getPreferredSize()` function. It's working now, thanks! – JediBurrell May 31 '15 at 13:47
  • if you used the code in my previous post you would not require to override the `getPreferredSize` method. – Blip May 31 '15 at 13:49
  • `GridBagLayout` would be a preferred choice here because accomplishing this is very easy. The problem with external libraries is that you require others to have them or you will need to bundle them. – user1803551 May 31 '15 at 15:02
  • @user1803551 How is this not the case with libGDX or other libraries like that? – JediBurrell Jun 01 '15 at 01:33
  • It is the same case. – user1803551 Jun 01 '15 at 09:47

1 Answers1

1

all the other ones shrink the JTextField to 1 px wide.

No they don't. First of all make sure you create the text field with code like:

JTextField textField = new JTextField(10);

The number will all the text field to determine a reasonable preferred size.

You could then use a FlowLayout, which always respects the preferred size.

Or you could use a GridBagLayout which is more flexible, but also more complicated to use. Yes, when you use a GridBagLayout, if you shrink the fame too small, then components will be displayed at their minimum size, which could be a few pixels for a text field.

The key is to use frame.pack() so components are displayed at their preferred size so you don't have to worry about components shrinking.

Read the Swing tutorial on Layout Managers for more information.

camickr
  • 321,443
  • 19
  • 166
  • 288