HOME
J2ee tutorials
Java introduction
Java basic
Java installation
Java Packages Learn
Applets Java Threads Java Gui component

Java Event Handaling Java Streams and Files
 Java Swings Java JDBC
 Java Network Programming Java RMI
 Java Servlets Java javabeans
 EJB Struts

|
Java Swing Tutorials
One of the major improvements between JDK 1.2 AND 1.2 is that SWING API in JDK 1.2 gives a better and richer look and feel to GUI components than was available in java 1.0 by using the abstract window toolkit(AWT). The primary problem with the programs developed using AWT was that the look and feel of the interface was not consistent across platforms. Moreover the components available in AWT were based least common demonstrates denominator amongst those present in all platform. The AWT did provide a set of primitive user interface components such as buttons, labels and test fields.
What is abstract windowing toolkit (AWT)?
It provides the capability to create platform-independent, GUI based program and is very important contribution to java’s popularity.
AWT is not only a better API for developing windows application, but also a better API for programming window based applications on platform ranging
form motif to OS/2
Swing
Swing is the major component that is the part of the JFC. Swing provides many types of GUI components, providing 100% pure java implementations of these components, and gives the capability to change the appearance and behavior of these components on different platforms. Although swing components are implemented in terms of the underlying AWT, these components do not use AWT components. In fact all the traditional AWT components are re implemented as swing components.
With the use of model view architecture (MVC) which is implemented in swing, you can change the; look and feel of your application. For example, you can have your application designed or running on Microsoft windows to have a look as if it is running on the UNIX windows platform. This feature of swing is known as pluggable look and feel (PL&F).swing PL&F architecture makes it easy to customize.
Understanding swing package
Swing is a large API that consists of 9 different packages that provides numerous classes and interface under the javax.swing package.
Swing components are lightweight visual components that facilitate efficient graphical user interface development. Swing components offer a number of
advantages like:
Pluggable look and feel
MVC architecture
Wide variety components
Compound borders
Nested containers
Jframe
The jframe class is an extension to the AWT Frame class. An instance of the frame class is a heavyweight component. it creates a top level window that can be positioned and sized independently of other windows. The jframe instance is managed by the system window manager.
To create an instance of a JFrame class we write
JFrame frame=new JFrame(“new frame”);
Frame.setSize(300,300);
Frame.setVisible(true);
JInternalFrame
This class provides the lightweight container that provides many of the features of a native frame, including dragging, closing, becoming an icon, resizing, title display and support for a menu bar.
JApplet
This is an extended version of java.applet.Applet. Applet can create instance of the JFrame class to be displayed outside the browser if desired.
Buttons components
JButton
The most common button class for normal use is the JButton class. The class provides the user interface components for familiar push button
To create button
Write
JButton button=new JButton();
To create label with button
JButton button=new JButton(“submit”);
JCheckBox
An implementation of a checkbox ---an item that can be selected or deselected, and which displays its state to the user
To create an unselected checkbox with no text, no icon
JCheckBox check=new JCheckBox();
To create initially unselected checkbox with an icon
JCheckBox check=new JCheckBox(Icon);
JRadioButtons
This is an implementation of a radio button—an item that can be selected or deselected, and which displays its stats to the user
To create an unselected radio buttons with no set text, use;
JRadioButton radio=new JRadioButton();
JToolBar
Swing provides the JToolBar class to facilitate adding toolbars to your application or applet. The JToolBar class implements a floating toolbar that can be dragged off the original frame into its own frame. Toolbar provides a component that is useful for displaying commonly used actions or controls.
JToolBar tool=new JToolBar();
Text components
In java two components are used to get user input and edit text those are
Textfields(this field takes only one line of input)
Textareas(accept multiple lines of input)
JTextField
The usual way to add a text field to a window is to add it to a panel or some other containers
JPanel panel=new JPanel()
JTextField text=new JTextField(“Defult input”,20)l
Panel.add(text);
JTextAarea
The JTextArea componets is used for multiple line text.the user can enter any number of lines of the text
Example
JTextArea textarea=new JTextArea(8,30);
The above are some components. There are many components like
JList
JComboBox
JMenuBar
JSeprator
setBorder() getBorder()
but not explained in this tutorials
A simple swing components with applets |
JAppletDemo.java
/*applet code="JAppletDemo.class" Width=500 Height=500>
</applet>
*/
import java.awt.*;
import javax.swing.*;
public class JAppletDemo extends JApplet
{
public void init()
{
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
JButton b1=new JButton("Click");
getContentPane().add(b1);
JCheckBox cb1=new JCheckBox("Item1");
JCheckBox cb2=new JCheckBox("Item2");
JCheckBox cb3=new JCheckBox("Item3");
cp.add(cb1);
cp.add(cb2);
cp.add(cb3);
JRadioButton rb1=new JRadioButton("Red");
JRadioButton rb2=new JRadioButton("Blue");
JRadioButton rb3=new JRadioButton("Green");
cp.add(rb1);
cp.add(rb2);
cp.add(rb3);
JTextArea ta=new JTextArea(10,30);
cp.add(ta);
JLabel l1=new JLabel("Enter your name");
cp.add(l1);
JTextField tf=new JTextField(10);
cp.add(tf);
JComboBox cob=new JComboBox();
cob.addItem("Vegetables");
cob.addItem("Fruits");
cob.addItem("Snacks");
cob.addItem("Pasteries");
cp.add(cob);
}
}
| example-2
JFileChooserDemo.java
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class JFileChooserDemo extends JFrame implements ActionListener
{
JFileChooser jfc;
JButton b1;
JTextArea ta;
public JFileChooserDemo()
{
ta=new JTextArea(25,25);
b1=new JButton("click");
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
cp.add(b1);
cp.add(ta);
jfc=new JFileChooser();
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
jfc.showOpenDialog(this);
try
{
File f=new File(jfc.getSelectedFile().getAbsolutePath());
BufferedReader br=new BufferedReader(new FileReader(f));
String cf=null;
String line;
while((line=br.readLine())!=null)
cf+=line+"\n";
ta.setText(cf);
}
catch(IOException e)
{}
}
public static void main(String args[])
{
JFileChooserDemo jf=new JFileChooserDemo();
jf.setSize(400,400);
jf.setVisible(true);
}
}
| example-3
JColorChooserDemo.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class JColorChooserDemo extends JFrame
{
JColorChooser jc;
JTextField tf;
public JColorChooserDemo()
{
Container cp=getContentPane();
cp.setLayout(new BorderLayout());
jc=new JColorChooser();
tf=new JTextField(10);
cp.add(jc,"Center");
cp.add(tf,"South");
addActionListener(new AL());
}
public class AL implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
tf.setBackground(jc.getColor());
}
}
public static void main(String args[])
{
JColorChooserDemo jd=new JColorChooserDemo();
jd.setSize(400,400);
jd.setVisible(true);
}
}
|
|
Inform Friend About This Site |