wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Hi I need help to make my GUI program in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Hi I need help to make my GUI program in Java" Watch "Hi I need help to make my GUI program in Java" New topic
Author

Hi I need help to make my GUI program in Java

Pamela Hendersen
Greenhorn

Joined: Mar 13, 2013
Posts: 13
I want to make a simple switchboard in Swing. This switchboard controls all the bulbs in a building. The switchboard has a keyboard that lets you select the bulb location from a drop down menu (eg 2nd floor, room 5). Then, you can either turn the bulb off or on or change its brightness.
I want to also display which bulbs are on and their floor number. I need some basic guidance on how to make the GUI only for my program. The stuff behind the GUI should be easy.

I am posting the screens for this below. The application does not look exactly like this. But, this is what the theme is like -




[Thumbnail for 3.jpg]


[Thumbnail for 4.jpg]


[Thumbnail for 5.jpg]

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

The first thing to do is read The Swing Trail in the Java tutorials, or a Swing book. There are a lot of components, and you should become familiar with them, or at least comfortable in your ability to discover them.

Of particular note are the Layout Managers. Being comfortable with your options here makes building the GUI much easier - you have to learn to pick the correct layout for what you want to display, rather than using one or two you know about and trying to force them to work in all situations. What I do when laying out GUIs is to try to consider different portions of the layout independently - I create different panels and initially different windows for each layout portion, until I get it right. Then, when I have all the parts correct independently, I take the panels where each is laid out and combine them into the final layout. That helps me from trying to make everything fit into one super-layout control which tends to be hard to put together and modify. And if you keep the independent layouts in different classes/objects, it can make it easier to re-use the same layouts in different views.


Steve
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
Welcome to the Ranch

I shall move this discussion to our GUIs forum, where we usually discuss such question.
Pamela Hendersen
Greenhorn

Joined: Mar 13, 2013
Posts: 13
Steve Luke wrote:The first thing to do is read The Swing Trail in the Java tutorials, or a Swing book. There are a lot of components, and you should become familiar with them, or at least comfortable in your ability to discover them.

Of particular note are the Layout Managers. Being comfortable with your options here makes building the GUI much easier - you have to learn to pick the correct layout for what you want to display, rather than using one or two you know about and trying to force them to work in all situations. What I do when laying out GUIs is to try to consider different portions of the layout independently - I create different panels and initially different windows for each layout portion, until I get it right. Then, when I have all the parts correct independently, I take the panels where each is laid out and combine them into the final layout. That helps me from trying to make everything fit into one super-layout control which tends to be hard to put together and modify. And if you keep the independent layouts in different classes/objects, it can make it easier to re-use the same layouts in different views.


Thanks Luke. Can you suggest what might be a good layout for this purpose ? Is it as "simple" as creating a couple of panels for things like key board, display, text fields, drop down menus etc and dumping them into the appropriate layout ?
Steve Myers
Ranch Hand

Joined: Dec 08, 2012
Posts: 47
Pamela Hendersen wrote:Can you suggest what might be a good layout for this purpose ? Is it as "simple" as creating a couple of panels for things like key board, display, text fields, drop down menus etc and dumping them into the appropriate layout ?


IMO the eight or so layout managers included in the Swing API are not very good. The fact that there are eight is a clue to this.

I would suggest just starting with the third party MigLayout and saving yourself a lot of headache. I just noticed that it is now ported to JavaFX2 so thats a benefit as well.

It is fairly intuitive and easy to learn, and you can pretty much make any layout by hand with just the one layout manager and a minimum of nested panels.
The only other layout manager you might want to use is CardLayout since its functionality is unique.

Also IMO Swing is fairly straightforward its just tedious and the code to make the GUI is long.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2166
    
  47
Steve Myers wrote:IMO the eight or so layout managers included in the Swing API are not very good. The fact that there are eight is a clue to this.

Sorry but I have to strongly disagree with you on this. The fact that there are a number of different layout managers has nothing to do with whether or not they are good, it has everything to do with each one (apart from GridBagLayout) being designed to handle a very specific layout with the minimum of input from the programmer. Take GridLayout for example: yes you can produce an all-components-are-laid-out-in-a-grid-at-the-same size type layout with several other layout managers but none are as simple to use as GridLayout.

Steve Myers wrote:I would suggest just starting with the third party MigLayout and saving yourself a lot of headache.

MigLayout is a good layout manager but that doesn't mean it should be used to the exclusion of all others. GridBagLayout is one the most flexible of all layout managers but I don't know many people that would recommend using it in any but the most complex of layout problems.

My advice is to start to use the simple layout managers first and in particular, FlowLayout, BorderLayout and GridLayout. Then when you understand more about GUI's and in particular how layout managers work look at things like MigLayout.
Pamela Hendersen
Greenhorn

Joined: Mar 13, 2013
Posts: 13
Thanks for all the info about the layouts. I was surprised to see the MigLayout. Never knew that existed. Don't know if it is popular and well tested though.
Card Layout also seems useful in my case.

I am also a little concerned about multi-threading/concurrency. Is there a list of pitfalls or things to be careful about when creating concurrent programs with Swing ?
Steve Myers
Ranch Hand

Joined: Dec 08, 2012
Posts: 47
MigLayout's is pretty popular, here's where I heard of it - I have had no issues with it so far. Link also has quite a bit of discussion on layout managers in general.
Pamela Hendersen
Greenhorn

Joined: Mar 13, 2013
Posts: 13
Any serious disadvantages of the CardLayout ?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2166
    
  47
Not that I'm aware of.
If you need to flick between several different panels programmatically as opposed to allowing the user to select tabs on a JTabbedPane to show different panels then a CardLayout is a good solution.
Pamela Hendersen
Greenhorn

Joined: Mar 13, 2013
Posts: 13
Steve Myers wrote:MigLayout's is pretty popular, here's where I heard of it - I have had no issues with it so far. Link also has quite a bit of discussion on layout managers in general.


Is there a guide which can tell me exactly when to use the MigLayout and when to not ?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2166
    
  47
I would say always look for the easiest solution. So if you have to layout a grid of components all at the same size use GridLayout, if you have to position components in different areas of the screen use BorderLayout etc. Only when one of the basic layout managers or a simple combination of basic layout managers won't do what you want to do look at a more complex layout manager like MigLayout.
Steve Myers
Ranch Hand

Joined: Dec 08, 2012
Posts: 47
Pamela Hendersen wrote:Is there a guide which can tell me exactly when to use the MigLayout and when to not ?


I find that using MigLayout for everything is just easier. It's as easy to use as the simple API layout managers, but far more flexible. It basically combines the functionality of all the simple layout managers into one.

The problem I would run into with the simple API managers is nothing exactly fits into the mold, and you end having to use multiple nested layouts - then getting everything to align
properly and making the window resize properly just gets really messy and difficult.

I ended up rewriting everything with MigLayout, and only use it when I start something new.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hi I need help to make my GUI program in Java
 
Similar Threads
[bug] yellow bulb already goes off when only the firstnext unread topic is read
Lights and Switches
De-serialization
English and American English
another question for nerds