Widgets/Java Beans/Components

Written by Larry Gray on . Posted in Widgets / Javabeans / Components

Java Desktop Widgets, JavaBeans and Components

GUI applications are made up of Components in a modular manner. These components (parts) are plugged into one another more or less. Some contain others. Below I list some commonly known GUI widgets. Here is a link to a cool set of charts showing Java Swing Components And here A visual guide to Swing Components

  • Frame
  • Menu Bar
  • Button (Press and Toggle)
  • Button Bar
  • Text Label
  • Text Field
  • Text Box
  • Combo Box
  • List Box
  • Icon
  • Dialog Box (popup)
  • Tool Tip
  • Panel
  • Scroll Pane
  • Tabbed Pane
  • Scroll Bar (vertical or horizontal)
  • Progress Bar
  • Check Box (for selections)
  • Option Button (for selections)
  • Resize Bar
  • Tree Viewer
  • Table Viewer

I am sure there are more than listed above. And anyone may make custom widgets of any kind for any purpose for more robust or specialized GUI's. This brings me to the topic of JavaBeans next.

public class BushBean{
    private Color beanBushColor;
    private Name name;
    public setColor(Color aColor){
    public Color getColor(){
        return this.beanBushColor;
    public setName(String aName){
        this.name = aName;
    public String getName(){
        return this.name;
A Java Bean can be a GUI component or even a business logic class which is not even related to the GUI graphically. Java Bean itself is a design pattern. In order for the GUI widgets to function properly a given popular and well known design must be followed. This is called the MVC or Model-View-Controller. By the way a good site for design patterns is Vince Huston Design Patterns. Though I didn't find MVC but it may be the Mediator Strategy Pattern. I couldn't find a JavaBean design patterns there either. Java Bean itself is a class which has public 'setter' and 'getter' methods for its private properties. Using this design JavaBeans are more or less pluggable. And these properties control how it looks and functions within the GUI. They also control how the widget(view) interacts with its (model) and maybe (controller). Most if not all Java widgets are also a form of a JavaBean in design.

So what is a component? Its something that can be plugged into something else. A frame contains for example a menu bar, a button bar, a main panel. A menu bar contains menu items, and sub menu items etc. A button bar contains buttons. A panel can contain anything, such as other panels and edit boxes and text. A dialog might contain edit boxes, selection boxes etc and text. Some things you click on like sliders and arrows and scroll buttons are controls which are part of the widgets.

And what makes all these parts work together flawlessly? Well this becomes more complicated and even difficult to explain. In a word its called 'multithreading'. Well no there is another word I'll talk about later on in this article 'events'. Related terms might be 'processes'. In java we are dealing with multithreading. This is all part of this new (for the past 20 years for most of us(though in reality its been around since the 60's on mini-computers and main-frames)) technology called 'multitasking' not to be confused with windows 3.1 'task swapping'. When I grew up in computers the processor could only do one thing at a time. What a wonderful world that was in computers. We did talk about how in the future a micro-computer processor could actually be executing different parts of a program at the same time. That future was not far away. This is multithreading. Running different programs at once is multitasking. You as a programmer generally do not have to deal with threads in your GUI directly. This has already been done behind the scenes. You need to become familiar with the concept to troubleshoot and identify possible thread related problems.

Simple thread example.

public class HelloRunnable implements Runnable {

    public void run() {
        System.out.println("Hello from a thread!");

    public static void main(String args[]) {
        (new Thread(new HelloRunnable())).start();
        System.out.println("Hello from main thread.");

c:\dev\java>notepad HelloRunnable.java

c:\dev\java>javac HelloRunnable.java

c:\dev\java>java HelloRunnable
Hello from main thread.
Hello from a thread!

A major problem with all this is that if these processes and threads and such are not dealt with very carefully by the programmer then your GUI hangs up. Or certain parts of the GUI hang up. Or your system hangs up. I think we can all relate to this, especially those of us who have used the MS Windows systems. Though to be fair newer versions of windows seem to be better about not locking up as frequently. But the rule of thumb is that you do not use threads unless you have a good reason to and as few as possible.

Other notes on the widgets in the list above is that some of them are also seen on your web pages as HTML components. Also the last two widgets are very complex and have many specialized sub components. A widget itself can become as complex as any software with a learning curve of its own. Both for the programmer and the end user.

public class MouseEvents extends JPanel
        implements MouseListener, MouseMotionListener {
    // from MouseListner class
    public void mousePressed(MouseEvent e) {
    public void mouseReleased(MouseEvent e) {
    public void mouseEntered(MouseEvent e) {
    public void mouseExited(MouseEvent e) {
    public void mouseClicked(MouseEvent e) {
    // of MouseMotionListener class
    public void mouseMoved(MouseEvent e) {

    public void mouseDragged(MouseEvent e) {
Another major piece of the GUI puzzle is this notion of System "Events". This leads us to another term in programming called 'Event driven programming'. The system monitors mouse and keyboard events and relays them to your GUI application(if it has focus). Focus means its on top and in view of other applications. A component may also need to have focus. Usually you tab through components to give one or another focus. I list a few common GUI events below.
  • Mouse press
  • Mouse release
  • Mouse enter
  • Mouse exit
  • Mouse clicked
  • Mouse moved
  • Mouse drag
// this key listener class can be added to a component such as JTextField or JTextArea or even JPanel or any component.
public class KeyEvents implements KeyListener{
    public void keyTyped(KeyEvent e) {
    public void keyPressed(KeyEvent e) {
    public void keyReleased(KeyEvent e) {
  • Key typed
  • Key pressed
  • Key released
This is all part of controller coding. Basically in Java a class called a Listener is installed in a component. Java has KeyListener and MouseListener, MouseMotionListener and MouseWheelListener and MouseInputListener. On each event type special commands are added and executed if the event occurs to the component. This is event driven programming. Your GUI and Components patiently sit there waiting on you to do something so that they may do something for you. Components have focus as well as the entire application. The last component you clicked on has focus and is paying more attention to you than the rest of them. Focus can also be gained with the keyboard with accessibility keys added to each function or component. For example you can cycle through the components in the GUI with the enter key or tab key. Or the key "H" assigned for the help menu.

A programmers job is to create the view by plugging in all the components. Then he/she alters the model with adjustments to component properties. And then wires it all together with controller code in listeners and such. If I didn't explain all this GUI stuff properly then I am sorry, come back later and maybe I will have made things clearer. I am merely trying to introduce the reader to these concepts. Finally components/widgets/javabeans are fairly big business items because they are to programmers what parts in the auto parts store are to shade tree mechanics.

Desktop Applications

Written by Larry Gray on . Posted in Applications

Java Desktop Applications

Let me explain a few things that will prepare you to run a Java Desktop Application, and other Java Applications as well. If you do not get this right the application won't run.

Bytecode example that looks very similar to assembly language.

  sipush  1000
  if_icmpge       44
  if_icmpge       31
  ifne    25
  goto    38
  iinc    2, 1
  goto    11
  getstatic       #84; // Field java/lang/System.out:Ljava/io/PrintStream;
  invokevirtual   #85; // Method java/io/PrintStream.println:(I)V
  iinc    1, 1
  goto    2
  Java runs in a virtual machine, meaning this VM (java.exe or javaw.exe) interprets Java bytecode. For the VM to be able to interpret the bytecode it needs to be started up and given the name of an executable Java class. An executable Java class is one with a "public static void main(String args[])" method. Also any supporting classes must be on the classpath. The Java VM first loads the class with the main method and then begins loading all other classes needed for the application to run.

Objects, Classes and the classpath variable.

This is Java's world. Its a world with a classpath full of Java classes. Classes are in programming terms types of Objects. You may have heard of OOP (Object Oriented Programming). Java is based on languages such as SmallTalk and C++ when it comes to OOP with some variants of its own. Prior to objects or OOP program code was grouped into files called libraries. Data was grouped into records. OOP combines this and logically groups program logic and data into units called objects.

This slideshow requires JavaScript.

So what is a classpath? Its extremely similar to the DOS or Unix system PATH variable in function. The path variable reduces the number of locations that your operating system will have to search to find commands that you enter at the command line. If you didn't have this it might take many minutes just to find the command to run because its looking over the entire hard drive for the command. The classpath is a system variable. There are other system variables related to Java as well such as JAVA_HOME and CATALINA_HOME and others. I won't talk about those in this article however. The classpath provides a set of locations on the hard drive where the Java VM should look for classes when loading them (loading up and running your java application). This is very handy for the VM because it doesn't have to search the entire hard drive for classes but only say 5 to 10 percent of it or much less. Also this eliminates situations where you have multiple versions of classes in different locations causing class version conflicts. The classpath may be set at the DOS command line or Unix command line. It may be set on Windows in the System Properties Advanced Settings as a permanent setting. On Unix it may be set in .bashrc file for the user. And when executing the VM, a command called 'java' on linux and 'java.exe' on windows, you may specify classpath options as well. This classpath is very important because if you do not get it right your application will fail to startup. There is more to learn about the classpath that I tell you about here, such as inclusion of current folder '.' for example.
import java.awt.*;  
import javax.swing.*;  

public class DisplayGraphics extends JPanel{  
    public void paint(Graphics g) {  
        g.fillRect(130, 30,100, 80);  
        g.drawOval(30,130,50, 60);  
        g.fillOval(130,130,50, 60);  
        g.drawArc(30, 200, 40,50,90,60);  
        g.fillArc(30, 130, 40,50,180,40);  
        public static void main(String[] args) {  
        DisplayGraphics displayGraphics =new DisplayGraphics();  
        JFrame f=new JFrame();  

Once the classpath issues are resolved all that one need do to start a Java application is something like 'java AClassName' or 'java.exe AClassName'. If this does not work for you then you have a classpath problem or have not used the proper class name which may need to be the fully qualified name. For example 'java apackage.AClassName' Java classes are categorized in packages most of the time, which have names. These names correspond to folder names on the system. If indeed there were a package named apackage then it would be in a folder '\apackage' or '/apackage' depending on your system. Remember "\" on windows and "/" on Unix in file paths.

You may also start up Java Applications from within an IDE such as Eclipse by merely clicking on the class and selecting Run from a menu. Another method would be to run a Java application that has been packaged in an executable Jar file. These files are named with '.jar' at the end. On a properly installed Java Runtime Environment or JRE or Java Development Kit JDK or SDK, one can place the jar on his desktop and merely click it to run it as he would any GUI app by clicking an Icon. At the command line you may use a command like 'java -jar somejavaapplication.jar' or java.exe on windows. On linux follow this with the & symbol to run it in the background so that the console is freed up for other things. On windows you may use 'javaw.exe' to do the same thing.

I talk in the Java console applications article more about anything to do with Java from the console or command line. I wanted to mention it here to give the reader a good foundation for the understanding on how to startup Java GUI applications. The easiest way to run a java application is from the jar file by clicking it from the desktop or by adding the jar file to the start menu so that it may be started as any normal GUI application. You may need to add a shortcut on the desktop to the jar file instead of copying the jar file. In more complex applications the jar has to be executed from the installation folder, or using a windows shortcut or unix symbolic link. You may also alternatively navigate in the system folder explorer and click on the executable jar file to run it.

A note to programmers about how this executable jar file works. First you must make up a manifest file that has one entry which describes which class should be the 'main class'. When building this jar file you need to specify the manifest file name in the command. Just unpack 'unjar' any executable jar file to see how this is setup. Simply edit the manifest file to view the syntax. On my java projects there may be many class files with main methods. I do this for several reasons. Many of the main methods are for "boot strap" testing. Meaning I am simply using it to run and test and debug the class itself without starting up and loading the rest of the project. In other cases I have multiple apps within one project. It would be a simple thing to make a menu app for starting up any of them. In those cases the only main method that is needed is the one in the MenuApp and it would be the main class in the jar.

Once you learn to run Java GUI Application the fun begins. You will find them to be as nice as any GUI application if they are properly designed and made. Java GUI's can be made from several libraries and each give them a slightly different look and feel. The standard libraries that come with Sun's and now (Oracle's) Java is made with AWT(Abstract Windowing Toolkit) or Swing which is based on AWT. AWT came first, then Swing. Usually in today's Java world any application from Sun's Java would be a Swing application. Swing itself comes with 4 main 'look and feels'. First there is the Java default Swing Look and Feel. Next the Windows Look and Feel. Then there is the Metal Look and Feel which resembles Unix like GUI's. And there is Motif which resembles Mac GUI's. And custom Look and Feel's can be made as well. So don't be surprised if you start up different Java apps and they look slightly different. This was done so that users on different system could set a look and feel that more closely resembles their own native system's GUI.

IBM has a GUI library (libraries are also know as API or application programmer interface) called SWT (Standard Widget Toolkit). If you are familiar with Eclipse IDE I believe that it is made using SWT and not Sun's Swing. And finally there is OpenGL which is for high end graphics mostly in Game, Simulator or CAD type applications. OpenGL may have its own widgets and look and feel I assume.

If you have installed Sun's Java SDK (JDK) Development Kit (IBM has a development kit and now there is Open JDK yet another one.), you will find demonstration applications for probably both AWT and Swing GUI. These are fairly impressive so have a look. Find and download some Java apps and try running them. A good place to look would be www.sourceforge.net.  Source Forge is a web site which is dedicated to helping assist and coordinate the collaborative efforts of programmers from around the world on creating free software. (This opens up a whole other can of worms as in "why would anyone want to work for free on software", which I will not go into here.) Just know that the software is free, so download and enjoy. Many of the tools I use as a programmer are projects that are hosted at sourceforge. Sourceforge is not the only open source host on the internet. When I get time later in another article I'll be glad to list some others, and they host Java Projects as well. In conclusion I say "search and enjoy".

Well one last note. To run Java apps from the command line you may need to add the Java distribution binaries to the System path variable. This is slightly different on each system and I won't explain this here. Simply search the web and you find many examples and explanations. If I explain more it will be in the article about Java console applications.