Swing component point are mentioned in PPT which helpgul for creating Java GUI Applications

sonalipatil225940 17 views 72 slides Sep 27, 2024
Slide 1
Slide 1 of 72
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72

About This Presentation

this ppt is helpful for creating GUI based on swing concept.


Slide Content

Unit No-2 Swing

Swing JComponent and JContainer Swing user interface is distributed into two parts 1. Component 2. Container Swing JComponent Swing component is an independent control such as button,label,textarea,textfields etc. They need a container to display. Components are derived from Jcomponent class. Jcomponent inherits from AWT class Container and Component. All Swing Component classes are under javax.swing packages.

Swing JComponent and JContainer 2.Swing JContainer JContainer is the area where components are placed or grouped. Swing defines two types of container. i > Heavy W eight Container ii> Light Weight Container i > Heavy Weight Container Heavy Weight Container are top-level containers that do not inherit from Jcomponent . These are JFrame,JApplet , Jdialog,JWindow

Swing JComponent and JContainer 2.Swing Jcontainer i > Heavy Weight Container Most common containers are Jframe and Japplet Top level containers defines set of panes. It Contents a fixed set of panes 1. Glass Pane( JPanel ) it is top level pane. It covers all other panes.it is used to handle the mouse events. 2.Layered Pane( JLayeredPane ) it is used to hold content pane and menu bar. 3.Content Pane( JPanel ) it is used to hold all visual components. Content Pane is accessed via getContentPane ().

Swing JComponent and JContainer Ii> Light Weight Container These containers inherit from Jcomponent . Containers are often used to Organize and manage groups of related components. Light Weight Containers supported by swing JPanel and JRootPane to create groups of related controls that are contained within container.

Swing Components 1-JFrame Frame is used to create top-level window having title, border and window buttons. Frame is an object of JFrame class. Frame is a standard graphical window. Constructor of Frame Class : JFrame () JFrame (String title) where title specifies the title for the frame. JFrame can be created by using two ways. By extending the Frame class. By creating an instance of a Frame class.

Methods of JFrame Class Methods Description void setResizable ( boolean resizable) sets frame to r esizable. void setTitle (String title ) sets the title of the frame. void setSize ( int width,int height) sets the width and height of a frame. String get Title () Obtain the title of the frame. void setVisible ( boolean visible) Sets the frame visible or not .

Program import javax.swing .*; import java.awt .*; class JFrameDemo extends JFrame { public JFramedemo () { Container ct = getContentPane (); JLabel l1=new Jlabel (“swing”); ct.add (l1); } public static void main(String args []) { JFrameDemo d=new JFrameDemo (); d .setTitle (“Frame Demo"); d.setSize (300,300); d.setVisible (true); d.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE ); } } Compile and Execute Program javac JFrameDemo.java java JFrameDemo

In JFrame we can directly close a JFrame window by using this method. This method is defined as void setDefaultCloseOperation ( int operation) This close operation is specified by three static constants: i > JFrame.EXIT_ON_CLOSE ii> JFrame.HIDE_ON_CLOSE iii> JFrame.DO_NOTHING_ON_CLOSE

2.JApplet The second type of application that commonly uses the swing components is the JApplet . Swing JApplet is same as AWT based applet.it extends JApplet instead of Applet class. JApplet extends from AWT Applet, hence the life cycle and all functionality found in Applet is achieved in JApplet .

Program Import javax.swing .*; Import java.awt .*; public class JAppletDemo extends JApplet { public void init () { Container ct = getContentPane (); Jlabel l1=new Label(“Applet-window”); ct.add (l1); } } /*<applet code=“ JAppletDemo " width="300" height="300">   </applet>  */

3.Icons In Swing, icons are encapsulated by the ImageIcon class, which paints an icon from an image. Icon is small fixed size picture, typically used to decorate components.  Two of its constructors are shown here: 1.ImageIcon(String filename) 2.ImageIcon(URL url ) The first form uses the image in the file named filename. The second form uses the image in the resource identified by url

3.Icons The ImageIcon class implements the Icon interface that declares the methods shown here: Method Description I > int getIconHeight ( ) :Returns the height of the icon in pixels. II> int getIconWidth ( ) :Returns the width of the icon in pixels. III> void paintIcon (Component comp, Graphics g, int x, int y):Paints the icon at position x,y on the graphics context g.

4.JLabel Swing labels are instances of the JLabel class, which extends JComponent . It is used to provide instructions and information. used to provide instructions and information. It can display text and/or an icon. Some of its constructors are shown here: 1-JLabel( ImageIcon obj ) 2-JLabel(String text) 3-JLabel(String text, ImageIcon obj , int align) The align argument is either LEFT, RIGHT,CENTER, LEADING, or TRAILING. These constants are defined in the Swing Constants interface, along with several others used by the Swing classes

The icon and text associated with the label can be read and written by the following methods: ImageIcon getIcon ( ) String getText ( ) void setIcon ( ImageIcon obj ) void setText (String text)

Example import java.awt .*; import javax.swing .*; /* <applet code=" JLabelDemo " width=“250” height=“150”> </applet> */ public class JLabelDemo extends JApplet { public void init () { Container contentPane = getContentPane (); ImageIcon ii = new ImageIcon ("IC.jpg"); JLabel jl = new JLabel ("IC", ii, JLabel.CENTER ); contentPane.add ( jl ); } }

5.TextField JTextField , which allows to edit one line of text. The JTextField is derived from JTextComponent Class. Some of its constructors are shown here: JTextField ( ) JTextField (String text) JTextField ( int max_chars ) JTextField (String text,int max_chars ) Here text is the String that is to be initialized and max_chars is maximum capacity of characters.

import java.awt .*; import javax.swing .*; /* <applet code=" JTextFieldDemo " width=“300” height=“50”> </applet> */ public class JTextFieldDemo extends JApplet { JTextField jtf ; public void init () { Container contentPane = getContentPane (); contentPane.setLayout (new FlowLayout ()); jtf = new JTextField (15); contentPane.add ( jtf ); } }

6.TextArea JTextArea is a derived class of JTextComponent . The AWT’s textarea deals with scrolling but JTextArea doesn’t manage scrolling. JTextArea can be placed inside a JScrollPane . Following are the constructors of JTextArea : JTextArea () JTextArea (String text) JTextArea ( int rows, int columns) JTextArea (String text, int rows, i nt columns)

Import javax.swing .*; Import java.awt .*; p ublic class JTextareademo extends JFrame { p ublic JTextareademo () { Container ct = getContentPane (); JTextArea area = new JTextArea (“This is java”,20,20 ); setBounds (50,100,70,30); ct.Add (area); } public static void main (String[] args ) { JTextareademo d = new JTextareademo (); d.setTitle (“ TextArea ”); d.setSize (500,500); d.setLayout (null); d.setVisible (true); d.setDefaultCloseOperation ( Jframe.EXIT_ON_CLOSE ); }

7.JComboBox Swing provides JComboBox class that creates a combo box. The combo box is combination of text field and a drop down list. By default it shows a drop down list box that allow user to select one item but user can also type his selection into the text field. Following are two constructors of JComboBox class JCombobox () JCombobox (Vector obj ) Items can be added manually into the combobox by using addItem () method. It is defined as: void addItem (Object obj );

Import javax.swing .*; Import java.awt .*; public class JComboboxdemo extends JFrame { public JComboboxdemo () { Container ct = getContentPane (); JComboBox jc = new JComboBox (); jc.addItem (“India”); jc.addItem (“Italy”); jc.addItem (“Japan”); jc.addItem (“France”); ct.Add ( jc ); } public static void main (String[] args ) { JComboboxdemo () d = new JComboboxdemo () d.setTitle (“Combo Box”); d.setSize (500,500); d.setVisible (true); d.setDefaultCloseOperation ( Jframe.EXIT_ON_CLOSE ); } }

Swing Buttons Jbutton JCheckBox JRadioButton

JButton The JButton class is used to create a labeled button. The swing button classes are subclasses of AbstractButton class In swing we can associate an icon and/or string with the JButton class. The JButton is very much different from AWT Button in many aspects. Constructors of JButton : JButton (String label) JButton ( ImageIcon obj ) JButton (String label, ImageIcon obj )

JButton Methods of JButton : void setLabel (String label) String getLabel () void setIcon ( ImageIcon obj ) ImageIcon getIcon () The base class AbstractButton also contains some methods that allows to control behavior of buttons. These methds are: Void setDisabledIcon (Icon obj ) Void setPressedIcon (Icon obj ) Void setSelectedIcon (Icon obj ) Void setRolloverIcon (Icon obj )

Import javax.swing .*; Import java.awt .*; public class JButtonDemo extends JFrame { public JButtonDemo () { Container ct = getContentPane (); JButton b = new JButton (“Click Here” ); setBounds (50,100,95,30); ct.add (b); } public static void main (String[] args ) { JButtonDemo d = new JButtonDemo (); d.setTitle (“Button window”); d.setSize (500,500); d.setLayout (null); d.setVisible (true); d.setDefaultCloseOperation ( Jframe.EXIT_ON_CLOSE ); } }

JCheckBox : The JCheckBox class is used to create a checkbox. It is used to turn an option on (true) or off (false). Clicking on a CheckBox changes its state from "on" to "off" or from "off" to "on ". It inherits  JToggleButton  class . It provides the facility to insert images also. Constructors of JCheckBox : JCheckeBox ( ImageIcon obj ) JCheckeBox ( ImageIcon , boolean state) JCheckeBox (String text) JCheckeBox (String text, boolean state) JCheckeBox (String text, ImageIcon obj ) JCheckeBox (String text, ImageIcon obj , boolean state)

JCheckBox : Methods of JCheckBox : void setText (String text) String getText () void setIcon ( ImageIcon obj ) ImageIcon getIcon () void setSelected ( boolean state) boolean isSelected ()

Import javax.swing .*; Import java.awt .*; public class JCheckBoxDemo extends JFrame { public JCheckBoxDemo () { Container ct = getContentPane (); JCheckBox ch1=new JCheckBox (“c”); JCheckBox ch2=new JCheckBox (“ c++ ”); ct.add (ch1); ct.add (ch2); } public static void main (String[] args ) { JCheckBoxDemo d = new JCheckBoxDemo (); d.setTitle (“ CheckBox window”); d.setSize (500,500 ); d.setVisible (true); d.setDefaultCloseOperation ( Jframe.EXIT_ON_CLOSE ); } }

JRadioButton : The JRadioButton class is used to create a radio button. It is used to choose one option from multiple options . JRadioButton is the immediate derived class of JToggleButton which provides two stated button. It provides the facility to insert images also Constructors of JRadioButton : JRadioButton( ImageIcon obj ) JRadioButton( ImageIcon , boolean state) JRadioButton(String text) JRadioButton(String text, boolean state) JRadioButton(String text, ImageIcon obj ) JRadioButton(String text, ImageIcon obj , boolean state)

Radio buttons must be configured into a group. Hence, only one of the radio button can be checked from a group at any time. If user selects a radio button then automatically previously selected radio button will get deselected. So, for this create object of ButtonGroup class with default constructor. Use add() method of ButtonGroup class to add radio button objects into a group. Syntax is : void add(JRadioButton obj )

Import javax.swing .*; Import java.awt .*; public class JRadioButtonDemo extends JFrame { public JRadioButtonDemo () { Container ct = getContentPane (); JRadioButton bt1=new JRadioButton(“Male”); JRadioButton bt2=new JRadioButton(“Female”); ct.add (bt1 ); ct.add (bt2); ButtonGroup bg1=new ButtonGroup (); bg1.add(bt1); bg1.add(bt2); } public static void main (String[] args ) { JRadioButtonDemo d = new JRadioButtonDemo (); d.setTitle (“ RadioButton window”); d.setSize (500,500); d.setVisible (true); d.setDefaultCloseOperation ( Jframe.EXIT_ON_CLOSE ); } }

Advanced Swing Components JTabbedPane JScrollPane JTable JProgressBar JToolTip Jslider Jtree JDialog

1. JTabbedPane : JTabbedPane is a component which appears as a group of multiple tabs, each tab containing a title and a panel as group of components. A tabbed pane is a component that appears as a group of folders in a file cabinet. Each folder has a title. When a user selects a folder, its contents become visible. Only one of the folders may be selected at a time. Tabbed panes are commonly used for setting configuration options Tabbed panes are extended by the JTabbedPane class, which extends from JComponent . Using the addTab method the tabs can be added to the tabbed pane. Syntax is, void addTab (String str , Component comp)

Constructors of JTabbedPane : There are three constructors of JTabbedPane . 1.JTabbedPane() -The first form creates an empty TabbedPane with a default tab placement of JTabbedPane.TOP . 2.JTabbedPane( int tabPlacement ) -Second form creates an empty TabbedPane with the specified tab placement of any of the following: JTabbedPane.TOP JTabbedPane.BOTTOM JTabbedPane.LEFT JTabbedPane.RIGHT

3.JTabbedPane( int tabPlacement , int tabLayoutPolicy ) The third form of constructor creates an empty TabbedPane with the specified tab placement and tab layout policy. Tab placements are listed above. Tab layout policy may be either of the following: 1.JTabbedPane.WRAP_TAB_LAYOUT 2.JTabbedPane.SCROLL_TAB_LAYOUT

Tabs are defined via the following method: void addTab (String str , Component comp) Here, str is the title for the tab, and comp is the component that should be added to the tab. The general procedure to use a tabbed pane in an applet is outlined here: 1. Create a JTabbedPane object. 2. Call addTab ( ) to add a tab to the pane. (The arguments to this method define the title of the tab and the component it contains.) 3. Repeat step 2 for each tab. 4. Add the tabbed pane to the content pane .

public class JTabbedPaneDemo extends Japplet { public void init() { JTabbedPane jtp = new JTabbedPane (); jtp.addTab ("Languages", new LangPanel ()); jtp.addTab ("Colors", new ColorsPanel ()); jtp.addTab ("Flavors", new FlavorsPanel ()); getContentPane ().add( jtp ); } }

class LangPanel extends JPanel { public LangPanel () { JButton b1 = new JButton ("Marathi"); add(b1); JButton b2 = new JButton ("Hindi"); add(b2); JButton b3 = new JButton ("Bengali"); add(b3); JButton b4 = new JButton ("Tamil"); add(b4); } } class ColorsPanel extends JPanel { public ColorsPanel () { JCheckBox cb1 = new JCheckBox ("Red"); add(cb1); JCheckBox cb2 = new JCheckBox ("Green"); add(cb2); JCheckBox cb3 = new JCheckBox ("Blue"); add(cb3); } }

class FlavorsPanel extends JPanel { public FlavorsPanel () { JComboBox jcb = new JComboBox (); jcb.addItem ("Vanilla"); jcb.addItem ("Chocolate"); jcb.addItem ("Strawberry"); add( jcb ); } }

2. JScrollPane JScrollPane is the class that provides a rectangular scrollable area in which a component can be viewed. Horizontal and/or vertical scroll bars may be provided. Scroll panes are implemented in Swing by the JScrollPane class, which extends JComponent . constructors : 1.JScrollPane() 2.JScrollPane(Component comp) 3.JScrollPane( int vsb , int hsb ) 4.JScrollPane(Component comp, int vsb , int hsb )

2. JScrollPane Here, comp is the component to be added to the scroll pane. vsb and hsb are int constants that define when vertical and horizontal scroll bars for this scroll pane are shown. These constants are defined by the ScrollPaneConstants interface. constants are described as follows: Constant Description HORIZONTAL_SCROLLBAR_ALWAYS Always provide horizontal scroll bar HORIZONTAL_SCROLLBAR_AS_NEEDED Provide horizontal scroll bar, if needed VERTICAL_SCROLLBAR_ALWAYS Always provide vertical scroll bar VERTICAL_SCROLLBAR_AS_NEEDED Provide vertical scroll bar, if needed

2. JScrollPane Here are the steps that you should follow to use a scroll pane in an applet: 1. Create a JComponent object. 2. Create a JScrollPane object. (The arguments to the constructor specify the component and the policies for vertical and horizontal scroll bars.) Add the scroll pane to the content pane.

2. JScrollPane Import java.awt.*; Import javax.swing .*; /*<applet code=“ JScrollPaneDemo ”, width=“300”,height=“300”> </applet>*/ public class JScrollPnaeDemo extends Applet { public void init() { Container ct= getContentpane (); JTextArea jt =new JTextArea (“This is text area displayed with scrollpane appears with horizontal and vertical scrollbars”); int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED ; int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED ; JScrollPane jsp = new JScrollPane ( jt , v, h); or JScrollPane jsp = new JScrollPane ( jt , ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS , ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS ); ct.add ( jsp ); } }

3.JTable A table is a component that displays rows and columns of data. We can drag the cursor on column boundaries to resize columns. We can also drag a column to a new position. Tables are implemented by the JTable class constructors is shown here: 1.JTable(Object data[ ][ ], Object colHeads [ ]) 2.JTable( int numRows , int numColumns ) 3.JTable(Vector rowData , Vector columnData ) Here, data is a two-dimensional array of the information to be presented, and colHeads is a one-dimensional array with the column headings. The “ numRows’”and “ numColumns ” are values with which the table is to be created. The “ rowData ” and “ columnData ” are the vector values by which the table is constructed.

Methods of JTable : 1. addColumn ( TableColumn []column) : adds a column at the end of the JTable . 2. getRowCount () : Returns the number of rows in the model 3. getColumns ( ) : retrieves the number of the columns of the current ResultSet object

3.JTable Here are the steps for to create table in scroll pane 1) Create a JTable object. 2) Create a JScrollPane object. 3) Add the table to the scroll pane. 4) Add the scroll pane to the content pane.

import java.awt.*; import javax.swing .*; /*<applet code=“ JTableDemo ”, width=“300”,height=“300”> </applet>*/ public class JTableDemo extends JApplet { public void init() { Container ct = getContentPane (); final String[] colHeads = { "Name", "Phone", "Fax" }; final Object[][] data = { { " Pramod ", "4567", "8675" }, { " Tausif ", "7566", "5555" }, { " Nitin ", "5634", "5887" }, { " Amol ", "7345", "9222" }, { " Vijai ", "1237", "3333" }, { " Ranie ", "5656", "3144" }, { " Mangesh ", "5672", "2176" }, { " Suhail ", "6741", "4244" }, { " Nilofer ", "9023", "5159" }, { " Jinnie ", "1134", "5332" }};

JTable table = new JTable (data, colHeads ); int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED ; int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED ; JScrollPane jsp = new JScrollPane (table, v, h); ct.add ( jsp ); } }

4.JProgressBar It used to indicate the progress of a task by showing the percentage of completion of task. The progress bar fills up as the task reaches it completion. In addition it shows the percentage of completion of task, it can also display some text. It is normally used while creating a GUI for downloading or transferring of file . The Progress bar can be of two types namely, Horizontal and Vertical. Progress Bar is an object of class JProgressBar which is subclass of Jcomponent .

4.JProgressBar Constructors defined by JProgressBar: Constructor Description JProgressBar() It is used to create a horizontal progress bar but no string text. JProgressBar(int min, int max) It is used to create a horizontal progress bar with the specified minimum and maximum value. JProgressBar(int orient) It is used to create a progress bar with the specified orientation, it can be either Vertical or Horizontal by using SwingConstants.VERTICAL and SwingConstants.HORIZONTAL constants. JProgressBar(int orient, int min, int max) It is used to create a progress bar with the specified orientation, minimum and maximum value.

4.JProgressBar Methods defined by JProgressBar: Method Description void setStringPainted(boolean b) It is used to determine whether string should be displayed. void setString(String s) It is used to set value to the progress string. void setOrientation(int orientation) It is used to set the orientation, it may be either vertical or horizontal by using SwingConstants.VERTICAL and SwingConstants.HORIZONTAL constants. void setValue(int value) It is used to set the current value on the progress bar.

4.JProgressBar import java.awt .*; import javax.swing .*; public class Demo extends JFrame { public Demo() { Container ct = getContentPane (); JProgressBar jb = new JProgressBar(0,300); jb.setValue (220); jb.setStringPainted ( true ); // jb.setString ("task remaining"); ct.add ( jb ); }

public static void main(String[] args ) { Demo d= new Demo(); d.setLayout ( new FlowLayout ()); d.setSize (400,400 ); d.setTitle (" ProgressBar "); d.setVisible ( true ); d.setDefaultCloseOperation ( JFrame. EXIT_ON_CLOSE ); } }

5.JToolTip Tooltip is very important component that shows a temporary pop-up message when the mouse pointer stays on a component. This pop-up message can be a short description or labelled information of any component. We can add tooltip text to almost all the components of java swing. Methods used:   1.getToolTipText () : returns the tooltip text for that component . 2.setToolTipText(String s) : sets the tooltip text for the component .

5.JToolTip import java.awt .*; import javax.swing .*; public class JToolTipDemo extends JFrame { public JToolTipDemo () { Container ct = getContentPane (); JPanel jp = new JPanel (); JTextArea t1= new JTextArea (10,10); t1.setToolTipText("This is text area"); jp.add (t1); ct.add ( jp ); }

p ublic static void main(String[] args ) { JToolTipDemo d= new JToolTipDemo (); d.setLayout ( new FlowLayout ()); d.setSize (400,400); d.setTitle ("ToolTip"); d.setVisible ( true ); d.setDefaultCloseOperation ( JFrame. EXIT_ON_CLOSE ); } }

6.JSlider JSlider is a component that allows the user to select a numeric value from a specified range of integer values. It has a knob which can slide on a range of values and can be used to select a particular value. Slider is an object of class JSlider which is extended from Jcomponent .

Constructor Description JSlider() creates a slider with the initial value of 50 and range of 0 to 100. JSlider ( int orientation) creates a slider with the specified orientation set by either JSlider.HORIZONTAL or JSlider.VERTICAL with the range 0 to 100 and initial value 50. JSlider(int min, int max) creates a horizontal slider using the given min and max. JSlider(int min, int max, int value) creates a horizontal slider using the given min, max and value. JSlider(int orientation, int min, int max, int value) creates a slider using the given orientation, min, max and value.  Constructors of JSlider class

import java.awt .*; import javax.swing .*; public class JSliderDemo extends JFrame { public JSliderDemo () { Container ct = getContentPane (); JPanel jp = new JPanel (); JSlider js = new JSlider (JSlider. HORIZONTAL,0,50,25); jp.add ( js ); ct.add ( jp ); } public static void main(String[] args ) { JSliderDemo d= new JSliderDemo (); d.setLayout ( new FlowLayout ()); d.setSize (400,400); d.setTitle ("Slider-Demo"); d.setVisible ( true); d.setDefaultCloseOperation ( JFrame. EXIT_ON_CLOSE ); } }

7JTree A tree is a component that presents a hierarchical view of data. Jtree is an expandable and collapsible component containing sub-trees to display. In swing the trees are implemented using the JTree class. This class expands the JComponent . JTree is a complex component. It has a 'root node' at the top most which is a parent for all nodes in the tree.

7JTree Some of its constructors are shown here: 1.JTree( Hashtable obj ) 2.JTree(Object obj [ ]) 3.JTree( TreeNode Obj ) 4.JTree(Vector Obj )

7JTree The MutableTreeNode interface declares methods that can insert and remove child nodes or change the parent node. The TreeNode interface declares methods that obtain information about a tree node. The DefaultMutableTreeNode class implements the MutableTreeNode interface. It represents a node in a tree. One of its constructors is shown here: DefaultMutableTreeNode (Object obj ) Here, obj is the object to be enclosed in this tree node. To create a hierarchy of tree nodes, the add( ) method of DefaultMutableTreeNode can be used. void add( MutableTreeNode child) Here, child is a mutable tree node that is to be added as a child to the current node.

Program import java.awt.*; import javax.swing .*; public class JTreeDemo extends JFrame { public JTreeDemo () { Container ct= getContentPane (); ct.setLayout (new BorderLayout ()); DefaultMutableTreeNode root=new DefaultMutableTreeNode (“India”); DefaultMutableTreeNode c1=new DefaultMutableTreeNode (“Maharashtra”); DefaultMutableTreeNode a=new DefaultMutableTreeNode (“Mumbai”); DefaultMutableTreeNode b=new DefaultMutableTreeNode (“ Pune ”); DefaultMutableTreeNode c=new DefaultMutableTreeNode (“ Nashik ”); DefaultMutableTreeNode d=new DefaultMutableTreeNode (“Nagpur”); DefaultMutableTreeNode c2=new DefaultMutableTreeNode (“ Gujrat ”); root.add (c1); root.add (c2);

c1.add(a); c1.add(b); c1.add(c); c1.add(d); JTree tree=new Jtree (root); ct.add (tree); } public static void main(String[] args ) { JTreeDemo d=new JTreeDemo (); d.setSize (400,400); d.setTitle (“ Tree -Demo "); d.setVisible (true); d.setDefaultCloseOperation ( JFrame. EXIT_ON_CLOSE ); } }

8.JDialog Dialog box is an independent pop-up window that display over its parent window. It is commonly used by almost all GUI Applications. It is always displayed in a top-level window and works like a frame window. Dialog box is created using JDialog class

8.JDialog Some of the constructors defined by Jdialog class are as follows: 1.JDialog() 2.JDialog(Frame owner); 3. JDialog (Frame owner , Boolean mode); 4. JDialog (Frame owner, String Title); 5. JDialog (Frame owner, String Title, Boolean mode); Where, Owner is the parent frame dialog box created. Mode specifies dialog box as modal or non-modal. Title is the title of the dialog box.

MVC Architecture (Model-View-Controller) The MVC is the most adopted component architecture that effectively contains :Model- view-Controller. In MVC Architecture : 1.The Model represents the information or properties associated with the component. 2.The View represents the way that component is displayed. 3.The Controller represents how the component reacts to user.

MVC Architecture (Model-View-Controller) In Java Programming, the Model contains the simple Java classes, the View used to display the data and the Controller contains the servlets.

MVC Architecture (Model-View-Controller) A client (browser) sends a request to the controller on the server side, for a page. The controller then calls the model. It gathers the requested data. Then the controller transfers the data retrieved to the view layer. Now the result is sent back to the browser (client) by the view
Tags