Home Here you can view your subscribed threads, work with private messages and edit your profile and preferences Registration is free! Frequently Asked Questions Search

HardwareDude.com Forum Index -> 3rd Party Software Disucssion

Get your GUI Swinging!


Post new topic   Reply to topic

  Author    Thread
sri
Moderator
Moderator


Joined: 28 Jan 2006
Posts: 381
Location: Hyderabad , India


 Reply with quote  
Get your GUI Swinging!

Get your GUI Swinging!
What is Swing?
With the release of the Java 2 platform (and the Java Development Kit 1.2), developers now have access to a wider range of graphical user interface components, and greater flexibility and control over their appearance. The previous graphics toolkit, the Abstract Windowing Toolkit (AWT), was sufficient for simple applets, but was a poor substitute for a commercial quality suite of components. As part of the Java Foundation Classes (JFC), the Swing library of components rectifies the shortcomings of AWT, and gives developers the ability to create professional graphical user interfaces that will be the envy of developers using other languages like Visual Basic or Delphi.
The Swing API isn't a replacement for the AWT. Swing is designed to be used in conjunction with existing AWT components, and to complement it. Not only does Swing offer new components, but it also offers greater control over their appearance, and a customizable "look and feel" (L&F), that allows applications to swing from a "Java look and feel" to a "Windows look and feel" on-the-fly. Developers even have the ability to create their own L&F, for custom applications or games
Getting started with Swing
Developing applications with Swing user-interfaces is actually quite easy. You can even convert your existing applications over to use the new Swing components. In this tutorial, we'll show how to get your applications swinging
Learning about Swing
The best way to learn about Swing is to actually use an application that has a Swing GUI. A good starting place is the SwingSet demo, that is shipped with JDK1.2

If you don't already have JDK1.2 installed, you'll need to install it to work with Swing applications. The latest version of JDK1.2 can be found at http://java.sun.com/
Running the SwingSet Demo
The demo can be found in the demo/jfc/SwingSet directory, located under your JDK1.2 installation directory. From a DOS Prompt or shell window, change to this directory then run the SwingSet application.
c:\>cd \jdk1.2\demo\jfc\SwingSet
c:\jdk1.2\demo\jfc\SwingSet>java SwingSet
The first thing you should notice is that the demonstration takes awhile to load - a window with a progress bar will be shown as the demonstration loads. The progress bar component is a nice way of letting users know that a slow application is still chugging along, and hasn't fallen over Wink
Once the demonstration loads, you can then click on different tab buttons to view the various components and behaviors that make up the Swing API. Take some time to experiment and click on some of the components - you'll be pleasantly surprised at the functionality that's available.
While you have the SwingSet demo running, take a look at the menu options. You can now trigger a menu using only the keyboard (e.g. ALT-F for the File menu). Take a look at the Options menu, and the radio buttons provided. These allow you to dynamically change the look-and-feel of components at run-time.

Switch between the Java L&F (also referred to as 'Metal'), and the Motif/Windows L&F. You'll notice that all of the components (including menus) change appearance. This is a very powerful feature of Swing, and gives users control over which GUI style is used. Personally, I think that the Metal L&F is very comfortable to use, though end-users may prefer to stick with Windows L&F.
Once you've had a play around with Swing, you'll be reluctant to go back to the older AWT components. Remember however that only the Java 2 Platform supports Swing (though older Java 1.1 Virtual Machines can still run Swing if you include the components in the codebase of your application). For the moment at least, it rules out developing Swing applets, unless the Java Plug-in is used to run them under a Java 2 Virtual Machine
Developing your first Swing application
Using the Swing API in your own applications is quite straightforward, as is converting existing applications to Swing. The first thing that all Swing applications must do is import the Swing packages. Beta versions of JDK1.2 used a different package location, but the official release stores most of the Swing related packages under the javax.* extensions hierarchy
// Import AWT classes
import java.awt.*;
// Import AWT event classes
import java.awt.event.*;
// Import Swing classes
import javax.swing.*;
Note that there are many more Swing packages, covering very specific areas of functionality. As your application uses more and more of the functionality Swing offers, you'll need to add more packages. For the moment, however, only javax.swing is required.
Advice when using Swing
Here are a few rules of thumb, that will assist you when using Swing :-
> Every AWT component (eg java.awt.Button), has a corresponding Swing component. Just at J to the beginning (Button -> JButton).
> Adding components to a JFrame is slightly different to a Frame. You must add your components to JFrame.getContentPane().
Using these rules, it's quite straightforward to convert an existing application to Swing, or to design one from scratch. So let's take a look at a simple application, that displays a small frame (centered in the middle of the screen). The frame will display a label, as well as a button that closes the window. We'll take a look at the AWT version first, and compare it to the Swing version.
Writing the AWT version
AWTFrame.java
import java.awt.*;
import java.awt.event.*;
public class AWTFrame
{
public static void main (String args[]) throws Exception
{
Frame frame = new Frame("AWT Demo");
Label label = new Label ("AWT Version");
Button button = new Button ("Click to close");
button.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent evt)
{
System.exit(0);
}
});
// Set layout manager
frame.setLayout( new FlowLayout() );
// Add to frame
frame.add( label );
frame.add( button );
frame.pack();
// Center the frame
Toolkit toolkit = Toolkit.getDefaultToolkit();
// Get the current screen size
Dimension scrnsize = toolkit.getScreenSize();
// Get the frame size
Dimension framesize= frame.getSize();
// Set X,Y location
frame.setLocation ( (int) (scrnsize.getWidth()
- frame.getWidth() ) / 2 ,
(int) (scrnsize.getHeight()
- frame.getHeight()) / 2);
frame.setVisible(true);
}
}
Writing the Swing version

SwingFrame.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingFrame
{
public static void main (String args[])
throws Exception
{
JFrame frame = new JFrame("Swing Demo");
JLabel label = new JLabel("Swing Version");
JButton button = new JButton ("Click to close");
button.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent evt)
{
System.exit(0);
}
});
// Get content pane
Container pane = frame.getContentPane();
// Set layout manager
pane.setLayout( new FlowLayout() );
// Add to pane
pane.add( label );
pane.add( button );
frame.pack();
// Center the frame
Toolkit toolkit = Toolkit.getDefaultToolkit();
// Get the current screen size
Dimension scrnsize = toolkit.getScreenSize();
// Get the frame size
Dimension framesize= frame.getSize();
// Set X,Y location
frame.setLocation ( (int) (scrnsize.getWidth()
- frame.getWidth() ) / 2 ,
(int) (scrnsize.getHeight()
- frame.getHeight()) / 2);
frame.setVisible(true);
}
}
Wrapping it all up
swing vs AWT :
Swing offers developers substantial advantages over the existing AWT package. Not only are there a much wider range of components, but developers (and users) have much greater control over how they appear. A button, for example, can contain an images as well as a label. A frame can have a special border around it. There are plenty of great reasons to migrate your AWT applications to Swing, or to develop future projects with a Swing GUI
However, remember that older Java Virtual Machines do not include the Swing API - and this includes most web browsers at the moment. You'll also need to include Swing with your applications if you plan to offer JDK1.1 compatible versions of your software.

- srikanth dhanwada

Post Thu Feb 09, 2006 12:04 am 
 
  Display posts from previous:      



Post new topic   Reply to topic
Page 1 of 1



Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 



-Your Link Here-
Networking hardware and software solutions | web hosting directory | Web hosting and domain talk
Powered by phpBB: © 2008 HardwareDude.com | SiteMap |Privacy | Terms of Service
Powered By :