Introducing Java - Swing Objects
Introducing Java - Swing Objects
Written by Ian Elliot   
Friday, 20 May 2016
Article Index
Introducing Java - Swing Objects
Objects And Programs

Objects And Instances

Finally we have to make the distinction between the definition of an object - its class - and a realization of an object - an instance of the object. 

This is another subtle idea, perhaps the most subtle we have encountered so far but once you get it then everything seems much simpler and much more logical. 

The idea is that an object needs something to define it - i.e. what properties, methods and events it offers to the outside world. This definition is called its class. Once you have a class that defines an object you can use the class to make as many "editions" or instances of the class as you want.

The class is like a rubber stamp that defines a design that you can use to stamp as many instances of the design as need on a piece of paper. 

This is a subtle idea but Swing happens to make the whole idea very easy to understand. 

For example, the button icon that you see in the tool Palette isn't actually a button. While it is in the Palette is just sits there and you can't click it or use it in any way. The button in the Palette can be thought of as the Java class that defines the button.  

You might not be able to click it but it can use it to create as many instances of a button as you care to create by dragging and dropping onto the JFrame. Each instance of the button is independent and can be customised by setting values on its properties. Each instance also has a name that distinguishes it from every other instance. When you drag-and-drop a component the name is automatically generated as are many of its properties but you can change the name to something else if it helps - as long as the name is unique.

At the moment the idea that there are classes that define objects and instances that are the objects may seem like a subtle and almost unnecessary one but if you can keep it in mind it will make the transition to creating your own classes and objects much simpler.

  • A class is the definition of an object and it can be used to make as many instances of the object as you require.

So the basic idea of constructing a UI and a program with Swing and NetBeans is that you use the Tool Palette to place components on the JFrame. You then write Java code to make things happen - respond to events and change the objects properties and use object methods.

The designer gives you the layout of the UI and the code that you write makes it all work to do something. 

Let's see how this works in a simple example. 

A game scoreboard

Armed with the ideas of components as object instances we can easily make a scoreboard for simple game.

What we are going to do is provide two buttons - one which makes the progress bar increase and one which makes it decrease each time it is clicked. The game is for one player to get the bar all the way to the right and the other to the left by clicking. As only one player can click at a time - there is only one mouse - this results in a sort of scoreboard.

  • Place a Progress bar and two buttons on the JFrame.
  • Position them to make a symmetric layout - see below.

layout

 

The designer will help you position them by displaying all sorts of alignments between components as you move them around. 

  • Next change the label property of the left-hand button to plus and the label property of the right-hand button to minus.

There is usually more than one way to do a job in NetBeans. To change the label property you can either use the Properties window or you can right click on the button and then select Edit text.

  • Finally set the value property of the ProgressBar to 50 - you can only do this using the Property Window.

Next we have to add some code that changes the value properties of the ProgressBar when the buttons are clicked. To add a click event handler simply double click on the button that you want to add it to.

For example, double clicking on the lefthand button takes you to the code editor where you will see the generated code:

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

 

All we have to do is to write some Java code to replace the TODO line that adds one to the value property of the ProgressBar. This is slightly more complicated than you might expect but it provides an example of using methods.

The ProgressBar has two methods related to the value property: getValue and setValue. If you write

jProgressBar1.setValue(10);

then the ProgressBar's value property is set to 10. Similarly the instruction:

jProgressBar1.getValue();

returns the current setting of the value.

Notice the empty brackets a the end of the getValue method - methods always have to have brackets.

As jProgressBar1.getValue() gets the current value:

jProgressBar1.getValue()+1;

is one more than the current value and so:

jProgressBar1.setValue(jProgressBar1.getValue()+1);

sets the  value to one more than it currently is and this is the instruction we need in the left-hand button's event handler:

private void jButton1ActionPerformed(
              java.awt.event.ActionEvent evt) {
 jProgressBar1.setValue(jProgressBar1.getValue()+1);
}

The righthand button's event handler is just as easy to create. G

o back to the designer and double click on the righthand button to generate its click event handler and change the TODO line so that it reads:

private void jButton2ActionPerformed(
               java.awt.event.ActionEvent evt) {
 jProgressBar1.setValue(jProgressBar1.getValue()-1);
}

 

Now if you run the program you will, be asked to set the main class i.e. where the program starts and in this case the answer is MyJFrame. After this you will see the window with the ProgressBar and the two buttons. Clicking on the buttons will move the ProgressBar to the right or the left.

 

run

 

This isn't an amazing application - although if you were targeting a mobile phone or similar device you do find apps that are this simple making money!

The point is that this is a very simple Java program with three components and a small amount of code. As you learn more Java you can see the potential to do more interesting things.

Next time: we look at some basic Java instructions and get started with real programming.

 

Modern Java
With NetBeans And Swing

covericon

Contents

  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. 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, FacebookGoogle+ or Linkedin

 

 
 
 

blog comments powered by Disqus

 

<ASIN:0072263849>

<ASIN:0596009208>

<ASIN:0132354799>

<ASIN:1430234164>

<ASIN:0596009070>



Last Updated ( Thursday, 09 June 2016 )
 
 

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