File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes labels in java gui components Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "labels in java gui components" Watch "labels in java gui components" New topic
Author

labels in java gui components

dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
hi all.
Is there good coding practice for putting labels in java gui controls? If I'm coding a java gui application, is it ok to hard code the labels...like for example...
Button b = new Button("Ok");
thanks.


SCJP 1.4<br />SCWCD 1.4
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
hey Dennis,
Thats what i do,
here is a piec of code i did for my distance learning course:

Davy


How simple does it have to be???
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
sorry bearing in mind that the above posted code is a constructor, with these member variables:

Davy
Ken Krebs
Ranch Hand

Joined: Nov 27, 2002
Posts: 451
There is nothing wrong with that, especially if you know you will not have to internationalize the application. If so, you may want to put the message Strings in a resource bundle and pull them out as needed in the app. It's also worth noting that some IDE's, like NetBeans, provide some support for helping to do this internationalization after the fact.


kktec<br />SCJP, SCWCD, SCJD<br />"What we observe is not nature itself, but nature exposed to our method of questioning." - Werner Heisenberg
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
Hi Davy,Ken...looks good. Thank you for your replies.
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
Having written 2 applications that are available to the world, I now put all Strings into a ResourceBundle from the start of a project. This helps in two ways:
1) If you ever i18n, the work is already done for you.
2) As I write the app, I tend to change the strings displayed to the user fairly often til I get it right. By having all the strings in one place, I don't have to hunt down which class the String is defined in and I can change a string without recompiling.
Just my $0.02
Brian


My Java Freeware:<br />MACCC - <a href="http://maccc.pipasoft.com" target="_blank" rel="nofollow">http://maccc.pipasoft.com</a><br />Nator - <a href="http://nator.pipasoft.com" target="_blank" rel="nofollow">http://nator.pipasoft.com</a>
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Moving this to Swing/AWT.


[Jess in Action][AskingGoodQuestions]
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
Hi Brian. Thanks for your reply. Seems like a good idea but is there a performance-related issue with your approach?
thanks.
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
Most of the time a label on a component is set one time (at creation time) and a lookup of the string for it in a ResourceBundle will take negligible time compared to setting up the rest of the Swing/SWT/GUI. So, no there should not be a noticeable performance hit. Even if you change the label after it's created, a simple lookup of the string does not make any noticeable difference in the GUI. Download and try either of my apps (links in sig) - both are i18n'd - every piece of text you see is stored in a ResourceBundle (a .properties file) which is stored in the program's .jar file.
Brian
[ February 10, 2004: Message edited by: Brian Pipa ]
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
Awesome. Thanks Brian.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: labels in java gui components
 
Similar Threads
Java Desktop Application Problem
javascript window problem
GUI and code in a simple game
Swing and I18N question
Trouble adding JLabels to JLayeredPane