• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hi I need help to make my GUI program in Java

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 -

3.jpg
[Thumbnail for 3.jpg]
1
4.jpg
[Thumbnail for 4.jpg]
2
5.jpg
[Thumbnail for 5.jpg]
3
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I shall move this discussion to our GUIs forum, where we usually discuss such question.
 
Pamela Hendersen
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?
 
Ranch Hand
Posts: 47
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any serious disadvantages of the CardLayout ?
 
Tony Docherty
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 47
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Power corrupts. Absolute power xxxxxxxxxxxxxxxx is kinda neat.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic