Swings
-
Upload
balwinder-kumar -
Category
Education
-
view
612 -
download
1
description
Transcript of Swings
SWINGS
Aarti Narang
Jaskiran Kaur
Upneet Kaur
SWINGS
A swing is the primary JAVA GUI widget toolkit , an API for providing a GUI for java programs.
It was developed to provide a more sophisticated set of GUI components Other than in AWT package.
Supports more powerful and flexible components that provides advanced and pluggable look and feel.
FEATURES
Platform independent Model –View-Controller Extensible Customizable Configurable Light weight UI
RELATIONSHIP TO AWT
SWINGS
Light weighted components.
Completely written in java.
Better design than AWT.
Own look and feel.
AWT
Heavy weighted components.
Uses of native libraries.
Design issues are still not good.
Has to code properly.
AWT AND SWING CLASS HIERARCHY
FILES TO IMPORT
Java.awt.* Javax.swing.*
Javax : hierarchy of packages and set of “light weight” components for platform-independent rich GUI components.
SWING COMPONENTS
JApplet JButton JCheckBox JLabel JComboBox JRadioButton JScrollPane JTabbedPane JTable JTree JTextField
Used for making swing applet instance.
LABELS Swing labels are instances of JLabel class.
It can display text and/or an icon.
Constructors are:Jlabel(Icon i)Jlabel(String s,Icon I,int align)
Methods are:Icon getIcon()String getText()Void setIcon(Icon i)Void setText(String s)
EXAMPLE
Import java.awt.*;Import javax.swing.*;/* <applet code=“demo” width=200 height=200> </applet>
*/public class demo extends JApplet {
public void init(){Container contentPane= getContentPane();
JLabel j1=new JLabel (“HELLO”,i1,JLabel.CENTER); contentPane.add(j1);}}
IMAGE ICONS Swing image icons are instances of “ImageIcon”
class, which paints an icon from an image.
Constructors are:ImageIcon(URL url)ImageIcon(String filename)
Methods are:int getIconHeight()int getIconWidth()void paintIcon(Component c, Graphics g, int x,int
y)
EXAMPLE
Import java.awt.*;Import javax.swing.*;/* <applet code=“demo1” width=200 height=200> </applet> */public class demo1 extends JApplet {
public void init(){Container contentPane= getContentPane();
ImageIcon i1= new ImageIcon(“1.gif”);
JLabel j1=new JLabel (“HELLO”,i1,JLabel.CENTER); contentPane.add(j1);}}
TEXT FIELDS
Swing text field are instances of JTextComponent class. It has one subclass “JTextField” , for adding one line of text.
Constructors are:JTextField()JTextField(int cols) JTextField(String s,int cols)JTextField(String s)
EXAMPLE
Import java.awt.*;Import javax.swing.*;/* <applet code=“demo3” width=200 height=200> </applet> */public class demo3 extends JApplet {
JTextField jtf;
public void init(){Container contentPane= getContentPane();
jtf=new JJTextField(20); contentPane.add(jtf);}}
BUTTONS
AbstractButton is a superclass for checkboxes, push buttons and radio buttons.
JButton subclass provides the functionality of a push button.
Constructors are:JButton(Icon i)JButton(String s,Icon i)JButton(String s)
EXAMPLEImport java.awt.*;Import javax.swing.*;/* <applet code=“demo2” width=200 height=200> </applet> */public class demo2 extends JApplet {
public void init(){Container contentPane= getContentPane();ImageIcon img=new ImageIcon(“2.gif”);
JButton jb1=new JButton(img);contentPane.add(jb1);
JButton jb2=new JButton(“Click here”);contentPane.add(jb2);}}
CHECK BOXES JCheckBox class provides the functionality of a
checkbox. Its immediate superclass is JToggleButton, which provides support for two state buttons.
Constructors are:JCheckBox(Icon i)JCheckBox(Icon i, Boolean b)JCheckBox(String s)JCheckBox(String s, Boolean b)JCheckBox(String s,Icon i)JCheckBox(String s,Icon I, Boolean b)
void setSelected(Boolean b)
EXAMPLEImport java.awt.*;Import javax.swing.*;/* <applet code=“demo4” width=200 height=200> </applet> */public class demo4 extends JApplet {
public void init(){Container contentPane= getContentPane();JCheckBox cb=new JCheckBox(“C”,true);contentPane.add(cb);
JCheckBox cb=new JCheckBox(“C++”,normal);contentPane.add(cb);
JCheckBox cb=new JCheckBox(“Java”,normal);contentPane.add(cb);}}
RADIO BUTTONS
Radio buttons are supported by JRadioButton class, a subclass of AbstractButton.
Constructors are same as of checkboxes.
Radio buttons must be grouped into one set, so we use “ButtonGroup” class.
void add(AbstractButton b)
EXAMPLEImport java.awt.*;Import javax.swing.*;/* <applet code=“demo5” width=200 height=200> </applet> *public class demo5 extends JApplet {public void init(){Container contentPane= getContentPane();JRadioButton b1=new JRadioButton(“A”);contentPane.add(b1);JRadioButton b2=new JRadioButton(“A”);contentPane.add(b2);JRadioButton b3=new JRadioButton(“A”);contentPane.add(b3);
ButtonGroup bg=new ButtonGroup();bg.add(b1);bg.add(b2);bg.add(b3);} }
TABBED PANES A tabbed pane is a component that appears as a group
of folders in a file cabinet. Each folder has title and is selected one at a time.
Tabbed panes are encapsulated by JTabbedPane class.
void addTab(String str, Component comp)
Procedure to use tabbed pane is:
Create a JTabbedPane object. Call addtab() to add a tab to pane(title, and its
component for each tab). Repeat step 2 for each tab. Add tabbed pane to content pane of applet. Define subclasses for tabs.
EXAMPLEpublic class demo6 extends JApplet {public void init(){JTabbedPane tabs=new JTabbedPane();Tabs.addTab(“Home”, new homemenu());Tabs.addTab(“Insert”, new insertmenu());Tabs.addTab(“View”, new viewmenu());getContentPane().add(tabs);}}class homemenu extends Jpanel {public homemenu() { ---// buttons } }
class insertmenu extends Jpanel {public insertmenu() { ---// radio /checkboxes } }
SCROLL PANES A scroll pane is a component that presents a rectangular
area in which a component may be viewed horizontally or vertically.
Scroll panes are encapsulated by JScrollPane class.
JScrollPane(Component comp)JScrollPane(int v, int h)JScrollPane(Component comp, int v, int h)
H and v are constants defined by ScrollPaneConstants interface, as follows,
HORIZONTAL_SCROLLBAR_ALWAYS HORIZONTAL_SCROLLBAR_AS_NEEDED VERTICAL_SCROLLBAR_ALWAYS VERTICAL_SCROLLBAR_AS_NEEDED
EXAMPLE
public class demo5 extends JApplet {public void init(){Container contentPane= getContentPane();contentPane.setLayout(new Borderlayout());
Jpanel jp =new Jpanel();Jp.setLayout(ButtonGroup bg=new ButtonGroup();bg.add(b1);bg.add(b2);bg.add(b3);} }
COMBO BOXES A check box is a combination of text field and a drop-
down list.
Check boxes are provided by JCheckBox class, which extends JComponent.
JComboBox()JComboBox(Vector v)
Here v is a vector that initializes the combo box. Items are added to list of choices via a method:
void addItem(Object obj)
Here obj is the object to be added to the combo box.
EXAMPLEpublic class demo extends JApplet implements ItemListener {public void init() {Container contentPane= getContentPane();JComboBox jc = new JComboBox();jc.addItem(“Ludhiana”);jc.addItem(“Chandigarh”);jc.addItem(“Moga”);jc.addItemListener (this);contentPane.add(jc);
JLabel j1 =new JLabel(“ “);contentPane.add(j1);}public void itemStateChanged(ItemEvent e) {String s=(String) i.getItem();J1.setText(s);} }
Thank You…
House is open for queries...