15. Swing
Swing is a set of classes that provides more powerful and flexible components than are possible with the AWT. In addition to buttons, check boxes and labels, Swing supplies severals components including tabbed panes, scroll panes, trees, picture buttons, combo box , etc.
Unlike AWT components, Swing components are platform independent since they are written in Java. They are called as lightweight components. Swing related classes are contained in javax.swing and its subpackages.
JApplet
Applets that use Swing must be subclasses of JApplet, which extends Applet. JApplet is rich in functionality than Applets and provides various panes such as content pane, glass pane and root pane. To add a component, obtain the pane then call add( ) method for the pane of the JApplet.
Container getContentPane( ) to obtain the content pane
void add(comp) to add a component in content pane
Comp
label
Constructor
JLabel(Icon I)
JLabel(String s)
JLable(String s, Icon I, int align)
Methods
Icon getIcon( )
String getText( )
void setIcon(Icon I)
void setText(String s
Comp
Text Field
Constructor
JTextField( )
JTextField(int cols)
JTextField(String s)
JTextField(String s, int cols)
Methods
.........................
Comp
Buttons
Constructor
JButton(Icon i)
JButton(String s)
JButton(String s, Icon I)
Methods
................................
Comp
CheckBox
Constructor
JCheckBox(Icon I)
JCheckBox(String s)
JCheckBox(Icon I, boolean)
JChcekBox(String s, boolean)
JCheckBox(String s, Icon I, boolean)
Methods
...........................
Comp
Radio Buttons
Constructor
JRadioButton(Icon I)
JRadioButton(String s)
JRadioButton(String s, boolean)
JRadioButton(s, I, boolean)
Methods
.........................................
Comp
Combo Box
Constructor
JComboBox( )
JComboBox(Vector v)
Methods
..............................
Comp
Tabbed Panes
Constructor
JTabbedPane( )
Methods
addTab(title, comp)
Comp
Scroll Panes
Constructor
JScrollPane(comp)
JScrollPane(int vsb, int hsb)
JScrollPane(comp, vsb, hsb)
Methods
.......................................
Comp
Trees
Constructor
JTree(HashTable h)
JTree(Object ob[])
JTree(TreeNode t)
JTree(Vector v)
Methods
...........................................
Comp
Tables
Constructor
JTable(Obect data[][], Object colheads[])
Methods
...............................
Icons
Method
Int getIconHeight( )
int getIconWidth( )
void paintIcon(comp, Graphics, x, y)
void setDisabledIcon(icon)
void setPressedIcon(icon)
void setSelectedIcon(icon)
void setRolloverIcon(icon) Scroll Panes
JScrollPane(comp, vsb, hsb)
The vsb, hsb constants are
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED
Example :
import javax.swsing.*;
import java.awt.*;
public class Jlabel extends JApplet
{
public void init( )
{
Container cp = getContentPane( );
ImageIcon ii = new ImageIcon("Birds");
JLabel jl = new JLabel("Birds",ii,JLabel.Center);
Cp.add(jl);
}
}
Example :
import javax.swing.*;
import java.awt.*;
public class jscroll extends JApplet
{
public void init( )
{
Container CP = getContentPane( );
Jpanel jp = new Jpanel( );
jp.setLayout(new GridLayout(20,20));
int b = 0;
for(int ii = 0; I<20; I++)
{
for(k=0;k<20;k++)
{
jp.add(new JButton("Button " + b));
b++;
}
}
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;JScrollPane jsp = new JscrolPane(jp, v, h) ;
CP.add(jsp, BorderLayout.CENTER);
}
}
No comments:
Post a Comment