You are on page 1of 26

Swings- An Introduction

What is JFC?
JFC Java Foundation Classes Five key pieces:
Java 1.1 Abstract Window Toolkit (AWT) Java2D API ***Swing*** Native Drag and Drop Accessibility API

AWT
AWT stands for Abstract Window ToolKit. The Abstract Window Toolkit supports GUI Java programming. It is a portable GUI library for stand-alone applications and/or applets. The Abstract Window Toolkit provides the connection between your application and the native GUI.

Why Swing Over AWT


Cons: Portability: use of native peers creates platform specific limitations. Some components may not function at all on some platforms. Third Party Development: the majority of component makers, including Borland and Sun, base new component development on Swing components. There is a much smaller set of AWT components available, thus placing the burden on the programmer to create his or her own AWT-based components. The AWT components depend on native code counterparts (called peers) to handle their functionality. Thus, these components are often called "heavyweight" components.

Swing Pros
Portability: Pure Java design provides for fewer platform specific limitations. Behavior: Pure Java design allows for a greater range of behavior for Swing components since they are not limited by the native peers that AWT uses. Features: Swing supports a wider range of features like icons and pop-up tool-tips for components.

Vendor Support: Swing development is more active. Sun puts much more energy into making Swing robust. Look and Feel: The pluggable look and feel lets you design a single set of GUI components that can automatically have the look and feel of any OS platform (Microsoft Windows, Solaris, Macintosh, etc.). It also makes it easier to make global changes to your Java programs that provide greater accessibility (like picking a hi-contrast color scheme or changing all the fonts in all dialogs, etc.).

The Swing Set


All Swing classes are derived from the AWT abstract class Container Container

Panel AWT classes Applet Dialog Swing classes

Window

ScrollPane

Frame

JComponent

JApplet JDialog

JFrame

JWindow

Heavyweight Swing components

Object

Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects implement the methods of this class. The Component class is the abstract superclass of many of the AWT classes. It represents something that has a position, a size, can be painted on the screen, and can receive input events.

Component

Some Methods Defined in Class Component: setForeground(Color) set the components foreground color setBackground(Color) set the components background color setFont(Font) set the components font paint(Graphics) repaint( )
repaint the component schedule the component for repainting

addxxxListener(xxxListener)

add an xxx listener for the component (where xxx is Component, Focus, Key, Mouse, or MouseMotion)

An xxxListener is an object that waits for an event to occur, then executes certain actions in response to the event.

Swing Components
Swing classes

JComponent

JApplet JDialog

JFrame

JWindow

Heavyweight Swing components

JLabel JPanel JTextComponent JComboBox JScrollBar AbstractButton

JList

JScrollPane

others

JTextField JTextArea

JEditorPane

JMenuItem JButton

JToggleButton

JCheckBox JRadioButton

More Components
FontChooser JColorChooser JDesktopIcon JDirectoryPane
JFileChooser

JImagePreviewer JInternalFrame JLayeredPane


JDesktopPane

JOptionPane JProgressBar
10

JRootPane JSeparator JSlider JSplitPane JTabbedPane JTable JToolBar JToolTip JTree JViewport

Layout Managers
Each container has a layout manager, which controls the way the components are positioned in the container. One of the advantages of using layout managers is that there is no need for absolute coordinates where each component is to be placed or the dimensions of the component. The layout manager automatically handles the calculation of these. Programmer only specifies relative positions of the components within the container. There are many different layout managers, this presentation describes the most popular ones. Different layout managers can be used interchangeably and one inside the other. Whenever the dimensions of the container change (e.g. user resizes the window), layout manager recalculates the absolute coordinates of components for them to fit the new container size.

Types of Layout Manager


Border Layout Box Layout Flow Layout Grid Layout GridBag Layout Card Layout

Border Layout
The Border Layout manager arranges components into five regions: North, South, East, West, and Center. Components in the North and South are set to their natural heights and horizontally stretched to fill the entire width of the container. Components in the East and West are set to their natural widths and stretched vertically to fill the entire width of the container. The Center component fills the space left in the center of the container.

import java.awt.*; import java.applet.*; import javax.swing.*; public class BorderLayoutExample2 extends Jframe { public static void main(String[] args) { BorderLayoutExample2 frame = new BorderLayoutExample2(); Frame.doSomething(); } void doSomething() { Container c = getContentPane(); c.setLayout(new BorderLayout()); c.add(new JButton("One"), BorderLayout.NORTH); c.add(new JButton("Two"), BorderLayout.WEST); c.add(new JButton("Three"), BorderLayout.CENTER); c.add(new JButton("Four"), BorderLayout.EAST); c.add(new JButton("Five"), BorderLayout.SOUTH); c.add(new JButton("Six"), BorderLayout.SOUTH);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setVisible(true); } }

Output:

Flow Layout
The Flow Layout manager arranges the components left-to-right, top-to-bottom in the order they were inserted into the container. When the container is not wide enough to display all the components, the remaining components are placed in the next row, etc. Each row is centered.

import java.awt.*; import java.applet.*; import javax.swing.*; public class FlowLayoutExample extends JApplet { public void init () { getContentPane().setLayout(new FlowLayout ()); getContentPane().add(new JButton("One")); getContentPane().add(new JButton("Two")); getContentPane().add(new JButton("Three")); getContentPane().add(new JButton("Four")); getContentPane().add(new JButton("Five")); getContentPane().add(new JButton("Six")); }

Output:

Grid Layout
The Grid Layout manager lays out all the components in a rectangular grid. All the components have identical sizes, since the manager automatically stretches or compresses the components as to fill the entire space of the container.

import java.awt.*; import java.applet.*; import javax.swing.*; public class GridLayoutExample extends JApplet { public void init() { Container c = getContentPane(); c.setLayout(new GridLayout(2, 4)); c.add(new JButton("One")); c.add(new JButton("Two")); c.add(new JButton("Three")); c.add(new JButton("Four")); c.add(new JButton("Five")); } }

Output:

Calculator Program Using Swings


import java.awt.*; import java.awt.event.*; import javax.swing.*; class Awt implements ActionListener { JFrame f; JButton b1,b2,b3,b4,b5; JTextField t1,t2,t3; JLabel l,l1; Awt() { f=new JFrame("Listener"); t1=new JTextField(" "); t2=new JTextField(" "); t3=new JTextField(" ");

b1=new JButton("Add"); b2=new JButton("Sub"); b3=new JButton("Mul"); b4=new JButton("Div"); l=new JLabel(); l1=new JLabel();
} public void awt1() { f.setLayout(new GridLayout(3,2)); f.setVisible(true); f.add(t1); f.add(t2); f.add(t3); f.add(b1); f.add(b2); f.add(b3); f.add(b4);

f.add(l); f.add(l1); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); f.pack(); } public void actionPerformed(ActionEvent e) { String s=new String(e.getActionCommand()); l.setText(s); if((s).equals("Add")) { int a=Integer.parseInt(t1.getText()); int b=Integer.parseInt(t2.getText());

Integer c=a+b; t3.setText(c.toString()); } else if((s).equals("Sub")) { int a=Integer.parseInt(t1.getText()); int b=Integer.parseInt(t2.getText()); integer c=a-b; t3.setText(c.toString()); } else if((s).equals("Mul")) { int a=Integer.parseInt(t1.getText()); int b=Integer.parseInt(t2.getText()); Integer c=a*b; t3.setText(c.toString()); }

public static void main(String args[]) { Awt a=new Awt(); a.awt1(); }


}

You might also like