Swing and GUI Components
Swing and GUI Components
Swing and GUI Components
object
Java.awt.component
Java.awt.container
Jcomponent Window
Frame
JFrame
JComponent
The Jcomponent class is a subclass of container. This class contains a
large no. of subclasses which define components like JButton , JTable.
Jcomponent
Abstract button
JButton
JMenuItem
JToggleButton
JCheckbox
JRadioButton
JComboBox
JInternalFrame
JLabel
JLayeredPane
JList
JMenuBar
JOptionPane
JPanel
JPopupMenu
JProgressBar
JScrollPane
JSeparator
JSlider
JTable
JTableHeader
JTextComponent
JTextArea
JTextField
JPasswordField
JToolBar
JToolTip
JTree
They are called root containers. These have several panes , JRootPane,
JMenuBar, JLayeredPane , ContentPane and GlassPane.
Constructors of JFrame
• JFrame() -- Creates an invisible window without title
• JFrame(String title) -- Creates an invisible window with a title given by
string title.
Methods in JFrame
• String getTitle() -- Returns a string representing a title of the frame
• void setTitle(String title) -- sets the title of the frame to this string
• void setVisible(Boolean b) -- shows or hides this frame window if b is
true, it shows the window otherwise it hides it.
• void setSize(int width, int height) -- sets the size of the window to the
specified width and height in pixels.
• void setLayout(LayoutManager mgi) -- sets the layout manager for
container
• int getX() -- returns the x component of window
• int getY() -- returns the y component of window
• void setLocation(int x,int y) -- moves the frame window to a new
location on the screen , the top left corner is specified by x and y
• int getHeight() -- returns the current height of the window
• int getWidth() -- returns the current width of the window
import javax.swing.*;
public class frameex extends JFrame
{
public static void main(String a[])
{
JFrame f = new JFrame("My Frame");
f.setSize(300,300);
f.setVisible(true);
}
}
JFrame generates the window events and these events are handeled in
java.awt.event package
• voidwindowActivated(WindowEvent we)
• voidwindowClosed(WindowEvent we)
• voidwindowClosing(WindowEvent we)
• voidwindowDeactivated(WindowEvent we)
• voidwindowDeiconified(WindowEvent we)
• voidwindowIconified(WindowEvent we)
• voidwindowOpened(WindowEvent we)
frame1.java
import javax.swing.*;
import java.awt.event.*;
public class frame1 extends JFrame implements WindowListener
{
frame1(String a)
{
setTitle(a);
addWindowListener(this);
}
public void windowActivated(WindowEvent e)
{ }
public void windowOpened(WindowEvent e)
{ }
public void windowIconified(WindowEvent e)
{ }
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowClosed(WindowEvent e)
{ }
frameevent.java
frameevent1.java
public class frameevent1
{
public static void main(String a[])
{
frame1adap f = new frame1adap("My Frame");
f.setSize(300,300);
f.setVisible(true);
}
}
f. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
JLabel
The JLabel class is a subclass of JComponent. A JLabel object is a compone
for placing text or an icon or both. A JLabel displays a single line of read only
text in a container. The text can be changed by the application and not by
user.
It has the following int typeconstants that indicate the alignment of the label
content.
JLabel.CENTER JLabel.RIGHT JLabel.TOP
JLabel.LEFT JLabel.BOTTOM
Creating JLabel
Constructors
• JLabel() --- Creates an empty label
• JLabel(String s) --- Creates a label with specified string
• JLabel(String s, int alignment) --- creates a label with specified string
and with specified alignment
• JLabel(Icon i) --- creates a label using icon
• JLabel(Icon I, int alignment) --- creates a label using icon and
specified alignment
• JLabel(String s,Icon I, int alignment) --- creates a label using icon
and specified alignment and label is set to specified string
Methods
• Icon getIcon() -- returns the icon of the label
• String getText() -- returns the text
• void setFont(Font f) -- sets the font of the label
Example iconex.java
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class iconex extends JFrame
{
Container c;
iconex(String s)
{
setTitle(s);
c=getContentPane();
c.setLayout(new FlowLayout());
JLabel l1= new JLabel("hi");
JLabel l2= new JLabel("bye",JLabel.RIGHT);
Icon i= new ImageIcon("c:\fruit.jpg");
JLabel l3= new JLabel("fruits",i,JLabel.LEFT);
c.add(l1);
c.add(l2);
c.add(l3);
}
public static void main(String a[])
{
JFrame f = new JFrame("My Frame");
f.setSize(300,300);
f.setVisible(true);
}
}
labelapplet.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code="labelapplet" height = 300 width=300>
</applet>
*/
public class labelapplet extends JApplet
{
JLabel l1;
public void init()
{
Container c=getContentPane();
c.setLayout(new FlowLayout());
l1=new JLabel("Welcome");
c.add(l1);
}
}
JButton
JButton is a concrete subclass of AbstractButton which is a subclass of
JComponent. When a button is clicked an ActionEvent is created. If an
application want to perform some action when button is clicked, it should
implement the interface ActionListener and register the listener receive
events from the button. One of the advanced features of JButton is that an
image can be used as button
Constructors
• JButton -- Creates a button with no label
• JButton(String s) -- creates a button with specified string s as a label
• JButton(Icon i) -- creates a button with the icon I as button
• JButton(String s, Icon i) -- creates a button with icon I as button and s
as the label
Methods
• void setText(String label) -- sets the buttons label to the specified string
• String getText() -- returns the label of the button
• void setIcon(Icon i) --
• Icon getIcon() -- returns the icon of the label
• void addActionListener(ActionEvent ae) -- add the specified action
listener to receive action from button
Example simple frame that having one button and one label when button is
clicked “bye” is displayed on label
Label2.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
buttonex.java
public class buttonex
{
public static void main(String a[])
{
Sweety Batavia Page 8
Swing
Example -- the frame contains two buttons an done label when button is
pressed label displays the string associated with button. The
getActionCommand() is called to obtain the string of pressed button
Labelbutton.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
c=getContentPane();
c.setLayout(new FlowLayout());
setTitle(s);
b1= new JButton("HI..");
b2= new JButton("bye...");
l1=new JLabel();
b1.addActionListener(this);
b2.addActionListener(this);
c.add(b1);
c.add(b2);
c.add(l1);
}
public void actionPerformed(ActionEvent ae)
{
l1.setText(ae.getActionCommand());
}
}
labelbutton2.java
public class labelbutton2
{
JTextField
The simple type texts are dealt by JTextField , JPasswordField , JTextArea
class. The styled texts are handled in JEditorPane and JTextPane class
JTextfield is a subclass of JText Component which is a subclass of
JComponent. The alignment of the text is defined by the following int type
constants
JTextField.LEFT , JTextField.CENTER , JTextField.RIGHT
Constructors
• JTextField() -- creates a new text field the text is set to null and the no.
of columns is set to 0
• JTextField(String t) -- creates a new text field with the specified string
as the text
• JTextField(int columns) -- creates a new text field the text is set to null
and the no. of columns is set to specified int value
• JTextField(String text , int column ) -- creates a new text field with
specified string as the text with a width to display the specified no. of
columns.
Methods
ActionEvent
In the textfield after filling in the entries the return key is to be pressed.
Pressing of the enter key generates actionEvent and ActionEvent is managed
by actionListener
textfieldeg2.java
public class textfieldeg2
{
public static void main(String a[])
{
textfieldeg f = new textfieldeg();
f.setSize(300,300);
f.setVisible(true);
}
}
KeyEvent
It is generated when a key is pressed , typed or released. The key typed
event is generated only when a character is generated. Pressing alt+key do
not produce a character.
Some virtual key constants
VK_ENTER VK_CANCEL VK_CONTROL VK_SPACE
VK_BACK_SPACE VK_CLEAR VK_SHIFT VK_ESCAPE
VK_CAPS_LOCK VK_PAGE_UP VK_PAGE_DOWN VK_LEFT
VK_UP VK_DOWN VK_RIGHT VK_COMMA
VK_DELETE VK_F1 to VK_F12
c.setLayout(new FlowLayout());
t1=new JTextField(10);
t1.addKeyListener(this);
c.add(t1);
l1=new JLabel();
c.add(l1);
}
public void keyTyped(KeyEvent e)
{
char ch = e.getKeyChar();
String s;
s=t1.getText()+ch;
l1.setText(s);
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==e.VK_SPACE)
l1.setText(l1.getText()+ "space");
}
public void keyReleased(KeyEvent e)
{ }
}
textfieldeg4.java
public class textfieldeg4
{
public static void main(String a[])
{
textfiledeg1 f = new textfiledeg1();
f.setSize(300,300);
f.setVisible(true);
}
}
textfieldapplet.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/* <applet code="textfieldapplet" width=300 height=300>
</applet>
*/
public class textfieldapplet extends JApplet implements ActionListener
{
JTextField t1;
JLabel l1;
public void init()
{
Container c=getContentPane();
c.setLayout(new FlowLayout());
t1=new JTextField(10);
t1.addActionListener(this);
l1=new JLabel();
c.add(t1);
c.add(l1);
}
public void actionPerformed(ActionEvent ae)
{
l1.setText(t1.getText());
}
}
JPasswordField
It creates display similar to JTextField. The only difference is that when text
is displayed the actual char are replaced by *. It is subclass of
JTextComponent
Constructors
• JPasswordField() -- creates a new empty password field
• JTextField(String t) -- creates a new password field with the specified
string as the text
• JTextField(int columns) -- creates a new password field the text is set to
null and the no. of columns is set to specified int value
• JTextField(String text , int column ) -- creates a new password field with
specified string as the text with a width to display the specified no. of
columns.
Methods
• void setEchochar(char c) -- sets the specified character as echo
character for this field
• char getEchochar() -- Returns the echo character set for this field
Example shows how to create PasswordField in JFrame when user enter key
it will be displayed in label
passwordeg1.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
t1.addActionListener(this);
c.add(t1);
l1=new JLabel();
c.add(l1);
}
public void actionPerformed(ActionEvent ae)
{
l1.setText(t1.getText());
}
}
passwordeg2.java
public class passwordeg2
{
public static void main(String a[])
{
passwordeg1 f = new passwordeg1();
f.setSize(300,300);
f.setVisible(true);
}
}
note: keyevents are same as in JTextField
JCheckBox
JcheckBox is a subclass of JToggleButton which is again a subclass of
AbstractButto. A chechbox is a two state graphical component that will be in
either selected or in deselected state
A checkbox when clicked generates the following events
1. ActionEvent
2. ChangeEvent
3. ItemEvent
Constructors
• JCheckBox() -- creates a checkbox with a blank label the checkbox is set
to deselected state
• JCheckBox(Icon i) -- creates a checkbox using the specified icon and is
set to deselected state
• JCheckBox(Icon I, Boolean state) -- creates a checkbox using the
specified icon and state
• JCheckBox(String s) -- creates the checkbox with the label set to s and
is set to deselected state
• JCheckBox(String s, Boolean state) --
• JCheckBox(String s, icon i) --
• JCheckBox(String s,icon I , Boolean state) --
Methods
• String getText() -- Returns the text contained in this checkbox
• void setText(String s) --
• Icon getIcon() -- returns the icon of the checkbox
• void setSelected(Boolean state) -- sets the checkbox to specified state
• boolean isSelected() -- returns the state of the checkbox
Sweety Batavia Page 15
Swing
JRadioButton
JRadioButton are like Jcheckbox. It is used to represent a collection of
mutually exclusive options i.e out of several options only one will be in
selected state and all the remaining are in deselected state. The radiobuttons
are created using jradiobutton class which is asubclass of jtogglebutton. The
jradiobutton must be placed in a button group the button group is created
using the buttongroupclass which has no argument constructor. After
creating jradiobutton it is to be added to he buttongroup using add()
method. Jradiobutton generates the following events
1. Actionevent
2. itemevent
3. changeevent
Constructors
• JRadioButton() -- creates a radiobutton without any label the
radiobutton is set to deselected state
• JRadioButton(Icon i) -- creates a radiobutton with icon i & radiobutton is
set to deselected state
• JRadioButton(Icon I ,Boolean state) -- creates a radiobutton with icon I
radiobutton is set to specified state
• JRadioButton(String s) -- creates a radiobutton with string as label and
radiobutton is set to deselected state
• JRadioButton(String s , Boolean state) -- creates a radiobutton with
string s and radiobutton is set to specified state
• JRadioButton(String s,Icon I) --
• JRadioButton(string s, Icon I ,Boolean state) --
Methods
All the methods are same as in JCheckBox
Example
When user checks any radiobutton its text is displayed in label
radiobutton.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
c.add(r1);
r2=new JRadioButton("Second");
r2.addActionListener(this);
c.add(r2);
r3=new JRadioButton("Third");
r3.addActionListener(this);
c.add(r3);
l1=new JLabel();
c.add(l1);
ButtonGroup bg = new ButtonGroup();
bg.add(r1);
bg.add(r2);
bg.add(r3);
}
public void actionPerformed(ActionEvent e)
{
if(r1.isSelected()==true)
l1.setText(r1.getText());
if(r2.isSelected()==true)
l1.setText(r2.getText());
if(r3.isSelected()==true)
l1.setText(r3.getText());
}
}
radiobutton2.java
public class radiobutton2
{
public static void main(String a[])
{
radiobutton f = new radiobutton();
f.setSize(300,300);
f.setVisible(true);
}
}
ItemEvent
This event indicate that item like radiobutton is selected or deselected. Some
properties are as follows
1. DESELECTED indicates that item is deselected
2. SELECTED indicates that item is selected
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
radiobutton4.java
public class radiobutton4
{
public static void main(String a[])
{
radiobutton3 f = new radiobutton3();
f.setSize(300,300);
f.setVisible(true);
}
}
Example
radiobutton5.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
}
public void itemStateChanged(ItemEvent e)
{
String s=e.getSource().getClass().getName();
if(s=="javax.swing.JCheckBox")
{
JCheckBox c=(JCheckBox)e.getSource();
l1.setText(c.getText());
}
if(s=="javax.swing.JRadioButton")
{
JRadioButton r=(JRadioButton)
e.getItemSelectable();
l1.setText(r.getText());
}
}
}
radiobutton6.java
public class radiobutton6
{
public static void main(String a[])
{
radiobutton5 f = new radiobutton5();
f.setSize(300,300);
f.setVisible(true);
}
}
JList
It is a subclass of Jcomponent. A list is used when the no. of items for
selection is large. Selection of an item is done by clicking on the item itself.
A swing list does not have a scrollbar to display the list list is to be placed
inside a JScrollPane() object.
Constructors
• JList() -- creates an empty list
• JList(Vector vec) -- creates a list with items specified in vector vec
• JList(object[] obj) -- creates a list with items specified in the object
array
Methods
• void setVisibleRowCount(int c) -- sets the no. of rows in the list to be
displayed. Default value is 8
• int getSelectedIndex() -- returns the index of the first selected item
• object getSelectedValue() -- returns the topmost selected item
• object[] getSelectedValues() -- returns the array of all selected items
j1=new JList(s);
c.add(j1);
b1=new JButton("click");
b1.addActionListener(this);
c.add(b1);
l1=new JLabel();
c.add(l1);
}
public void actionPerformed(ActionEvent e)
{
l1.setText((String)j1.getSelectedValue());
}
}
lsit2.java
public class lsit2
{
public static void main(String a[])
{
list1 f = new list1();
f.setSize(300,300);
f.setVisible(true);
}
}
here we can get only one selected item we can select more than one item by
pressing ctrl key if we want to get all selected item then following change is
to be done
public void actionPerformed(ActionEvent e)
{
int sel[]=j1.getSelectedIndices();
for(int i=0;i<sel.length;i++)
{
l1.setText(l1.getText()+j1.getModel().getElementAt(sel[i]));
}
}
DefaultListModel
Steps
1. create an instance of defaultlistmodel
2. add items to this model
3. create jlist by passing this model as argument in the constructor
example
DefaultListModel m=new DefaultListModel();
m.addElement(“aa”);
m.addElement(“bb”);
JList l1 =new JList(m);
Methods
• void addElement(Object o) -- add the given object at end of list
• void clear() -- removes all the elements from thelist
• Object firstElement() -- returns the firstElement
• Object getElementAt() -- returns the list item at the specified index
• Int getSize() -- returns the no of items in the list
• void insertElementAt(Object o,int index) --
• boolean removeElement(Object o) -- removes the first occurrence of
the object in the list
• void removeElementAt(int index) -- removes the item at the specified
index in the list
Example
aaa add
bbb
ccc Add all
ddd
list3.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
list3()
{
Container c=getContentPane();
c.setLayout(new FlowLayout());
m1=new DefaultListModel();
m1.addElement("aaa");
m1.addElement("bbb");
m1.addElement("ccc");
m1.addElement("ddd");
l1=new JList(m1);
m2=new DefaultListModel();
l2=new JList(m2);
c.add(l1);
c.add(l2);
b1=new JButton("Add");
b1.addActionListener(this);
c.add(b1);
b2=new JButton("Add All");
b2.addActionListener(this);
c.add(b2);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="Add")
{
int s[]=l1.getSelectedIndices();
String str[] =new String[10];
for(int i=0;i<s.length;i++)
{
m2.addElement(m1.getElementAt(s[0]));
str[i]=(String)m1.getElementAt(s[i]);
}
for(int i=0;i<str.length;i++)
{
m1.removeElement(str[i]);
}
}
if(e.getActionCommand()=="Add All")
{
for(int i=0;i<m1.getSize();i++)
{
m2.addElement(m1.getElementAt(i));
}
m1.clear();
}
}
}
list4.java
public class list4
{
public static void main(String a[])
{
list3 f = new list3();
f.setSize(300,300);
f.setVisible(true);
}
}
ListSelectionListener Interface
List selectionEvent is defined in the javax.swing.event package
Methods
void valueChanged(ListSelectionEvent e) -- it is generated when selection
value is changed
Example
listselection.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
f.setSize(300,300);
f.setVisible(true);
}
}
JComboBox
JComboBox is a subclass of JComponent. A combobox is a visual swing
graphical component that gives a popuplist when clicked. It is combination of
JList and JTextField. Only one item is visible at a time. A popupmenu displays
the choices a user can select from. In JList the items cannot be edited while
here it can be.
Constructors
• JComboBox() -- creates an empty ComboBox
• JComboBox (Vector vec) -- creates a ComboBox with items from
specified vector vec
• JComboBox (object[] obj) -- creates a ComboBox with items from
specified object array
Methods
• int getSelectedIndex() -- returns the index of the selected item
• object getSelectedItem() -- returns the currently selected item
• object[] getSelectedValues() -- returns the array of all selected items
• void addItem(Object o) -- add the given object at end of list
• Object getItemAt() -- returns the item at the specified index
• void removeItem(Object o) -- removes the specified item from the list
• void removeItemAt(int index) -- removes the item at the specified index
in the list
• int getItemCount() -- returns the no. of items in the list
• void removeAllItems() -- removes all items from the list
Example : when user clicks on any item in the combobox and press the
button display it is displayed in label.
combobox1.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
cb=new JComboBox(s);
c.add(cb);
b=new JButton("Display");
b.addActionListener(this);
c.add(b);
l=new JLabel();
c.add(l);
}
public void actionPerformed(ActionEvent e)
{
l.setText((String)cb.getSelectedItem());
}
}
combobox2.java
public class combobox2
{
public static void main(String a[])
{
combobox1 f = new combobox1();
f.setSize(300,300);
f.setVisible(true);
}
}
ItemEvent
Methods
• void itemStateChanged(ItemEvent e) -- whenever there is change of
state in the JCombobox this method is executed
Example when user selects any item immediately that item is displayed in
the label
combobox3.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
}
public void itemStateChanged(ItemEvent e)
{
JComboBox d= (JComboBox)e.getSource();
l.setText((String)d.getSelectedItem());
}
}
combobox4.java
public class combobox4
{
public static void main(String a[])
{
combobox3 f = new combobox3();
f.setSize(300,300);
f.setVisible(true);
}
}
JScrollPane
JScrollPane is a subclass of JComponent. A JScrollPane is a visual component
that helps to scroll around a large sized item that is too big to be seen all at
once. JScrollPane provides a horizontal scrollbar and vertical scrollbar
automatically
There are 4 corners each corner is identified by the following int type
constants defined in the interface scrollpaneconstants
• ScrollPaneConstants.UPPER_LEFT_CORNER
• ScrollPaneConstants.UPPER_RIGHT_CORNER
• ScrollPaneConstants.LOWER_LEFT_CORNER
• ScrollPaneConstants.LOWER_RIGHT_CORNER
The top border is column header and left border is called row header the
right border represents an automatically adjustable vertical JScrollbar the
bottom border represents an automatically adjustable horizontal JScrollbar.
Whether a horizontal scrollbar is needed or not is defined by following
constants called HorizontalScrollbarPolicy
• ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED
• ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS
• ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
Methods
• int getHorizontalScrollBarPolicy() -- returns the constant that tells the
whether the horizontal scrollbar will be displayed as needed always or
never.
• Int getVerticalScrollBarPolicy() --
JMenuItem
It is part of JMenu display in window when a jmenu is clicked a popupmenu
appears displaying all menu items. Each menu item acts like a button by
clicking on it actions can be initiated.
Constructors
• JMenuItem() -- creates an empty menu item
• JMenuItem(Icon img) -- creates an menu item with a specified icon
• JMenuItem(String s) --
• JMenuItem(String s, Icon img) --
Methods
• void addActionListener(ActionEvent e) –
• String setText() --
• Icon getIcon() --
JMenu
It is a container for JMenuItem a menu can be attached with several
menuitems.
Constructors
• JMenu() -- creates an empty menu
• JMenu(String s) --
Methods
• void add(component c) –
• MenuItem add(JmenuItem m) --
• Int getItemCount() --
• Insert(JMenuItem m,int index) -- Inserts the specified menu item at the
specified index
JMenuBar
Constructors
• JMenuBar() --
Methods
• Add(Jmenu m) -- adds the specified item to the menubar
• Int getMenuCount() -- returnsthe no of items in the menubar
Example
menueg.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
Sweety Batavia Page 30
Swing
}
}
to perform some operations when menu item is clicked action event is used
to handle this
menueg2.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
JMenuBar mb;
JLabel l;
menueg2()
{
Container co= getContentPane();
co.setLayout(new FlowLayout());
a = new JMenuItem("aaaaaa");
b=new JMenuItem("bbbbbb");
c=new JMenuItem("cccccc");
d=new JMenuItem("dddddd");
font = new JMenu("font");
font.add(a);
font.add(b);
font.add(c);
font.add(d);
mb=new JMenuBar();
mb.add(font);
co.add(mb);
}
public void actionPerformed(ActionEvent e)
{
l.setText("aaaa");
}
}
menueg3.java
public class menueg3
{
public static void main(String a[])
{
menueg2 f = new menueg2();
f.setSize(300,300);
f.setVisible(true);
}
}
JTable
It is the component that displays the row and cols of data
Constructors
• JTable(Object data[][] , object colhead[]) -- here data is a2 dimensional
array of data that is to be displayed and col heads is a 1 dimensional
array with the column headings
tableeg.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
tableeg()
{
Container co= getContentPane();
co.setLayout(new FlowLayout());
String hd[] ={"Name","Per","Dept"};
String data[][]={ {"aaa","60","CE"},
{"bbb","70","CE"},
{"ccc","80","IT"}};
JTable t=new JTable(data,hd);
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int
h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp=new JScrollPane(t,v,h);
co.add(jsp);
}
}
tableeg1.java
public class tableeg1
{
public static void main(String a[])
{
tableeg f = new tableeg();
f.setSize(300,300);
f.setVisible(true);
}
}
JTree
It presents a hierarchial view of data. User has ability to expand or collapse
the data
Constructors
• JTree(DefaultMutableTreeNode t) --
Methods
• TreePath getPathForLocation(int x,int y) -- it will return the treepath of
node at specific location
How to create JTree
DefaultMutableTreeNode class is having add method that adds the child
node.
Example
treeeg.java
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
treeeg2.java
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
co.setLayout(new FlowLayout());
DefaultMutableTreeNode top = new
DefaultMutableTreeNode("Top");
DefaultMutableTreeNode a = new DefaultMutableTreeNode("a");
top.add(a);
DefaultMutableTreeNode a1 = new
DefaultMutableTreeNode("a1");
a.add(a1);
DefaultMutableTreeNode a2 = new
DefaultMutableTreeNode("a2");
a.add(a2);
}
treeeg3.java
public class treeeg3
{
here getLAstPAthComponent() method will return the name of node for which
we gettreepath