SWING

PREVIOUS



Swing

·        Swing  classes are placed in the javax.swing package.
·        Swing is an enhancement of Awt concept.so we use some classes from the java.awt packge in swing programs.
JApplet class:
·        The japplet class is fundamental to swing . this class extends the familiar Applet class.
·        Any applet that involves one or more swing components should extend the Japplet class.
·        The japplet class has much more functionality than the applet class.
·        There is one important difference between the applet and Japplet classes
·        When we use Japplet is an applet, we should add each component to the container.whatever we add to the container will be automatically added to the appletes window.
Icons:
The icons are extensively used in swing. The image icon class encapsulates an icon.
The paintIcon()method in the imageicon class paints an icon from an image.


Jlabel control :
The names of most of the classes in the swing package start with the letter J.
Constructors:
JLabel(Icon i)

Import java.awt.*;
Import javax.swing.*;

Public class label extends JApplet
{
public void init()
{
Container c=getContentPane();

ImageIcon i=new ImageIcon(“    “);
JLabel j1=new JLabel(hello”,I,Jlabel.CENTER);
c.add(j1);
}
}                 
Jtextfield control:

Constructors:
JtextField()

Import javaawt.*;
Import javax.swing.*;
Public class text extends JApplet
{
JTextField j;

Public void init()
{
Container c=getContainerPane();
C.setLayout(new FlowLayout());
J=new JTextField(“hello “);
c.add(j);
}
}  



/*jbutton control:
in swing , a button can have a border around it.
it may have an image as its caption.
it may be to display different color and diff image,
depending on its current state.


the AbstractButton class is a super class for push buttons,
 check boxes and radio buttons.
this class extends JComponent class
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import javax.swing.*;
public class but extends JApplet implements ActionListener
{
JTextField t;
JButton b1,b2;
ImageIcon i;
//JColor f,b;
Container c;
public void init()
{
c=getContentPane();
i=new ImageIcon("ie.gif");
c.setLayout(new FlowLayout());
//f=new JColor(Color.red);
b1=new JButton("hai");
b1.setActionCommand("first");
b1.addActionListener(this);
b1.setBackground(new Color(0,255,0));
c.add(b1);


b2=new JButton(i);
b2.setActionCommand("second");
b2.addActionListener(this);
c.add(b2);

t=new JTextField(15);
c.add(t);
}
public void actionPerformed(ActionEvent a)
{
String ca=a.getActionCommand();
t.setText(ca);
}
}

//<applet code=but height=300 width=300></applet>


/*tables:
this component enables us to display row and columns  of data.
once we define this component, we can resize the columns by dragging
the cursor on the column boundaries.
we can also drag a column to a nwe position, if we wish to do so.
the JTable class encapsulates a table.this class extends JComponent class
constructor:
JTable(object data[][],object columnheadings[])
column-->one d
data-->two d
*/
import java.awt.*;
import javax.swing.*;
public class table extends JApplet
{
JTable jt;
JScrollPane scr;
Container c;
public void init()
{
c=getContentPane();
c.setLayout(new BorderLayout());
String[] ch={"name","basic","allowance","deduction"};
Object[] [] dt={{"mano","15000","10000","8000"},
                    {"anu","20000","18000","15000"},
                  {"eni","25000","22000","20000"},
 {"renu","25000","22000","20000"}};
jt=new JTable(dt,ch);
int v=ScrollPaneConstants.VER
TICAL_SCROLLBAR_ALWAYS;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;                  
scr=new JScrollPane(jt,v,h);
c.add(scr,BorderLayout.CENTER);
}
}
//<applet code=table width=400 height=300></applet>



nt complete:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
import javax.swing.event.*;
import java.applet.*;
//<applet code="clr1" width=500 height=500></applet>

public class clr1 extends JApplet
{
pd p;ed p1;

public void init()
{
Container cp=getContentPane();
JTabbedPane jdp=new JTabbedPane();
p=new pd();
p1=new ed();
jdp.addTab("Personal Details",p);
jdp.addTab("Educational Details",p1);
cp.add(jdp);
}
}

class pd extends JPanel
{
JTextField t,t1;
JLabel f,f1,f2,f3,f4,f5,f6,f7;
JComboBox jc,jc1,jc2;
JRadioButton r,r1;ButtonGroup bg;
JCheckBox b,b1,b2;
public pd()
{

f=new JLabel("Name:");
t=new JTextField(15);
f1=new JLabel("Date Of Birth:");
f7=new JLabel("dd");
jc=new JComboBox();
jc.addItem(" ");jc.addItem("1");jc.addItem("2"); jc.addItem("3");
jc.addItem("4");jc.addItem("5");  jc.addItem("6"); jc.addItem("7");  jc.addItem("8");  jc.addItem("9");  jc.addItem("10"); 
jc.addItem("11");  jc.addItem("12");  jc.addItem("13");  jc.addItem("14"); 
jc.addItem("15");  jc.addItem("16");  jc.addItem("17");  jc.addItem("18"); 
jc.addItem("19");  jc.addItem("20");  jc.addItem("21");  jc.addItem("22"); 
jc.addItem("23");  jc.addItem("24");  jc.addItem("25");    jc.addItem("26"); 
jc.addItem("27"); jc.addItem("28");  jc.addItem("29");  jc.addItem("30"); 
jc.addItem("31");  
f6=new JLabel("mm");
jc1=new JComboBox();
jc1.addItem(" ");jc1.addItem("1");jc1.addItem("2"); jc1.addItem("3");
jc1.addItem("4");jc1.addItem("5");  jc1.addItem("6"); jc1.addItem("7");
jc1.addItem("8");  jc1.addItem("9");  jc1.addItem("10"); 
jc1.addItem("11");  jc1.addItem("12");
f5=new JLabel("yy");
jc2=new JComboBox();
jc2.addItem(" ");jc2.addItem("1980");jc2.addItem("1981"); jc2.addItem("1982");
jc2.addItem("1983");jc2.addItem("1984");  jc2.addItem("1985"); jc2.addItem("1986");
jc2.addItem("1987");  jc2.addItem("1988");  jc2.addItem("1989");
f2=new JLabel("Gender:");
r=new JRadioButton("Male");
r1=new JRadioButton("Female");
bg=new ButtonGroup();
bg.add(r);
bg.add(r1);
f3=new JLabel("Educational Qualification:");
t1=new JTextField(15);
f4=new JLabel("Languages Known:");
b=new JCheckBox("English");
b1=new JCheckBox("Tamil");
b2=new JCheckBox("Hindi");
add(f);   add(t);
add(f1);add(f7);add(jc);add(f6);add(jc1);add(f5);add(jc2);
add(f2);add(r);add(r1);
add(f3);add(t1);
add(f4);add(b);add(b1);add(b2);
}
}
class ed extends JPanel implements ActionListener
{
JTable jt;
JScrollPane js;
JButton b1;
JLabel l1,l2,l3,l4,l5;
int i,j;
JTextField[] tf=new JTextField[5];JTextField tfy;
Object[][] dt=new Object[10][10];
public ed()
{
//setLayout(new GridLayout(7,2,50,50));
l1=new JLabel("Name");
//l1.setLayout(FlowLayout.LEFT);
l2=new JLabel("School");
//l2.setLayout(FlowLayout.RIGHT);
l3=new JLabel("10th Marks");
//l3.setLayout(FlowLayout.CENTER);
/*l4=new JLabel("12th Marks");
l5=new JLabel("Enter Row");
tf[0]=new JTextField(5);
tf[1]=new JTextField(15);tf[3]=new JTextField(15);
tf[2]=new JTextField(15);tfy=new JTextField(15);
b1=new JButton("ADD");
String[] ch={"NAME","SCHOOL","10th MARKS","12th MARKS"};
jt=new JTable(dt,ch);
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;                  
js=new JScrollPane(jt,v,h);*/
add(l1,"LEFT");//add(tf[0]);
add(l2,"RIGHT");//add(tf[1]);
add(l3);/*add(tf[2]);
add(l4);add(tf[3]);
add(l5);add(tfy);
add(b1);
b1.addActionListener(this);
add(js);*/
}
public void actionPerformed(ActionEvent ae)
{
/*if(ae.getSource()==b1)
{
int i=Integer.parseInt(tfy.getText());
for(j=0;j<4;j++)
{
dt[i][j]=tf[j].getText(); 
tf[j].setText("");  tfy.setText("");
}
}*/
}
}

No comments:

Post a Comment