This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JavaBeans, Swing, AWT and Builder Tools?  RSS feed

 
Shane Miller
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to java and have recently discovered something that is really confusing me. I understand so far, I think, that swing, and AWT(???) are librarys for building and creating GUI's with Java, right? Ok so now I learn about JavaBeans. And from what I read, they are pretty much java classes that are reusable... and for creating GUI's with Java also. If I am correct, then what is the difference? When I hear people refer to JavaBeans, it seems like a whole new type of class.

If someone could explain to me what a javabean is essentially and what is it good for that would be great. Also if javabeans are used for GUI creation when would one want to use a bean instead of a typical class using swing or AWT? Also what is the diff between swing, and AWT?

I guess Im just confused on the whole Idea of beans. I always read about there reuablility, but I thought the whole idea behind OOP, was creating classes that are reuable right? So what is so special about beans? Are they only for GUI's, and if not why would one want to use beans over just a typical swing class? And what is the diff, between swing and AWT??

As you can tell Im really confused, please some one be kind enough to clarify this.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, JavaBeans really don't have anything to do with GUIs. When tutorials write about JavaBeans they often use GUI components as examples because the are easy to visualize. Additionally, there are some tools which will make new JavaBeans-compliant classes by drag/dropping GUI components to a screen. But JavaBeans are not limited or specially associated with GUIs.

Now, what is a JavaBean? This FAQ will answer a lot of questions. But very briefly: JavaBeans is a 'standard' for accessing class data more efficiently. Start with a normal Java class (often called 'Plain Old Java Object' or 'POJO') and define the concept of 'Properties' which is basically data being held by the class/object. The JavaBeans standard defines means of accessing those properties. A Property can be accessed via a 'get' method and changed via a 'set' method. The JavaBeans standard defines how those methods should be named. There is a bit more to it (you need a public, no-arg constructor, and there are property listeners so one class can listen in to when another class's properties are changed) but basically this is it - a JavaBean is a Class with Properties whose Accessors and Mutators follow a specific naming convention.

Why use JavaBeans? What makes them special? Because the Properties accessors and mutators have a specific/defined naming convention tools can use a process called Introspection to find out what Properties a particular bean has, which ones can be written to, or read from, and what their data types are. Then the appropriate methods can be called in a consistent manner without hardcoding anything about them in the 'Client' code (the code which actually uses the Beans). Specifically, it allows code generation tools to easily access the classes and learn a lot about them in an automated fashion. It also provides a nice generally naming convention for any code you write. There are some cases where programming to the JavaBeans standard is more important - like in web applications using JSPs because there is simplified access to JavaBeans using special JSP syntax - as one example.

Coincidentally (well really by design but not by rule), a lot of the classes in the Swing API implement the JavaBeans standard which makes them easy to use with visual GUI builders you can get with most IDEs.

A lot has been written in the comparison between Swing and AWT. As a beginner, you might look at this IBM article which compares the feature set of AWT, Swing, and a third part GUI library called SWT.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!