Skip to main content

Java Swing: JSeparator, JTree

Java JSeparator

The object of JSeparator class is used to provide a general purpose component for implementing divider lines. It is used to draw a line to separate widgets in a Layout. It inherits JComponent class.


JSeparator class declaration

  1. public class JSeparator extends JComponent implements SwingConstants, Accessible  

Commonly used Constructors of JSeparator

ConstructorDescription
JSeparator()Creates a new horizontal separator.
JSeparator(int orientation)Creates a new separator with the specified horizontal or vertical orientation.

Commonly used Methods of JSeparator

MethodDescription
void setOrientation(int orientation)It is used to set the orientation of the separator.
int getOrientation()It is used to return the orientation of the separator.

Java JSeparator Example 1

  1. import javax.swing.*;    
  2. class SeparatorExample    
  3. {    
  4.           JMenu menu, submenu;    
  5.           JMenuItem i1, i2, i3, i4, i5;    
  6.           SeparatorExample()  {    
  7.           JFrame f= new JFrame("Separator Example");    
  8.           JMenuBar mb=new JMenuBar();    
  9.           menu=new JMenu("Menu");    
  10.           i1=new JMenuItem("Item 1");    
  11.           i2=new JMenuItem("Item 2");       
  12.           menu.add(i1);  
  13.           menu.addSeparator();  
  14.           menu.add(i2);  
  15.           mb.add(menu);    
  16.           f.setJMenuBar(mb);    
  17.           f.setSize(400,400);    
  18.           f.setLayout(null);    
  19.           f.setVisible(true);    
  20. }    
  21. public static void main(String args[])    
  22. {    
  23. new SeparatorExample();    
  24. }}    

Output:

Java Jseparator 1

Java JSeparator Example 2

  1. import javax.swing.*;    
  2. import java.awt.*;  
  3. public class SeparatorExample    
  4. {    
  5.     public static void main(String args[]) {  
  6.         JFrame f = new JFrame("Separator Example");      
  7.         f.setLayout(new GridLayout(01));  
  8.         JLabel l1 = new JLabel("Above Separator");  
  9.         f.add(l1);  
  10.         JSeparator sep = new JSeparator();  
  11.         f.add(sep);  
  12.         JLabel l2 = new JLabel("Below Separator");  
  13.         f.add(l2);  
  14.         f.setSize(400100);  
  15.         f.setVisible(true);  
  16.       }  
  17.     }    

Output:

Java Jseparator 2

Java JProgressBar

The JProgressBar class is used to display the progress of the task. It inherits JComponent class.

JProgressBar class declaration

Let's see the declaration for javax.swing.JProgressBar class.

  1. public class JProgressBar extends JComponent implements SwingConstants, Accessible  

Commonly used Constructors:

ConstructorDescription
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.

Commonly used Methods:

MethodDescription
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.

Java JProgressBar Example

  1. import javax.swing.*;    
  2. public class ProgressBarExample extends JFrame{    
  3. JProgressBar jb;    
  4. int i=0,num=0;     
  5. ProgressBarExample(){    
  6. jb=new JProgressBar(0,2000);    
  7. jb.setBounds(40,40,160,30);         
  8. jb.setValue(0);    
  9. jb.setStringPainted(true);    
  10. add(jb);    
  11. setSize(250,150);    
  12. setLayout(null);    
  13. }    
  14. public void iterate(){    
  15. while(i<=2000){    
  16.   jb.setValue(i);    
  17.   i=i+20;    
  18.   try{Thread.sleep(150);}catch(Exception e){}    
  19. }    
  20. }    
  21. public static void main(String[] args) {    
  22.     ProgressBarExample m=new ProgressBarExample();    
  23.     m.setVisible(true);    
  24.     m.iterate();    
  25. }    
  26. }    

Output:

JAVA Jprogressbar 1

Java JTree

The JTree class is used to display the tree structured data or hierarchical data. JTree is a complex component. It has a 'root node' at the top most which is a parent for all nodes in the tree. It inherits JComponent class.

JTree class declaration

Let's see the declaration for javax.swing.JTree class.

  1. public class JTree extends JComponent implements Scrollable, Accessible  

Commonly used Constructors:

ConstructorDescription
JTree()Creates a JTree with a sample model.
JTree(Object[] value)Creates a JTree with every element of the specified array as the child of a new root node.
JTree(TreeNode root)Creates a JTree with the specified TreeNode as its root, which displays the root node.

Java JTree Example

  1. import javax.swing.*;  
  2. import javax.swing.tree.DefaultMutableTreeNode;  
  3. public class TreeExample {  
  4. JFrame f;  
  5. TreeExample(){  
  6.     f=new JFrame();   
  7.     DefaultMutableTreeNode style=new DefaultMutableTreeNode("Style");  
  8.     DefaultMutableTreeNode color=new DefaultMutableTreeNode("color");  
  9.     DefaultMutableTreeNode font=new DefaultMutableTreeNode("font");  
  10.     style.add(color);  
  11.     style.add(font);  
  12.     DefaultMutableTreeNode red=new DefaultMutableTreeNode("red");  
  13.     DefaultMutableTreeNode blue=new DefaultMutableTreeNode("blue");  
  14.     DefaultMutableTreeNode black=new DefaultMutableTreeNode("black");  
  15.     DefaultMutableTreeNode green=new DefaultMutableTreeNode("green");  
  16.     color.add(red); color.add(blue); color.add(black); color.add(green);      
  17.     JTree jt=new JTree(style);  
  18.     f.add(jt);  
  19.     f.setSize(200,200);  
  20.     f.setVisible(true);  
  21. }  
  22. public static void main(String[] args) {  
  23.     new TreeExample();  
  24. }}  

Output:

JAVA Jtree 1




Anurag Rana Educator CSE/IT

Comments

Popular posts from this blog

JAVA Scrollbar, MenuItem and Menu, PopupMenu

ava AWT Scrollbar The  object  of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a  GUI  component allows us to see invisible number of rows and columns. AWT Scrollbar class declaration public   class  Scrollbar  extends  Component  implements  Adjustable, Accessible   Java AWT Scrollbar Example import  java.awt.*;   class  ScrollbarExample{   ScrollbarExample(){               Frame f=  new  Frame( "Scrollbar Example" );               Scrollbar s= new  Scrollbar();               s.setBounds( 100 , 100 ,  50 , 100 );               f.add(s);               f.setSize( 400 , 400 );               f.setLayout( null );               f.setVisible( true );   }   public   static   void  main(String args[]){           new  ScrollbarExample();   }   }   Output: Java AWT Scrollbar Example with AdjustmentListener import  java.awt.*;   import  java.awt.event.*;   class  ScrollbarExample{        ScrollbarExample(){               Frame f=  new  Frame( "Scro

Difference between net platform and dot net framework...

Difference between net platform and dot net framework... .net platform supports programming languages that are .net compatible. It is the platform using which we can build and develop the applications. .net framework is the engine inside the .net platform which actually compiles and produces the executable code. .net framework contains CLR(Common Language Runtime) and FCL(Framework Class Library) using which it produces the platform independent codes. What is the .NET Framework? The Microsoft .NET Framework is a platform for building, deploying, and running Web Services and applications. It provides a highly productive, standards-based, multi-language environment for integrating existing investments with next-generation applications and services as well as the agility to solve the challenges of deployment and operation of Internet-scale applications. The .NET Framework consists of three main parts: the common language runtime, a hierarchical set of unified class librari

C++ this Pointer, static, struct and Enumeration

C++ this Pointer In C++ programming,  this  is a keyword that refers to the current instance of the class. There can be 3 main usage of this keyword in C++. It can be used  to pass current object as a parameter to another method. It can be used  to refer current class instance variable. It can be used  to declare indexers. C++ this Pointer Example Let's see the example of this keyword in C++ that refers to the fields of current class. #include <iostream>    using   namespace  std;   class  Employee {       public :           int  id;  //data member (also instance variable)               string name;  //data member(also instance variable)            float  salary;          Employee( int  id, string name,  float  salary)             {                   this ->id = id;                  this ->name = name;                  this ->salary = salary;            }             void  display()             {                 cout<<id<< "  " <<name<&