0% found this document useful (0 votes)
6 views29 pages

Swingtree

The document describes how to create and use JComboBox in Java. JComboBox is used to create dropdown lists. Methods like addItem(), getItemCount(), getItemAt() and getSelectedItem() are used to add, remove and get items from the JComboBox.

Uploaded by

laxmimane245
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views29 pages

Swingtree

The document describes how to create and use JComboBox in Java. JComboBox is used to create dropdown lists. Methods like addItem(), getItemCount(), getItemAt() and getSelectedItem() are used to add, remove and get items from the JComboBox.

Uploaded by

laxmimane245
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

 It is used to represent data in hierarchical

format.
 DefaultMutableTreeNode Class is used to
create node of tree.
 DefaultMutableTreeNode Class is available
in javax.swing.tree package.
i.e.
import
javax.swing.tree.DefaultMutableTreeNode;
 add() of DefaultMutableTreeNode Class is used
to add node to tree.

Syntax:
void add(DefaultMutableTreeNode
child_node_obj);

e.g.
b.add(a);

 b- parent node of type DefaultMutableTreeNode


 a-child node of type DefaultMutableTreeNode
 E.g.
 DefaultMutableTreeNode select_node=new
DefaultMutableTreeNode(“select”);
 DefaultMutableTreeNode color_node=new
DefaultMutableTreeNode(“Colors”);
e.g. select_node.add(color_node);
 JTree(DefaultMutableTreeNode root_node_obj)-
 To create tree.
 E.g. JTree t=new JTree(select_node);

c.add(t);
import java.awt.*;
import javax.swing.*;
Import java.applet.*;
import javax.swing.tree.DefaultMutableTreeNode;
/* <applet code=JTreeDemo.class width=200
height=200></applet>*/

public class JTreeDemo extends JApplet


{
Public JTreeDemo()
{
Container c=getContentPane();
FlowLayout f=new FlowLayout();
c.setLayout(f);
 DefaultMutableTreeNode selectnode=new
 DefaultMutableTreeNode("Select");
 DefaultMutableTreeNode colornode=new
DefaultMutableTreeNode("colors");
 DefaultMutableTreeNode fruitnode=new
DefaultMutableTreeNode("fruits");
 DefaultMutableTreeNode rednode=new
DefaultMutableTreeNode("red");
 DefaultMutableTreeNode greennode=new
DefaultMutableTreeNode("green");
 DefaultMutableTreeNode bluenode=new
DefaultMutableTreeNode("blue");
 DefaultMutableTreeNode grapesnode=new
DefaultMutableTreeNode("grapes");
 DefaultMutableTreeNode banananode=new
DefaultMutableTreeNode("banana");
selectnode.add(colornode);
selectnode.add(fruitnode);

colornode.add(rednode);
colornode.add(greennode);
colornode.add(bluenode);

fruitnode.add(grapesnode);
fruitnode.add(banananode);
JTree t=new JTree(selectnode);
 c.add(t);
 }
 }
public JTreeDemo()
{
Container c=getContentPane();
FlowLayout f=new FlowLayout();
c.setLayout(f);
DefaultMutableTreeNode selectnode=new DefaultMutableTreeNode("Selelct");

DefaultMutableTreeNode colornode=new DefaultMutableTreeNode("colors");


selectnode.add(colornode);

DefaultMutableTreeNode rednode=new DefaultMutableTreeNode("red");


colornode.add(rednode);
DefaultMutableTreeNode greennode=new DefaultMutableTreeNode("green");
colornode.add(greennode);
DefaultMutableTreeNode bluenode=new DefaultMutableTreeNode("blue");
colornode.add(bluenode);
DefaultMutableTreeNode fruitnode=new DefaultMutableTreeNode("fruits");

selectnode.add(fruitnode);

DefaultMutableTreeNode grapesnode=new DefaultMutableTreeNode("grapes");

fruitnode.add(grapesnode);

DefaultMutableTreeNode banananode=new DefaultMutableTreeNode("banana");

fruitnode.add(banananode);

JTree t=new JTree(selectnode);


c.add(t);

}
}
import java.awt.*;
import javax.swing.*;

/*
<applet code="JScrollPaneDemo1" width=300 height=250>
</applet>
*/

public class JScrollPaneDemo1 extends JApplet


{
public void init()
{
Container c = getContentPane();

// Add 100 buttons to a panel


JPanel jp = new JPanel();
GridLayout g=new GridLayout(10,10);
jp.setLayout(g);
int cnt = 0;
 for(int i = 0; i < 10; i++)
 {
 for(int j = 0; j < 10; j++)
 {
 jp.add(new JButton("Button " + (cnt+1)));
 cnt++;
 }
 }
 // Add panel to a scroll pane

 int v =
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
 int h =
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
 JScrollPane jsp = new JScrollPane(jp);

 c.add(jsp);
 }
 }
 JTable class is used to display data in tabular
form.
 Constructor :
JTable (Object[][] row_data,-
Object[] column_heading)
Here ‘row_data’ is two dimensional array of
information to be displayed.
‘column_heading’ is one dimensional array with
column headings.
1)getRowCount()
-it returns number of rows of table.
Syntax:
int getRowCount();
e.g.
int i=t.getRowCount();
2) getColumnCount()
-it returns number of columns of table.
Syntax:
int getColumnCount();
e.g.
int i=t.getColumnCount();
import java.awt.*;
import javax.swing.*;
import java.applet.*;

/*
<applet code="JTableDemo1.class" width=300
height=300>
</applet>
*/
public class JTableDemo1 extends JApplet
{
public JTableDemo1()
{
Container c=getContentPane();
FlowLayout f=new FlowLayout();
c.setLayout(f);
String colheads[]={"Student No", "Name", "Age"};
 String row_data[][]={
 {“1”, "Satish", “22”},
 {"2", "Sachin", “32”},
 {"3", "Shweta", “4”}
 };
 JTable jt=new JTable(row_data, colheads);
 c.add(jt);
 }
 }
 String row_data[][]={
 {“1”, "Satish", “22”},
 {"2", "Sachin", “32”},
 {"3", "Shweta", “4”}
 };
 JTable jt=new JTable(row_data, colheads);
 JScrollPane p=new JScrollPane(jt,
 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS,
 ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
 c.add(p);
 }
 }

 }
 }
/*
<Applet code="JTableDemo1.class" width="300" height="300">
</Applet>
*/
public class JTableDemo1 extends JApplet
{
Container c;
public void init()
{
c=getContentPane();

String colheads[]={"Student No", "Name", "Age"};


Object data[][]={
{"1", "Satish",22},
{"2", "Sachin",32},
{"3", "Shweta",4}
};
JTable jt=new JTable(data, colheads);
JScrollPane p=new JScrollPane(jt);
c.add(p);
}
}
 -to create dropdown list.
 Constructor:
 JComboBox()
 -to create blank combobox.
e.g. JComboBox c1=new JComboBox();
1.addItem()-to add item to combobox.
Syntax:
void addItem(Object item);
e.g. c1.addItem(“abc”);
2. getItemCount()
-returns total count of items.
Syntax:
int getItemCount();
e.g. int i=c1. getItemCount();
3.getItemAt()
Returns item from specific index.
Syntax:
Object getItemAt(int index);
e.g. String s=c1.getItemAt(1);
4. getSelectedItem()
Returns selected item.
Syntax:
Object getSelectedItem();
e.g.
String s=c1. getSelectedItem();
 5.removeAllItems()
 Removes all items from combobox.
Syntax:
Void removeAllItems();
e.g.
c1. removeAllItems();
6. removeItemAt()
Removes item from specific index.
Syntax:
Void removeItemAt(int index);
e.g.
c1. removeItemAt(1);
 9823079041
 02027653238

You might also like