Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to interact with the code in Java Swing

The tutorial relates to the ability of the Java Swing to interact with the code and then create applications with an attractive GUI.

All the programs that we normally write are console based. Hence, it gets boring and the users can only interact with the program only if they've something to compile the codes. Thus, this is where swing comes in to play, swing allows the user to interact with the program better and more playful. So what is swing ? Swing is a package that create applications which use a flashy Graphical User Interface ( or GUI ), pronounce as " gooey ." In English, simply all the application that we are using such as, Jgrasp compiler, Media player, Microsoft Word, and so on. Swing API provides many different classes for creating various types of user-interface elements. We will look at three of those classes: JFrame, JLabel, and JPanel. The swing class has more than that, lets see a brief description of the classes.

  • Object: All classes derive from Object.
  • Component: The component class represents an object that has visual representation that can be shown on-screen and can interact with users.
  • This is in the AWT class rather than the Swing class.
  • Container: It builds on the basic visual capabilities of the Component class by adding the ability to hold other containers. This is in the AWT class.
  • Window: It defines a window, a specialized type of container object that has a border, a title bar, and buttons that minimize, maximize, and close.
  • Frame: A type of window that serves as the basis for Java GUI applications. It's also in the AWT class.
  • JFrame: It's an older Frame class.
  • JComponent: It's a swing class that is the basis for all other swing components except for frames.
  • JPanel: It creates panels, which are containers used to organize and control the layout of the other components such as lables, buttons, textfield and so on.
  • JLabel: The class creates a label that displays a simple text value.

At the moment, you might not have a clue of what all those are, but keep in mind of the descriptions as we move along in the chapter it will be more clear. Lets see a very simple graphic user interface (GUI) using JOptionPane.

Listing1: Code displaying the GUI using JOptionPane

/*
	    This is a simple GUI using JOptionPane
	*/
	import javax.swing.*;
	public class Name
	{
	    public static void main(String[] args)
	    {
	        // Ask for a name
	         
	        String input = JOptionPane.showInputDialog("Enter your name");
	        // Outputs the input name
	        JOptionPane.showMessageDialog(null, "The name you enter is " + input);
	    }
	}

To get an input from the user, we use JOptionPane.showInputDialog(" " ); and to output to the screen we use JOptionPane.showMessageDialog(null, " "); so know the difference, and when outputting don't forget to put the null. The inputs from swing is a String, so if we want to do calculations we would have to convert the string into number, thus the Java API gives us a great method to do so.

Integer.parseInt(" ") -- Converts String into an integer

Double.parseDouble(" ") -- Converts String into a double

Float.pareseFloat(" ") -- Converts String into a float

Lets see a example on how to use them.

The program demonstrate how to convert Strings into Numbers

Listing2: Code displaying the process to convert Strings into Numbers

*
*/
import javax.swing.*;
public class ConvertNum
{
    public static void main(String[] args)
        // Gets the first number
	        String firstnum = JOptionPane.showInputDialog("Enter a number");
        // Gets the second number
        String secnum = JOptionPane.showInputDialog("Enter another number");
         
        double sum = 0.0;

        // Convert the Strings into a Double
        double num1 = Double.parseDouble(firstnum);
        double num2 = Double.parseDouble(secnum);
        // Sums up the number
        sum = num1 + num2;
        // Output the sum into the screen
        JOptionPane.showMessageDialog(null, "The sum of the two number is " + sum);
    }
}

Now, lets get into a different kinds of swing. A window type of swing application. To help us understand the swing world better, I created a method and constructor table of swing classes in the link below. As we move along, it would be best if you first read the swing method and constructor table first and try to remember what each methods/constructors do.

Recall from the inheritance, once we extends a class, we are able to use any methods that belong to the other class. It is the same thing with swing classes, if we extend any of the classes we are able to use their methods as if it were its own. It might be a little confusing from just reading it, hence lets see a example of it.

/*
    The program demonstrates the use of JFrame
    It sets the title to Hello World
	*
	*/
import javax.swing.*; // Needed for the swing class
public class HelloWorld extends JFrame
{
    public HelloWorld()
    {
        // Sets the size of the window
        setSize(300, 200);
        // Sets the title of the window
        setTitle("This is a Hello World Frame");
        // Sets the action when the user clicks on the X of the window
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // Sets the window to be visible
        setVisible(true);
    }
    public static void main(String[] args)
    {
        new HelloWorld();
    }
}

The above program is a very simple swing program with a title set to " This is Hello World Frame ." Since swing applications are still java applications, and all java applications need a static main method that starts the application. Hence, in order for the window to display we would need to add a main method. The above method used the inheritance style, we inherits the method from the JFrame class, thus we are allow to use all the method inside the class. Notice, the name must be the same as the class name that we created. If we look back at the program above, we have the class name as HelloWorld, follow with another method with HelloWorld. The two names has to match or else the compiler would give out an error. Then inside the main method, all we need is an instance of the class.

Conclusion

We learned on the potential of Java Swings to interact with the web programs and how it can come up with a flashy and attractive Graphical User Interface.



Software developer with more than 5 years of development on Java, HTML, CSS.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login