The Java User Interface - More Swing
Written by Mike James   
Article Index
The Java User Interface - More Swing
Properties
Some Controls

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 properties 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, just click the run button, and if you click the button on the form that is displayed you will see the text appear in the text field.

 

app1

 

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 but 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 will return to these components later and describe the way that they are used in more detail. 

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
With NetBeans And Swing

largecover

Contents

  1. Why Java?
  2. Getting started with Java
  3. Introducing Java - Swing Objects
  4. Writing Code
  5. Command Line Programs
  6. User Interface - More Swing
  7. Working With Class
  8. Java Class Inheritance
  9. Java Data Types - Numeric Data
  10. Java Data Types - Arrays And Strings
  11. Building a Java GUI - Containers
  12. Advanced OOP - Type, Casting, Packages
  13. Value And Reference 
  14. Java Lambdas, SAMs And Events

 

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

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info

 

<ASIN:0072263148>