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){
        this.beanBushColor=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!

c:\dev\java>
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) {
        System.out.println(e);
    }
     
    public void mouseReleased(MouseEvent e) {
      System.out.println(e);
    }
     
    public void mouseEntered(MouseEvent e) {
        System.out.println(e);
    }
     
    public void mouseExited(MouseEvent e) {
      System.out.println(e);
    }
     
    public void mouseClicked(MouseEvent e) {
       System.out.println(e);
    }
    .....
    // of MouseMotionListener class
    public void mouseMoved(MouseEvent e) {
       System.out.println(e);
    }

    public void mouseDragged(MouseEvent e) {
      System.out.println(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) {
        System.out.println(e);
    }
    public void keyPressed(KeyEvent e) {
       System.out.println(e);
    }
    public void keyReleased(KeyEvent e) {
        System.out.println(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.