Introducing Java - Swing Objects
Introducing Java - Swing Objects
Written by Alex Armstrong   
Tuesday, 19 October 2010
Article Index
Introducing Java - Swing Objects
Swing in practice


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.

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 slighlty 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


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


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


is one more than the current value and so:


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) {

The righthand button's event handler is just as easy to create. Go 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) {


Now if you run the program 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.




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 Contents

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


If you would like to be informed about new articles on I Programmer you can either follow us on Twitter, on Facebook , on Digg or you can subscribe to our weekly newsletter.







Last Updated ( Wednesday, 07 November 2012 )

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