MQH Blog

Three Days with Java: Day 2

Introduction

user

Mohammed Q. Hussain

A Programmer.


Featured

Java

Three Days with Java: Day 2

Posted by Mohammed Q. Hussain on .

Here we continue what we have started in the previous post “Three Days with Java: Day 1”.

The Initial Position of Swing Application Window

Because I already wrote applications for GTK I have the habit of showing the main window of the application when the user starts it in a position which is close to the mouse pointer and I wanted to implement that with Swing.

We could do that by knowing the current position of the mouse then passing this position to the method which sets the position of the window. The class MoustInfo provides us a method called “getLocation”; by using it we could get the current location of the mouse pointer as the following:

MouseInfo.getPointerInfo().getLocation()

After that we could pass this location to the method setLocation which is available in our window’s object (which should be of type JFrame). Example:

JFrame mainWin = new JFrame( "Three Days with Java" );

// ... //

mainWin.setSize( 600, 600 );
mainWin.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

// Shows the window near to the mouse cursor
mainWin.setLocation( MouseInfo.getPointerInfo().getLocation() );

Right-to-Left Languages and Swing

If you application’s interface has been written in right-to-left language such as Arabic it’s probably a good idea to change the direction of the text for users’ convenience. In Swing there is a method called “setComponentOrientation” which is available in the class Component to do that. Let’s see the following example:

JPanel paneName = new JPanel();
paneName.setComponentOrientation( ComponentOrientation.RIGHT_TO_LEFT );
paneName.setBorder( BorderFactory.createTitledBorder( "Right-to-Left Text" ));

Note the second line, we passed a parameter which indicate the that JPanel which we just created has right-to-left direction; due this line the JPanel itself and the text “Right-to-Left Text” will be shown from right to left. If we remove the second line the direction will back to the default one.

Antoher example:

JTextField textName = new JTextField( 25 );
textName.setComponentOrientation( ComponentOrientation.RIGHT_TO_LEFT );

In this example we set the direction of a text field using the same method setComponentOrientation; when the user write some input in this text field the direction of the input will be from right to left and that will provide better user experience if he/she is going to write a text in a right-to-left language.

Swing’s JList

Let’s now discuss some classes which provided by Swing. I’ll start with JList which provides us a way to display a list for the user. In my application I used it to display a list of options for the user so he/she could select an item from the list and according to the selected item the application decides how to insert the data to database.

The use of JList is too simple. You just need to build an array of String which has the available items then pass this array to the constructor of JList. Example:

JList UnitsList = new JList( new String[] { "a", "b", "c", "d", "e" } );

win.add( UnitsList );

First we created an object of JList - which is stored in the variable UnitsList - and we passed an array of String which contains items from “a” to “e” as a parameter to the constructor of JList. After that we added this list to the window - which is represented by the object “win” and the example assumes that the type of this object is JFrame -.

To get the value which has been selected by the user we could use the method getSelectedValue which is provided by the class JList; it returns the value itself and not the index of the selected value. We could also use the method setSelectedIndex to indicate that some item on the list has been selected; we could use it for instance to automatically select the default item from the list then the user has the freedom to change the default selected value. Let’s assume that we want to set the item “c” in our previous example as the default item when the user runs the window which has our JList. The following line does that:

UnitsList.setSelectedIndex( 2 );

You can see that we passed 2 as a parameter; this value represents the index of the item; the indices in JList starts with 0 which means that the item “c” in our list has the index 2 and not 3.

Swing’s JScrollPane

In the JList’s part of this post we built a list with a specific number of items and when we put it in our window we knew exactly what is the best height to be set for our list to show it perfectly in the UI. Let’s assume that we need to build a dynamic list; that is we know nothing about the number of items which reside inside it. Because the height of our list in the UI must be static and our list’s items is dynamic; at some point the number of items will be big enough to not fit in the UI due the static height which decided statically. To handle this issue we need to set a scrollbar for our list in the UI and after that we could set a static height value for the list and add whatever number of items in it without worrying if all items will be shown for the user or not. Once the number of items be too large to be fit in the specified height the scrollbar will be enabled and the user could use it to navigate all available items in the list.

The class JScrollPane can be used to add scrollbars for Swing’s components. It’s simple to use; what we should do to use it is creating a new object of it and passing the object of the component - which we which to enable the scrollbar for it - as a parameter to the constructor then adding the object of JScrollPane in the interface - instead of the original component itself -. Examples are always better :-)

JFrame win = new JFrame( "Three Days with Java" );

win.setSize( 500, 200 );
win.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

// ... //

JList alpha = new JList( new String[] { "a", "b", "c", "d", "e", "h", "i",
				"j", "k", "l", "m", "n", "o", "p",
				"q", "r", "s", "t", "u", "v", "w",
				"x", "y", "z" } );

JScrollPane scrollPane = new JScrollPane( alpha );

win.add( scrollPane );

// ... //

win.setVisible( true );

If you run this example you will see that the window is too small to fit the whole items of the list; and due that a scrollbar will be shown on the list so you could navigate all items. You can run the following example which is exactly same as the current but doesn’t use JScrollPane:

JFrame win = new JFrame( "Three Days with Javaا" );

win.setSize( 500, 200 );
win.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

// ... //

JList alpha = new JList( new String[] { "a", "b", "c", "d", "e", "h", "i",
				"j", "k", "l", "m", "n", "o", "p",
				"q", "r", "s", "t", "u", "v", "w",
				"x", "y", "z" } );

win.add( alpha );

// ... //

win.setVisible( true );

And you tell me the difference :-)

JScrollPane can be used with any other Swing’s component and not only with JList. Let’s see the following example which uses JScrollPane with a text area:

JFrame win = new JFrame( "Three Days with Java" );

win.setSize( 200, 100 );
win.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

// ... //

JTextArea txt = new JTextArea();

JScrollPane scrollPane = new JScrollPane( txt );

win.add( scrollPane );

// ... //

win.setVisible( true );

Run the example and try to write text with multiple lines; at some point the scrollbar will be enabled to scroll you lines. In the following example the use of JScrollPane has been eliminated:

JFrame win = new JFrame( "Three Days with Java" );

win.setSize( 200, 100 );
win.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

// ... //

JTextArea txt = new JTextArea();

win.add( txt );

// ... //

win.setVisible( true );

If you run it and start to write multiple line text on the text area at some point the old lines will start to disappear and the only way to back to them again is by using the keyboard.

Swing’s JOptionPane

Sometime you need to show dialogs for the user and for that you could use JOptionPane which provides a way to show the most common types of dialog boxes.

Here I’m going to discuss the static method JOptionPane.showConfirmDialog and there are more such as showInputDialog, showMessageDialog and showOptionDialog; each method of them has a descriptive name and you maybe need to use a method other than “showConfirmDialog” for your application; you could read the official guide for more information.

The method JOptionPane.showConfirmDialog can be used to show a confirmation dialog for the user. Example:

int msg = JOptionPane.showConfirmDialog( null,
										"This identifier is already stored in database. Do you really want to update its information?",
										"Warning", JOptionPane.YES_NO_OPTION );

It’s obvious that the second parameter is the message which will be shown for the user. The third one is the title of the dialog box and the last one decides what are the buttons that will be shown for the user in this dialog box and in our case we would like to show two buttons: “Yes” and “No”.

The variable “msg” contains the response of the user which could be handled as the following:

if ( msg == JOptionPane.YES_OPTION )
	update = true;
else
	no_clicked = true;

The End. In case you have missed the first part of this post you can find it here. See you in the next part :-)

user

Mohammed Q. Hussain

http://www.maastaar.net

A Programmer.