INTRODUCTION TO JFC (JAVA FOUNDATION CLASSES)
The earlier versions of java were released with some simple libraries. JDK1.2 was introduced with a new set of packages – the java foundation classes, or JFC – that includes an improved user interface called the swing components.
The JFC were developed, to address the shortcomings of AWT(Abstract Windowing Toolkit). The development of JFC was unique. JFC 1.2 is an extension of the AWT, not a replacement for it. The JFC visual components extend the AWT container class. The methods contained in the component and container classes that AWT programmers are familiar with are still valid for JFC visual classes.
The AWT user interface classes are now superseded by classes provided by the JFC. The support classes play an important role in JFC applications. AWT support classes , those that do not create a native window are not replaced by JFC classes.
SWING :
Swing components facilitate efficient graphical user interface (GUI) development. These components are a collection of lightweight visual components. Swing components contain a replacement for the heavyweight AWT components as well as complex user-interface components such as trees and tables.
Swing components contain a pluggable look and feel(PL&F). This allows all applications to run with the native look and feel on different platforms. PL&F allows applications to have the same behavior on various platforms. JFC contains operating systems neutral look and feel. Swing components do not contain peers. Swing components allow mixing AWT heavyweight and swing lightweight components in an application. The major difference between lightweight and heavyweight components is that lightweight components can have transparent pixels while heavyweight components are always opaque. Lightweight components can be non-regular while heavyweight components are always rectangular.
Swing components are JavaBean compliant. This allows components to be used easily in a Bean aware applications building program. The root of the majority of the swing hierarchy is the Jcomponent class. The class is an extension of the AWT container class.
Swing | AWT |
---|---|
Swing component does not need any native code to implement. | AWT component can be implementing with code. |
Swing lets you specify which look and feel your programs GUI uses. | AWT components always have the look and feel of the native platform. |
Swing components don’t have to be rectangular. For ex. Buttons can be rounded. | AWT components are always rectangular. |
The swing architecture is shown in the figure given below.
Swing components comprises of a large percentage of the JFC release. The swing component toolkit consists of over 250 pure java classes and 75 interfaces contained in about 10 packages. They are used to build lightweight user interface. Swing consists of user interface(UI) classes and non user interface classes. The non-UI classes provide services and other operations for the UI classes.
Some of the Swing packages are given below.
The JComponent class is the root of the visual component class hierarchy in the JFC. The visual components are known as the “J” classes. The functionality contained in the JComponent class is available to all the visual components contained in the JFC. The JComponent class is repository of functionality for all visual components.
The JComponent class is at the top of the hierarchy of all visual components contained in the JFC. The hierarchy is shown in the following figure.
The JApplet class is an extended version of the AWT applet class that adds support for root panes and other panes.. This class is the preferred entry point when creating applets that contain JFC components. The components are added to the ContentPane.
The constructor that can be used to create a JApplet are listed below :
Some of the methods that can be used in conjunction with the JApplet is given below :
Frame windows : A frame is a top-level window that contains a title, border, minimize and maximize buttons. JFC provides the JFrame class. This is used as a top-level-frame.
JFrame : A Swing frame is represented by the class Jframe, is an extension of the AWT Frame classes. It is the part of javax.swing package. A Swing frame is a container that functions as the main window for programs that use Swing components. An instance of the JFrame Class is a heavyweight component.
The JFrame can be created using the constructors mentioned below :
Some of the methods that may be used in conjunction with the JFrame() are listed below :
JPanel is a Swing lightweight container that is often used for grouping components within one of an applet or a frame. It can also be used to group other panels. The primary purpose of the class is to provide a concrete container for the JFC. The JPanel class is provided to gibve a concrete container class. Being an extennsion of the Jcpmponent class, JPanel is a container and inherits the features contained in that class.
The various constructros that can be used to create a JPanel are as given below.
The methods supported by this class includes :
JButton : JButtons behaves In a way that is similar to Button. It can be added to JPanel and its actions can be monitored via the ActionListener. The JButton has to be pushed to make something happen. It consist of label and /or an icon that describes its function, an empty area around the text/icon and a border. By default, the border is a special border that reflects the status of the button.
A JButton can be constructed by any of the constructors mentioned below:
Some methods can be used in conjuctoin with a JButton are listed below:Creating our own UI for JButton
Right-clicks on a Button :
The default action of a JButton is to receive a left mouse click. The button could be programmed to receive a right mouse click also. There are ways in which this can be achived.
JCheckBox : A JCheckBox is a control that may be turned on and off by the user to designate some kind of property being selected or not selected. It consist of a background rectangle, and a text string and/or icon. The JCheckBox normally shows its current state visually. This is done by placing a check mark in a box, or by changing the icon.
A JCheckbox generates item events when its state changes. The checkbox can be created by using any one of the constructors mentioned below:
JRadioButtons : This is normally used as one of a group of radio buttons of which only one may be selected at a time. These are grouped using a ButtonGroup and are usually used to select from a set of mutually exclusive options. It consists of a background rectangle and text and/or an icon. If it includes an icon, the icon is used to visually reflect the current state of the radio button.
Using the constructors listed below , radio buttons can be created:
//Programs : Followig is the programm to display an Applet. import java.awt.event.*; import java.awt.*; import javax.swing.*; /* */ public class Applets extends JApplet { JButton B1; public void init() { JPanel contentpane = (JPanel)getContentPane(); B1= new JButton("My First Applet"); contentpane.add(B1); } }
The output appers as shown in following figure :
The following program is an example of Jframe/JButton
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Button1 extends JFrame implements ActionListener { JButton mtextbtn1; JButton mtextbtn2; public Button1() { setTitle("Button Example"); JPanel contentpane = (JPanel)getContentPane(); contentpane.setLayout(new GridLayout(2,2)); mtextbtn1= new JButton("Enabled"); mtextbtn1.setMnemonic("E"); mtextbtn1.addActionListener(this); contentpane.add(mtextbtn1); mtextbtn2 = new JButton("Disabled"); mtextbtn2.setMnemonic("D"); mtextbtn2.addActionListener(this); contentpane.add(mtextbtn2); mtextbtn1.setEnabled(true); myadapter myapp = new myadapter(); addWindowListener(myapp); } class myadapter extends WindowAdapter { public void windowclosing(WindowEvent e) { System.exit(0); } } public void actionPerformed(ActionEvent e) { if (e.getSource() == mtextbtn1) { setTitle("First button clicked"); } else if ( e.getSource() == mtextbtn2) { setTitle("Second button clicked"); } } public static void main(String args[]) { Button1 b = new Button1(); b.setSize(100,100); b.setVisible(true); } }
The output appears as shown in following figure.
Example program for JCheckBoxes/JFrame.
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class checkbox1 extends JFrame implements ItemListener { JCheckBox checkbox; public checkbox1() { setTitle("Check box Example"); JPanel contentpane = (JPanel)getContentPane(); contentpane.setLayout(new GridLayout(2,2)); checkbox = new JCheckBox("Toggle"); checkbox.addItemListener(this); contentpane.add(checkbox); myadapter myapp = new myadapter(); addWindowListener(myapp); } class myadapter extends WindowAdapter { public void windowclosing(WindowEvent e) { System.exit(0); } } public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { setTitle("Checkbox selected"); } else { setTitle("Checkbox unselected"); } } public static void main(String args[]) { checkbox1 c = new checkbox1(); c.setSize(250,250); c.setVisible(true); } }
The output appears as shown in the follwing figure;
Example program for JRadioButtons
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Radiobuttons extends JFrame implements ItemListener { JRadioButton rb1, rb2; ButtonGroup grp = new ButtonGroup(); public Radiobuttons() { setTitle("Radio Buttons Example"); JPanel contentpane = (JPanel)getContentPane(); contentpane.setLayout(new FlowLayout()); rb1 = new JRadioButton("Enabled"); rb1.addItemListener(this); rb1.setEnabled(true); contentpane.add(rb1); rb2 = new JRadioButton("Disabled"); rb2.addItemListener(this); //rb2.setActionCommand("Two Activated"); contentpane.add(rb2); rb2.setEnabled(false); grp.add(rb1); grp.add(rb2); myadapter myapp = new myadapter(); addWindowListener(myapp); } class myadapter extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } public void itemStateChanged(ItemEvent e) { if (e.getSource()==rb1) { setTitle("First radio button enabled"); rb1.setEnabled(false); rb2.setEnabled(true); } else if(e.getSource()==rb2) { setTitle("Second radio button enabled"); rb1.setEnabled(true); rb2.setEnabled(false); } } public static void main(String args[]) { Radiobuttons rb = new Radiobuttons(); rb.setSize(300,300); rb.setVisible(true); } }
The output appears as shown in the following figure :
All Rights Reserved. © 2024 BookOfNetwork