The Java User Interface - More Swing
The Java User Interface - More Swing
Written by Ian Elliot   
Thursday, 28 July 2016
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.

In programing meaningful names are good. 

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.




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;


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


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.


So now we know about properties and the get and set methods - and hence a bit about methods in general.

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 for the same thing.

Most Swing components have an Action() method that is called when something happens to the component that is appropriate to an "action". 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 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.




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.


  • 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



  1. Getting started with Java

    In chapter 1 we tell you how to get started with modern Java development in the shortest possible time. The approach uses NetBeans and Swing and all of the resources used are free to download and use.

  2. Introducing Java - Swing Objects
    In the second chapter of our beginner's guide to Modern Java we find out more about objects by exploring the Swing framework with a simple hands-on example.

  3. Writing Code

    Using ifs and loops is one of the most difficult parts of learning how to program. Our beginners introduction to Java reaches the part all programmers have know and know well - how to write code.

  4. Command Line Programs
    Command line programming means doing things in the simplest possible way. We take a careful look at how data types and code build a program.

  5. User Interface - More Swing
    Finding out how to create a User Interface (UI) using the Java Swing library is not only a useful skill, it also is an ideal way to learn about objects and to make sure that the ideas really have sunk in.

  6. Working With Class
    The Swing components have provided an easy approach to the idea of objects, but there comes a time when you have to find out how to create your own. In this part of Modern Java, we look at the standard ideas of object-oriented programming.

  7. Java Class Inheritance
    Working with classes and objects is a very sophisticated approach to programming. You can't expect to absorb all of its implications in one go. We have already looked at the basics of class and objects. Now we need to look at encapsulation, constructors, overloading and inheritance.

  8. Java Data Types - Numeric Data 
    After looking at some of the advanced ideas of classes and objects we need to return to some simpler topics to make our understanding complete. We need to look more closely at data and, to get things moving, numeric data. 

  9. Java Data Types - Arrays And Strings
  10. Building a Java GUI - Containers
    In this chapter we get to grips with the idea of a container that is used to host components to build a user interface. We also find out how the Swing GUI Builder generates code to make it all much easier. 
  11. Advanced OOP - Type, Casting, Packages
  12. Value And Reference 
  13. Java Lambdas, SAMs And Events



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.






or email your comment to:

Last Updated ( Thursday, 28 July 2016 )

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