source file name: tabdemo.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
// class for tree tab
class treedemo extends JPanel
{
JTree tr;
JTextField t1;
public treedemo()
{
BorderLayout bl=new BorderLayout();
setLayout(bl);
t1=new JTextField(40);
DefaultMutableTreeNode root=new DefaultMutableTreeNode("csdept");
DefaultMutableTreeNode fy=new DefaultMutableTreeNode("fycs");
DefaultMutableTreeNode sy=new DefaultMutableTreeNode("sycs");
DefaultMutableTreeNode ty=new DefaultMutableTreeNode("tycs");
DefaultMutableTreeNode fy1=new DefaultMutableTreeNode("PYTHON");
DefaultMutableTreeNode fy2=new DefaultMutableTreeNode("C");
DefaultMutableTreeNode fy3=new DefaultMutableTreeNode("DBMS");
DefaultMutableTreeNode sy1=new DefaultMutableTreeNode("corejava");
DefaultMutableTreeNode sy2=new DefaultMutableTreeNode("Advjava");
DefaultMutableTreeNode sy3=new DefaultMutableTreeNode("Android");
DefaultMutableTreeNode ty1=new DefaultMutableTreeNode("AI");
DefaultMutableTreeNode ty2=new DefaultMutableTreeNode("WebService");
DefaultMutableTreeNode ty3=new DefaultMutableTreeNode("DataSc.");
fy.add(fy1);
fy.add(fy2);
fy.add(fy3);
sy.add(sy1);sy.add(sy2);sy.add(sy3);
ty.add(ty1);ty.add(ty2);ty.add(ty3);
root.add(fy);root.add(sy);root.add(ty);
tr=new JTree(root);
add(tr,BorderLayout.NORTH);
add(t1,BorderLayout.SOUTH);
tr.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent me)
{
TreePath tp=tr.getPathForLocation(me.getX(),me.getY());
t1.setText("tree path "+tp);
}
}
);
}
}
// class for list tab
class ListDemo extends JPanel
{
JList ls;
Object ct;
JLabel lbtxt;
public ListDemo()
{
FlowLayout fl=new FlowLayout();
setLayout(fl);
JLabel lb=new JLabel("select your favorite Animals:");
lbtxt=new JLabel();
String[] animals={"Lion","Tiger","Jiraffe"};
ls=new JList(animals);
JScrollPane sp=new JScrollPane(ls);
add(lb);
add(sp);
JButton bt=new JButton("show");
add(bt);
bt.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
System.out.println("selected animals: "+ls.getSelectedValue());
lbtxt.setText("selected animals: "+ls.getSelectedValue());
}}
);
add(lbtxt);
}
}
// class for table tab
class TableDemo extends JPanel
{
String[] cols={"rollno","Name","salary"};
String data[][]={ {"101","Amit","670000"},
{"102","Jai","780000"},
{"105","Sachin","700000"}};
public TableDemo()
{
JTable t=new JTable(data,cols);
JScrollPane sp=new JScrollPane(t);
add(sp);
}
}
// Main class for tab demo
class tabdemo extends JFrame
{
TableDemo p1=new TableDemo();
treedemo p2=new treedemo();
ListDemo p3=new ListDemo();
public tabdemo()
{
setLayout(new BorderLayout());
JTabbedPane tp=new JTabbedPane();
tp.addTab("Table",p1);
tp.addTab("Tree",p2);
tp.addTab("List",p3);
add(tp);
}
public static void main(String as[])
{
tabdemo f=new tabdemo();
f.setTitle("Tabbedpane program");
f.setSize(300,300);
f.setVisible(true);
}
}
output:
0 Comments
Post a Comment