Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 307,212 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,572 people online right now. Registration is fast and FREE... Join Now!




Learning Swing Part I

 
Reply to this topicStart new topic

> Learning Swing Part I

jinnyishere
Group Icon



post 13 Sep, 2009 - 08:55 PM
Post #1


What is Swing?

So far, all the programs we've writing 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.

CODE
/*
    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);
        
    }
}

IPB Image

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.

CODE
/*
    The program demonstrate how 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);
        
    }

}

IPB Image

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.



CODE
/*
    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();
    }
}


IPB Image

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.
"To Be Continued"
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

jinnyishere
Group Icon



post 4 Oct, 2009 - 09:06 AM
Post #2
Please!! Do leave some feedbacks so i know what to improve...

is it confusing, not clear enough, needs more examples and so on..

open for any suggestions...
Go to the top of the page
+Quote Post

pbl
Group Icon



post 4 Oct, 2009 - 07:40 PM
Post #3
QUOTE(jinnyishere @ 4 Oct, 2009 - 09:06 AM) *

Please!! Do leave some feedbacks so i know what to improve...

is it confusing, not clear enough, needs more examples and so on..

open for any suggestions...

Good show but a few flaws

* 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.

Completly innacurate... Windows do not have title bar and buttons. Frame which are Windows with title bar and buttons

* Frame: are Windows with Title bar and buttons

* JFrame: It's an older Frame class.
completly false
Jxxxx components were invented to correct a lot of flaws in AWT components
JFrame extends, with much more functionnalities Frame as most Jxxxx extends in a better way their xxxx AWT counterpart
Go to the top of the page
+Quote Post

NeoTifa
Group Icon



post 5 Oct, 2009 - 08:28 AM
Post #4
Why do we need 10 parts explaining something that could be explained in like 3?
Go to the top of the page
+Quote Post

jinnyishere
Group Icon



post 5 Oct, 2009 - 07:16 PM
Post #5
k thx...gotcha...pbl

Though it's weird the book I learn it from is only a little more than year ago and that is what it says on the book. Our professor didn't said anything about it either...

10 parts haha not even hehe..but yea shorten it out would be a good idea.....
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 09:48PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month