Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Enable and Disable Multiple Selections in a JTree Component

                         

In this section, you will learn how to enable and disable the multiple selections in a JTree component. The multiple selections in tree component that means user will allow or disallow the permission for selection the more than one tree component at a time. By default, the tree allows the multiple selections.

Description of program:

The following program helps you in enabling and disabling the multiple selections in a JTree component. First of all, this program constructs a simple tree that contains the root and child of root node. After creating a tree, you will allow the selection component of tree. If you want to select only one node at a time, you will use the SINGLE_TREE_SELECTION, if you will provide the selection to one vertical contiguous set of visible nodes, you will use the CONTIGUOUS_TREE_SELECTION and if you will allow the permission for multiple selection of visible nodes, you will use the DISCONTIGUOUS_TREE_SELECTION. This tree are displayed on the java swing frame.

Description of code:

getSelectionModel():
This is the method that returns the current selection model. 

setSelectionMode():
 This is the method that returns the value of selectionMode property either single-item or multiple-item.

TreeSelectionModel:
This is an interface that represents the current state of selection to the tree component. The TreeSledtionModel allows the selection only one path at a time by using the SINGLE_TREE_SELECTION, a number of contiguous paths to use the CONTIGUOUS_TREE_SELECTION  or a number of discontiguous paths to use the DISCONTIGUOUS_TREE_SELECTION.

SINGLE_TREE_SELECTION:
It allows the permission for selecting only one path at a time.

CONTIGUOUS_TREE_SELECTION:
It allows the permission for selection to contiguous.

DISCONTIGUOUS_TREE_SELECTION:
It allows the selection that contains any number of items but can not be necessary contiguous.

Here is the code of program:

import javax.swing.*;
import javax.swing.tree.*;

public class SelectionTree{
  public static void main(String[] args) {
    JFrame frame = new JFrame("Enabling and Disabling Multiple Selections in a 
   JTree Component!"
);
    DefaultMutableTreeNode myComputer = new DefaultMutableTreeNode("My Computer");
    DefaultMutableTreeNode c = new DefaultMutableTreeNode("Local Disk(C:)");
    DefaultMutableTreeNode vinod = new DefaultMutableTreeNode("Vinod");
    DefaultMutableTreeNode swing = new DefaultMutableTreeNode("Swing");
    DefaultMutableTreeNode tr = new DefaultMutableTreeNode("Tree");
    DefaultMutableTreeNode a = new DefaultMutableTreeNode("3½ Floppy(A:)");
    DefaultMutableTreeNode e = new DefaultMutableTreeNode("New Volume(E:)");
    c.add(vinod);
    vinod.add(swing);
    swing.add(tr);
    myComputer.add(c);
    myComputer.add(a);
    myComputer.add(e);
    JTree tree = new JTree(myComputer);
//    Single selection node at a time
//    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.
    SINGLE_TREE_SELECTION);

//    provide selection to one vertical contiguous set of visible nodes
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.
 CONTIGUOUS_TREE_SELECTION
);
//    multiple selection of visible nodes
//    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.
  DISCONTIGUOUS_TREE_SELECTION);

    frame.add(tree);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setUndecorated(true);
    frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
    frame.setSize(200,200);
    frame.setVisible(true);
  }
}

Download this example.

Output of program:

Single selection node:

Selection to one vertical contiguous set of visible nodes:

Multiple selection of visible nodes:

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Latest Searches:
where we place plug-in
create table in struts
if not null condition
abstrct path name of f
howtodeclareanarrayinj
JPOX
dynamic insert value J
How to show expression
populate drop down fro
file input output
dwr reverse ajax
b tree implementation
jboss seam
iBatis Stored Procedur
Photoshop Effects Sig
using set in java
date of birth
swing
session jsp
find and replace bytes
Photoshop Color Colori
<c:if
Passing Parameters in
width
ASP EXAMPLE and 1=2
Photoshop Text Effects
Get Image
MULTIPLE SELECT COMBO
dojo debug
call ejb j2ee5
count row in resultset
JSF tree collapse
spring MVC application
strating a thread in j
jar entry
objective
set colors to Jtree
Java Database Related
*copyvalueof
DOWNLOAD JAVA HOME DIR
flex
count the number of di
java nodequeue tutoria
myfaces tomahawk
java date compare exam
Developing JSP files
read hexadecimal from
free source jsp n serv
struts.
string length using if
for loop tutorial
frame in html
xml stream
Date Examples java
Hibernate Tools Update
secure
String to array in jav
coldfusion tutorials
hibernate connection p
Update in jsp
dynamic array value
c:out in var of c:fore
paypal
logic
Photoshop Effects Grun
redirecting jsp
mandatory
jpanel
count
string conceptsring ma
convert object into in
javapdfs
PHP User Authenticatio
Merge Sort In Java
access modifiers
<html:submit/>
multiple submit button
how to modify table da
Rename the File or Dir
jazn login module
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.