Java Swing UI - textfield bloat

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
Arancaytar
Posts: 1642
Joined: Thu Mar 15, 2007 12:54 am UTC
Location: 52.44°N, 13.55°E
Contact:

Java Swing UI - textfield bloat

Postby Arancaytar » Mon Nov 15, 2010 12:06 pm UTC

I'm currently writing a graphical Game-of-Life simulator as a class project. It works well, but I can't seem to resize the UI elements the way I want to.

Ideally, I would like the simulated panel to be fluid in size (growing larger when the window does). Instead, if I do not specify any sizes at all, the element that soaks up all available space are the text fields that specify the dimensions (see screenshot). If I do specify exact sizes for every element, then all non-textfield elements get their exact size, but any size left over is soaked up by the text field, which completely ignore their preferred size setting.

Here's a screenshot.

Image
"You cannot dual-wield the sharks. One is enough." -Our DM.
Image

User avatar
flying sheep
Posts: 63
Joined: Sun Jan 31, 2010 12:35 am UTC

Re: Java Swing UI - textfield bloat

Postby flying sheep » Mon Nov 15, 2010 1:30 pm UTC

my advice would be to use a proper ui toolkit.
if you have to use java, i would recommend qtjambi.

0xBADFEED
Posts: 687
Joined: Mon May 05, 2008 2:14 am UTC

Re: Java Swing UI - textfield bloat

Postby 0xBADFEED » Mon Nov 15, 2010 3:06 pm UTC

flying sheep wrote:my advice would be to use a proper ui toolkit.
if you have to use java, i would recommend qtjambi.

Swing is as capable a ui toolkit as any other. What exactly, pray tell, makes Swing not a "proper" ui toolkit? It may not look so great (by default, customization can have it looking fairly slick) but it's certainly fine for this (and lots of other things).

In Swing, the positioning and sizing of UI components is largely handled by instances of LayoutManager. You associate a LayoutManager with the parent widget and then add your child widgets to the layout. The specific LayoutManager used determines how children are positioned and sized.

What LayoutManager are you using to control the positioning/dimensions of your widgets?

User avatar
Arancaytar
Posts: 1642
Joined: Thu Mar 15, 2007 12:54 am UTC
Location: 52.44°N, 13.55°E
Contact:

Re: Java Swing UI - textfield bloat

Postby Arancaytar » Mon Nov 15, 2010 4:17 pm UTC

I'm using nested boxes right now. In the above screenshot, the green playing field, the controls and the speed slider are three columns in a horizontal box layout; the buttons and textfield area are rows in a vertical box layout, and the textfields once again are in their own horizontal box layout inside this.

Maybe I should go for a grid layout instead, or something.
"You cannot dual-wield the sharks. One is enough." -Our DM.
Image

User avatar
flying sheep
Posts: 63
Joined: Sun Jan 31, 2010 12:35 am UTC

Re: Java Swing UI - textfield bloat

Postby flying sheep » Mon Nov 15, 2010 11:07 pm UTC

that’s why i recommend qt. in qt, everything follows the layouts you specified in a logical manner maybe they all did it wrong, but all swing programs which i saw so far and which were made by inexperienced people looked quite broken. in qt, i instantly managed to get my first gui ever to look right.

keeperofdakeys
Posts: 658
Joined: Wed Oct 01, 2008 6:04 am UTC

Re: Java Swing UI - textfield bloat

Postby keeperofdakeys » Mon Nov 15, 2010 11:50 pm UTC

You could add the display output into a second window, that would solve most of your problems.

Have you tried adding the textfields (are they JTextField or JTextArea?) to a single JPanel? Then if that JPanel is named panel, you can set maximum size like this: panel.setMaxiumSize(new Dimension(Y_SIZE_INT,X_SIZE_INT));. Also you can have one master JPanel, then have a few child JPanels in that ant put your elements in these JPanels; this enables you to specify a different layout for the elements while having one for the entire window.

User avatar
Arancaytar
Posts: 1642
Joined: Thu Mar 15, 2007 12:54 am UTC
Location: 52.44°N, 13.55°E
Contact:

Re: Java Swing UI - textfield bloat

Postby Arancaytar » Tue Nov 16, 2010 1:38 am UTC

Thanks for all the toolkit recommendations, but swing is what we're using in the course. :)

I hadn't heard of setMaximumSize() before; I was using setPreferredSize() to little effect, so thanks!
"You cannot dual-wield the sharks. One is enough." -Our DM.
Image

trakof
Posts: 12
Joined: Sat Jul 17, 2010 8:33 pm UTC

Re: Java Swing UI - textfield bloat

Postby trakof » Tue Nov 16, 2010 3:36 am UTC

Seems to me the OP doesn't fully understand layout managers, take a look at http://download.oracle.com/javase/tutor ... isual.html

User avatar
Arancaytar
Posts: 1642
Joined: Thu Mar 15, 2007 12:54 am UTC
Location: 52.44°N, 13.55°E
Contact:

Re: Java Swing UI - textfield bloat

Postby Arancaytar » Wed Nov 17, 2010 7:21 am UTC

Thanks; I had already read that page but have now decided to switch to a GridBagLayout instead of the boxes. It works a lot better. I still have the problem of having to set a fixed size for my playing field rather than getting it to resize with the window, but at least the controls (which are in their own GridLayout JPanel) stay at their normal size.
"You cannot dual-wield the sharks. One is enough." -Our DM.
Image

keeperofdakeys
Posts: 658
Joined: Wed Oct 01, 2008 6:04 am UTC

Re: Java Swing UI - textfield bloat

Postby keeperofdakeys » Wed Nov 17, 2010 7:30 am UTC

You could try using setSize on the GirdLayout JPanel to explicitly set a size, then see if the playing field resizes. Otherwise look in the java api and see if there is any kind of resize attribute for whatever the playing field is; there may also be something like enable resize? (this is a shot in the dark)

0xBADFEED
Posts: 687
Joined: Mon May 05, 2008 2:14 am UTC

Re: Java Swing UI - textfield bloat

Postby 0xBADFEED » Thu Nov 18, 2010 3:42 am UTC

If you're going to be using Swing to make UI's you should really invest some time in learning GroupLayout (provided you can use Java 1.6). It basically subsumes all other layouts and makes them largely unnecessary.

trakof
Posts: 12
Joined: Sat Jul 17, 2010 8:33 pm UTC

Re: Java Swing UI - textfield bloat

Postby trakof » Thu Nov 18, 2010 9:17 am UTC

Just remembered, JTextFields and TextFields cannot be more than 1 line, Java doesn't allow it. Did you accidentally define it as a text area?

User avatar
Arancaytar
Posts: 1642
Joined: Thu Mar 15, 2007 12:54 am UTC
Location: 52.44°N, 13.55°E
Contact:

Re: Java Swing UI - textfield bloat

Postby Arancaytar » Fri Nov 19, 2010 11:06 pm UTC

Nope, JTextField it is.

And indeed, it is only possible to enter one line. This line is vertically centered in the huge white area of the textfield.

Meanwhile, I've solved the problem using a GridLayout panel.
"You cannot dual-wield the sharks. One is enough." -Our DM.
Image

Ansuz
Posts: 11
Joined: Sun Jul 18, 2010 12:51 am UTC

Re: Java Swing UI - textfield bloat

Postby Ansuz » Tue Nov 30, 2010 7:14 pm UTC

Just gotta say, this thread reminded me why I dislike layout managers. To me, using layout managers seem like you're just making a lot of workarounds. I simply use manual/absolute positioning.

For anyone who hasn't messed with manual/absolute positioning, you can set the layout manager to null. This would make it so nothing is positioning your components for you. You'd have to call Component.setBounds() yourself to position and size them. It's a lot of more work, but a lot less frustrating.

-Ansuz

satinyou
Posts: 9
Joined: Sun Apr 25, 2010 9:07 am UTC

Re: Java Swing UI - textfield bloat

Postby satinyou » Thu Dec 09, 2010 5:32 pm UTC

I think one solution would be to use a border layout with the simulation panel in the center and everything else in the right hand border. That sort of thing has worked for me in the past.

Tchiak
Posts: 27
Joined: Fri Aug 22, 2008 10:01 pm UTC

Re: Java Swing UI - textfield bloat

Postby Tchiak » Wed Dec 15, 2010 11:59 am UTC

I had this same problem.. I used a BorderLayout, then in the center i used another layout manager to finish setting up the elements in the middle.. and a GUI once with 4 different Layout managers just to get the layout how I wanted it.. wrote it by hand.. looked good.. stunned the hell out of the prof


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 5 guests