The Java User Interface - More Swing
Written by Alex Armstrong   
Wednesday, 27 June 2012
Article Index
The Java User Interface - More Swing
Methods and Events

Working with Properties - Methods

 

Methods are things that object have that make something happen. They are short blocks of code that are used to do something usually but not always relevant to the object. 

If you want to set or get a property that you have previously worked with in the Property window but this time in code you need to use its set and get method.

You also need to know the name of the object that you are going to use.

This works very simply.

Every object has a name and the Button's name can be seen in the property Window to be JButton1.

Components are generally given fairly obvious names as you create them by dragging and dropping them. Later on you will want to give components more meaningful names like "OK_button" or something that allows you to work out what it is for when you read the code.

You can also see a diagram of all of the objects in your project in the Navigator window usually over on the left-hand side of Netbeans.

 

objects

 

Once you know the name of an object referencing a property in code is very easy if the property is called "name" then you would expect to find the object offering two methods setName and getName.  You refer to any property or method belonging to an object using;

objectname.propertyname

For example if you want to refer to the button's text property you would write

jButton1.getText

and to set it to a value

jButton1.setText("Click Me");

As we will discover later this is a convention and there are other ways to work with properties that are simpler but this is how Swing and most other Java libraries do the job and there are big advantages to doing it this way.

Events

So now we know about properties and the get and set methods.

The next question is how do we get the user interface to respond to what the user is doing?

The answer is that there are special methods that are called when something happens. The something that happens is called an event and the method that is executed as a result is called an event handler - but you will hear a range of descriptions.

Most Swing components have an Action() method that is called when something happens to the component that is appropriate. In the case of the button object clicking on it is an appropriate action and this is indeed what triggers its action event. 

You can add an Action method to your code manually but if you double click on any component in the designer then Netbeans will create one for you ready to customize. Adding an event handler can seem complicated at first - a matter of adding an event listener to the object - and so it is better to allow Netbeans to generate the code for you.

For example if you double click on the button you will find that you are transferred to the code editor ready to edit:

 private void jButton1ActionPerformed(
      java.awt.event.ActionEvent evt) {
  // TODO add your handling code here:
}

The Event parameter i.e. evt  is actually quite a complex object with lots of fields that tell you what the event was all about.  For example evt.getSource tells you what object  the event happened to and evt.getWhen gives you the time it happened.    

As an example let's customise the ActionPerformed method for the button to set some text in the text field:

private void jButton1ActionPerformed(
          java.awt.event.ActionEvent evt) {
 jTextField1.setText(
                 "You clicked the button");
}

You can try this program out and if you click the button you will see the text appear in the text field.

As you learn more Java and become more adventurous in the way you build user interfaces and applications you will need to learn more about events and how to create event handlers bu the simple Action event is enough for now.

Some Basic Controls

It is time to look at the range of components that go to make a rich user interface.

We have already looked at the button and the text field but this isn’t the simplest type of text component - the label is.

A label is used only to display a piece of text and it has no action event associated with it.  There is a setText and a getText method which allows you to work with the label in code but most of the time labels are just used as static text.

After the button and labels, checkboxes and radio buttons are about the most common elements of the user interface.

A checkbox can be selected or unselected. It does support an action event which is fired each time the user changes its state. To discover or set the state of a checkbox you use the getState and the setState methods. The state is either true or false.

Radio buttons correspond to a group of checkboxes that work together in a checkbox group. Only one of the radio buttons in a group can be selected at any given time.  However, if you simply drag-and-drop radio buttons onto the designer then you will discover that they don't work together as a group. 

To make them work as a group you have to explicitly create a ButtonGroup object and add each button to it. You can do this in code but as with most things to do with the UI it is easier to use the designer.

First select a ButtonGroup object from the Palette and drag it onto the designer. You wont see anything appear but you should see the new object in the object navigator window. By default it will be called buttonGroup1. Now select each of the radio buttons in turn and find their buttonGroup property in the property window and select the name of the group i.e. buttonGroup1 for each one. All of the buttons assigned to the same group work together and you can only select one at a time. You can, of course have as many button groups as you need.

As well as buttons and text there are also lists, drop down lists, combo boxes, scroll bars and so on. Each one of these could take a complete chapter to describe but you have the fundamentals of how user interface controls work.

Summary

  • Each control has properties that can be set via the property window or via get/set methods.
  • Each control triggers events that can be processed via an event handler.
  • Event handlers can be created using the designer.

Where Next?

There is a lot to learn about the individual controls but we also need to look in more detail at control containers, i.e. the sort of window that the controls are displayed in, and we need to look at the topic of layout, how controls are arranged in the window. Both topics are subjects of later chapters.

Modern Java Contents

  1. Getting started with Java
  2. Introducing Java - Swing Objects
  3. Writing Java Code - Methods
  4. Command Line Programs
  5. The Java User Interface - More Swing
  6. Working With Class
  7. Class Inheritance
  8. Building a Java GUI - Containers

To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, Facebook, Google+ or Linkedin,  or sign up for our weekly newsletter.

 

blog comments powered by Disqus



Last Updated ( Wednesday, 07 November 2012 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.