Page 1 of 1

Learning Swing Part I Rate Topic: ***** 5 Votes

#1 jinnyishere  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 127
  • Joined: 06-July 09

Posted 13 September 2009 - 09:55 PM

*
POPULAR

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.

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

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

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

}

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



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



Posted 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"

Is This A Good Question/Topic? 20
  • +

Replies To: Learning Swing Part I

#2 jinnyishere  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 127
  • Joined: 06-July 09

Posted 04 October 2009 - 10: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...
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Posted 04 October 2009 - 08:40 PM

View Postjinnyishere, on 4 Oct, 2009 - 09:06 AM, said:

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
Was This Post Helpful? 0
  • +
  • -

#4 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2699
  • View blog
  • Posts: 15,736
  • Joined: 24-September 08

Posted 05 October 2009 - 09:28 AM

Why do we need 10 parts explaining something that could be explained in like 3?
Was This Post Helpful? 0
  • +
  • -

#5 jinnyishere  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 127
  • Joined: 06-July 09

Posted 05 October 2009 - 08:16 PM

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.....
Was This Post Helpful? 0
  • +
  • -

#6 Aldahbra  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 02-January 10

Posted 02 January 2010 - 03:14 PM

View Postjinnyishere, on 4 Oct, 2009 - 09:06 AM, said:

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


I think it is good as it is, thanks.

I'm struggling to learn Java and keep finding that text books and sites don't break things down to the basic level for begginners. Don't shorten it, it explains everything

:D
Was This Post Helpful? 1
  • +
  • -

#7 Guest_rocsmak*


Reputation:

Posted 17 February 2010 - 03:27 PM


Was This Post Helpful? -1

#8 rocsmak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-February 10

Posted 17 February 2010 - 03:42 PM

View PostAldahbra, on 02 January 2010 - 02:14 PM, said:

View Postjinnyishere, on 4 Oct, 2009 - 09:06 AM, said:

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


I think it is good as it is, thanks.

I'm struggling to learn Java and keep finding that text books and sites don't break things down to the basic level for begginners. Don't shorten it, it explains everything

:D
I agree that you should not shorten your tutorial. It seems to me that some out there say they want to help, but if you are not a programming genius already then don't bother trying to explain something to someone because they should know already. I don't fall into the long explanation category, but I do enjoy the small details that some may leave out because you should know that already.I think Dream In Code is great and I hope to learn a few things when I visit.
Was This Post Helpful? 0
  • +
  • -

#9 DADDYCARDONA  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 04-July 10

Posted 07 November 2010 - 02:40 PM

View Postjinnyishere, on 04 October 2009 - 09:06 AM, said:

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

I think it is very helpful, I am starting to understand the swing alittle better as to my book I did not understand at all.
Was This Post Helpful? 0
  • +
  • -

#10 DeeGzNuTz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 13-December 10

Posted 13 December 2010 - 02:29 PM

One of the things I liked the most about your tutorial is the conversion factor used. It was given with a great example and produced a better understanding for myself. Haven't seen that before or read it in my book yet. Thanks a lot.
Was This Post Helpful? 0
  • +
  • -

#11 mogallin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 21-August 10

Posted 14 January 2011 - 12:13 PM

I have only read this part so far but I found it easy to understand (my only programming experience being with C++) and I liked the fact that your first example was only a couple of lines of code that would compile and run some sort of program. :^:

What i'm missing so far, for the sake of convenience, is a link to part II
Was This Post Helpful? 0
  • +
  • -

#12 jinnyishere  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 127
  • Joined: 06-July 09

Posted 08 October 2011 - 08:38 PM

View Postmogallin, on 14 January 2011 - 01:13 PM, said:

I have only read this part so far but I found it easy to understand (my only programming experience being with C++) and I liked the fact that your first example was only a couple of lines of code that would compile and run some sort of program. :^:

What i'm missing so far, for the sake of convenience, is a link to part II


Here is all the tutorials I have on the site,

http://www.dreaminco...torial-part-ii/

http://www.dreaminco...orial-part-iii/

http://www.dreaminco...torial-part-iv/

http://www.dreaminco...ing-tutorial-v/


~Thank You
Enjoy
Was This Post Helpful? 1
  • +
  • -

#13 curryjl  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 16
  • Joined: 02-March 09

Posted 08 February 2012 - 04:36 PM

I enjoyed the read and learned something from it. Good job!
Was This Post Helpful? 0
  • +
  • -

#14 jinnyishere  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 127
  • Joined: 06-July 09

Posted 04 May 2012 - 09:57 AM

Thank You !
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1