Selcting Multiple  CheckBoxe in a JTREE without holding CTRL / SHIFT

HI,
My JTree is having JCheckBOxes as nodes .... Now the problem is i want to select multiple nodes (checkboxes) with out holding CTRL/SHIFT key .
Please help regarding this issue ..
The code of my renderer class is as below:
* MyTreeRendered.java
* Created on April 27, 2006, 7:42 PM
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
public class MyTreeRendered extends DefaultTreeCellRenderer  {
    private JCheckBox cb ;
    /** Creates a new instance of MyTreeRendered */
    public MyTreeRendered() {
    public Component getTreeCellRendererComponent(
                        JTree tree,
                        Object value,
                        boolean sel,
                        boolean expanded,
                        boolean leaf,
                        int row,
                        boolean hasFocus){
       if(cb == null) {
           cb = new JCheckBox();
           cb.setBackground(tree.getBackground());
       DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
       cb.setText(node.getUserObject().toString());
       cb.setSelected(sel);
       return cb;
}

These are the methods which seem to define that behaviour:
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/plaf/basic/BasicTreeUI.html#isToggleSelectionEvent(java.awt.event.MouseEvent)
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/plaf/basic/BasicTreeUI.html#isMultiSelectEvent(java.awt.event.MouseEvent)
You'd have to write your own TreeUI and override them.

Similar Messages

  • Multiple Selection on JTree without holding down [CTRL] or [SHIFT]

    I need an example about how make multiple selection on JTree without holding down [CTRL] or [SHIFT].
    my JTree contains JCheckBox in any nodes, but I can't select two or more checkBox in time without holding down [CRTL] or [SHIFT].
    thanks for help.
    Jose A.

    I did this a few years ago so my newbiness is going to show through a bit, but I'm too lazy to update it.import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.tree.*;
    import java.util.*;
    public class Test3 extends JFrame {
      JTree jt = new JTree();
      MultiTreeSelectionModel mtsm = new MultiTreeSelectionModel(jt);
      JCheckBox multiCheck = new JCheckBox("Multi"),
          branchCheck = new JCheckBox("Branch");
      public Test3() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel content = (JPanel)getContentPane();
        multiCheck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                mtsm.setMultiSelect(multiCheck.isSelected());
        branchCheck.setText("Branch");
        branchCheck.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
                mtsm.setBranchSelect(branchCheck.isSelected());
        JPanel jp = new JPanel();
        jp.add(multiCheck);
        jp.add(branchCheck);
        content.add(jp, BorderLayout.NORTH);
        ActionListener specialKeyListener =
            new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    MultiTreeSelectionModel.keyModifiers = evt.getModifiers();
        KeyStroke keyStroke;
        for (int i = 0; i < keys.length; i++) {
            keyStroke = KeyStroke.getKeyStroke(keys[0], 0, true);
    content.registerKeyboardAction(specialKeyListener, keyStroke,
    JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    keyStroke = KeyStroke.getKeyStroke(keys[i][0], keys[i][1], false);
    content.registerKeyboardAction(specialKeyListener, keyStroke,
    JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    jt.setSelectionModel(mtsm);
    content.add(new JScrollPane(jt), BorderLayout.CENTER);
    setSize(300, 300);
    setVisible(true);
    public static void main(String[] args) { new Test3(); }
    private static int[][] keys = {{KeyEvent.VK_CONTROL, ActionEvent.CTRL_MASK},
    {KeyEvent.VK_SHIFT, ActionEvent.SHIFT_MASK},
    {KeyEvent.VK_ALT, ActionEvent.ALT_MASK}};
    class MultiTreeSelectionModel extends DefaultTreeSelectionModel {
    static int keyModifiers;
    private boolean branchSelect, multiSelect;
    private JTree tree;
    private TreePath[] savePaths;
    MultiTreeSelectionModel(JTree Tree) {
    tree = Tree;
    setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
    private boolean isSelected(TreePath Path) {
    return tree.isPathSelected(Path) && (keyModifiers & KeyEvent.SHIFT_MASK) == 0;
    private boolean branchSelect() {
    return branchSelect || ((keyModifiers & KeyEvent.ALT_MASK) != 0);
    public void addSelectionPaths(TreePath[] paths) {
    if (branchSelect()) paths = getAllPaths(paths);
    super.addSelectionPaths(paths);
    public void removeSelectionPaths(TreePath[] paths) {
    if (branchSelect()) paths = getAllPaths(paths);
    super.removeSelectionPaths(paths);
    public void setSelectionPaths(TreePath[] paths) {
    if (branchSelect()) {
    paths = getAllPaths(paths);
    if (paths != null && paths.length > 0 && isSelected(paths[0])) {
    super.removeSelectionPaths(paths);
    } else if (multiSelect) super.addSelectionPaths(paths);
    else super.setSelectionPaths(paths);
    protected TreePath[] getAllPaths(TreePath[] paths) {
    if (paths == null || paths.length == 0) {
    return paths;
    Vector vector = new Vector();
    DefaultMutableTreeNode treeNode, thisNode;
    for (int i = 0; i < paths.length; i++) {
    if (paths[i] != null) {
    thisNode = (DefaultMutableTreeNode) paths[i].getLastPathComponent();
    Enumeration enumeration = thisNode.preorderEnumeration();
    while (enumeration.hasMoreElements()) {
    // add all descendants to vector
    treeNode = (DefaultMutableTreeNode) enumeration.nextElement();
    TreePath treePath = new TreePath(treeNode.getPath());
    vector.add(treePath);
    int i = vector.size();
    TreePath[] allpaths = new TreePath[i];
    for (int j = 0; j < i; j++) {
    allpaths[j] = (TreePath) vector.elementAt(j);
    return allpaths;
    protected void setMultiSelect(boolean b) { multiSelect = b; }
    protected boolean isMultiSelect() { return multiSelect; }
    protected void setBranchSelect(boolean b) { branchSelect = b; }
    protected boolean isBranchSelect() { return branchSelect; }
    protected void savePaths(TreePath Path) {
    TreePath[] tmpPaths = getSelectionPaths();
    if (tmpPaths == null) {
    savePaths = null;
    } else {
    int cnt = 0;
    for (int i = 0; i < tmpPaths.length; i++) {
    if (tmpPaths[i].isDescendant(Path)) {
    cnt++;
    savePaths = new TreePath[cnt];
    cnt = 0;
    for (int i = 0; i < tmpPaths.length; i++) {
    if (tmpPaths[i].equals(Path) || tmpPaths[i].isDescendant(Path)) {
    savePaths[cnt++] = tmpPaths[i];
    protected void restorePaths() {
    if (savePaths != null) {
    final DefaultTreeSelectionModel foo = this;
    SwingUtilities.invokeLater(
    new Runnable() {
    public void run() {
    foo.setSelectionPaths(savePaths);

  • Cannot open links in new tab without holding Ctrl

    There is only one option i see in preferences for opening sites in new tabs. and it is called "Open links from applications..."
    Even with this checked, by default all links open in a new window, unless I hold down Ctrl while clicking the link.
    There MUST be a way to set ALL links to open in a new tab like every other browser I've used. No??

    Every single time this topic is raised, someone tries to claim there is a work-around, while seemingly forgetting that this is the Safari for Windows forum, and not the Safari for Mac forum.
    This is the single biggest glaring omission from Safari for Windows. It completely defeats the purpose of even having tabs, which makes me wonder what in the world were the developers thinking.
    Safari is a very pretty and fast browser, but I don't know many people who actually use it on a Windows machine due to the inability to simply open links in a new tab without right clicking or holding some extra button down. It's also the only mainstream browser for Windows that isn't capable of this simple task.
    Having watched this issue since Safari 3, without ever seeing any response other then "oh, but just do this work-around" (that only works for Macs), my guess is we'll never see it happen. The funny thing is that when you look at the ini files that can be changed on the Mac version, they are pretty much identical to the Windows version. So, the "fix" not working simply means that whoever was in charge of the part of code related to tabs when porting to Windows must have consciously decided there was no need for the ability to open links in new tabs.
    Proving once again that software engineers can be utterly clueless about the usability of their software.

  • I want to make an Easter Egg on a site, where a page will only open if you hold 'ctrl-shift' and click. Can I do this in MUSE?

    Trying to embed some 'secret' content onto a site, and I want to throw in this little ode to 'The Net' because it would amuse me....is there a way to do this in Adobe Muse?

    Hi Sam,
    Please refer to the following link Creating an Enter Page/Screen
    You can create the Easter egg like a page that loads before the index page as mentioned in that forum post. You can also add javascript to recognize the CTRL+Shift and click and then allow the user to enter the site. You can add the Javascript in HTML for head section of the page.
    Regards,
    Aish

  • Create multiple root in a JTree??????very urgent..pl

    hello
    i have a jtree which takes a defaultMutableTreenode as its parameter.
    My problem is that i have to create multiple root.
    and what i get with this is a single root at the top.
    what is the solution for creating multiple roots in a jtree.
    pl. help its very urgent
    thanx
    hussain

    Hello Hussain,
    I found in the sourcecode of JTree the following constructor which might is be helpful for you:
    * Returns a <code>JTree</code> with each element of the specified
    * <code>Vector</code> as the
    * child of a new root node which is not displayed. By default, the
    * tree defines a leaf node as any node without children.
    * @param value a <code>Vector</code>
    * @return a <code>JTree</code> with the contents of the
    *          <code>Vector</code> as children of the root node
    * @see DefaultTreeModel#asksAllowsChildren
    public JTree(Vector value) {
    this(createTreeModel(value));
    this.setRootVisible(false);
    this.setShowsRootHandles(true);
    Greetings
    Helmut

  • Is there any way to display JTree without using applet

    Hi,
    is there any way to display JTree without using applet . Can we display the JTree in a JSP page.
    With Regards,
    Sheema.

    Not a JTree, per se. But there are Javascript solutions out there and there are JSP tag library solutions which use a JTree on the server side to hold and maintain the data and expanded nodes.
    This is one that I've used before, it's pretty good:
    http://www.jenkov.dk/treetag/introduction.tmpl

  • Problem with multiple checkbox

    Hello Experts,
    I have one issue related to multiple checkbox in my JSP.I have a table in JSP with 3 columns.Last column is checkbox.I want to get data for only those teams which are selected in JSP , but I am able to get value for only checkbox column (in my case 'select') .I want to get data for other column also(In my case it is 'Marks').Please tell me what should I do to get marks also for those teams which got selected.Many Thanx in advance.
    This is my JSP
            <html:form action="/AddFirstRoundWinner.do">
                    <table width="100%"
                           border="2" cellspacing="0" cellpadding="0">
                        <tr align="left">
                            <td>Team Name</td>
                            <td>Marks</td>
                            <td>select</td>
                        </tr>
                        <!-- iterate over the results of the query -->
                    <logic:iterate id="FirstroundresultForm" name="FirstRoundDetails">
                            <tr align="left">
                                <td>
                                    <bean:write name="FirstroundresultForm" property="team_name" />
                                </td>
                                <td>
                                    <input type="textbox" readonly="true" name="marks" value="<bean:write name="FirstroundresultForm"                property="first_round_total_marks" /> "/>
                               </td>
                                <td>
                                    <input type="checkbox" name="select_team" value="<bean:write name="FirstroundresultForm"
                                                                                                        property="team_id"/>"/>
                                </td>
                            </tr>
                        </logic:iterate>
                    </table><br><br>
                    <html:submit property="step"> <bean:message key="button.select"/> </html:submit>
            </html:form>  This is my code in action class
    // here I am fetching all the teams which were selected using checkbox
    String select_team[];
            select_team = request.getParameterValues("select_team");
            if (select_team != null) {
                for (int i = 0; i < select_team.length; i++) {
                    result_list.add(select_team);

    <input type = "text" ..../> <!-- and not textbox as in your code-->ram.

  • HT202213 Can multiple iPhones share one iTunes without syncing the phones together? We just got new iPhones and set up desperate apple id's, but don't know if we can both sync to our existing iTunes without linking our phones together (contacts, apps, etc

    Can multiple iPhones share one iTunes without syncing the phones together? We just got new iPhones and set up seperate apple id's, but don't know if we can both sync to our existing iTunes without linking our phones together (contacts, apps, etc) ????

    How to use multiple iPods, iPads, or iPhones with one computer

  • Select Multiple Rows in a Table without CTRL

    Expecting the user to press "Ctrl" when selecting multple rows is very unfriendly and unintuitive. We'd like the row selection to work as in ALV where you just select multiple rows by clicking on them.
    We've set the tables rowSelectable to true and selectionMode to 'multi' but we still cannot select multiple rows without the CTRL hotkey.
    This issue apparently [arose before|About selection in the table] but wasn't resolved.
    System Details
    SAP_ABA     701     0006     SAPKA70106
    SAP_BASIS     701     0006     SAPKB70106
    SAP_AP     700     0019     SAPKNA7019

    Hello Marc,
    you need to call IF_WD_CONTEXT_NODE->set_selected(index = lv_index) sorry for the typo in my previous comment.
    by calling IF_WD_CONTEXT_NODE->set_selected method wont remove the lead selection.
    to unselect the records, you can call the same method by passing the FLAG value as abap_false.
    so for your usecase the logic will be like this in the ON_SELECT event handler
    1. get the index of the new_lead_selection
    2. check whether this is already seleted in the context node by calling IF_WD_CONTEXT_NODE->IS_selected
    3. if already selected then call IF_WD_CONTEXT_NODE->set_selected( flag = abap_false index = lv_index)
       if not selected then call IF_WD_CONTEXT_NODE->set_selected( index = lv_index )
    Hope this solved your problem.
    BR, Saravanan
    Edited by: Saraa_n on Jul 6, 2011 11:52 AM

  • How to delete multiple songs from iPhone 5S without losing form iTunes? The unchek function has not worked. Why?

    How to delete multiple songs from iPhone 5S without losing form iTunes? The unchek function has not worked. Why?

    Sorry I had to reply through your profile Gail from Maine, my PC has java issues. In any event, when I delete them directly from my device everything is perfect and cool. However, in the rare instance I want to add new music that I actually buy in stores (I know, quite the unique and old-fashioned idea...but hey Im an audiophile) once I upload the tunes, everytime I sync my library it re-adds everything that I spent hours deleting. In a perfect world, I thought I could maintain a massive iTunes Library, and add or delete (remove) songs from my iPhone to save both memory or keep my iPhone selections more current/apt to my musical "tastes" at that time. I know about the whole playlist thing, but thought there might be an easier way. ie - checking/un-checking the little box next to the song name, and then doing a sync. Again, everytime I do this however, whether everything is checked or un-checked it adds the entire library! So frustrating. Any suggestions. Thank you graiously in advance for your help.

  • Hiding a intermediate node in a Jtree without hiding leaf nodes

    Hi who can help me out of this problem. What my prob is dat i m interrested in hiding few intermediate nodes in JTree without hiding their children.
    Thanks
    Rakesh

    but how do you then expand to see the children? once the node is hidden, can you by any means expand/collapse it to view/hide the child leaves?

  • What's happening on my iMac?  There are two choices of selected start up disks appearance without hold down option key.  On the other hand, it start up normally show apple picture when I hold option key.

    There are two choices of selected start up disks:Main HD and Recovery HD, appearance without hold down option key.  On the other hand, it start up normally show apple picture when I hold option key.

    Hi artdiva28
    Welcome to Apple Discussions
    First go to *System Preferences > Startup Disk* > select the *Mac OS X 10.5. on Macintosh HD* > click the Lock > hit the Restart button.
    Then if the iMac is still having trouble starting up, insert your Install Disk and restart holding the Option key. In Startup Manager select the Install Disk and boot into it, choose your language, skip the Installer and go up on the menu bar to Utilities and open Disk Utility. In Disk Utility select your Macintosh HD and hit the Repair button. Once finished note any errors and what was repaired for future reference. Quit Disk Utility and restart once again holding the Option key boot back into the Macintosh HD and eject the Install Disk.
    Also see > http://support.apple.com/kb/HT2956
    Dennis

  • Possible to overwrite a pdf when in use or open a pdf without holding/locking the file for update

    Possible to overwrite a pdf when in use OR alternative open a pdf without holding/locking the file
    We frequently generate pdf files from our 3D program and would like to know if it's possible to either open a pdf file in a way that it doesn't get locked or hold up so the pdf can be updated even when opened or in use.
    Most programs e.g. ultra edit & notepad will let go the file when you opened and hence the file can be updated or over written even when in use.
    If any guru can help, would be highly appreciated

    Hi Abbas,
    It's not possible to overwrite a pdf when it is already in use by a different program or user. The file then opens up in Read Only mode.
    You can make a copy of the file and then apply those changes later in the original file. That's the only possible solution.
    Regards,
    Rahul

  • How to create JTree without root node

    Hello;
    I like to create a JTree without root node?
    Any help?
    Thanks!
    --tony                                                                                                                                                                                   

    javadocs JTree,
    setRootVisible
    public void setRootVisible(boolean rootVisible)
    Determines whether or not the root node from the TreeModel is visible.
    Parameters:
    rootVisible - true if the root node of the tree is to be displayedSee Also:
    rootVisible

  • I'm running an iBook G4. On occasion, a black and white display appears (in multiple languages) and requires restarting by holding the power button.

    I'm running an iBook G4. On occasion, a black and white display appears (in multiple languages) and requires restarting by holding the power button.

    What you are seeing is a kernel panic.
    Check out the X Lab's article on Resolving Kernel Panics. Maybe something there will help.

Maybe you are looking for

  • Can't email contents of page

    Hi all I use to be able to send html emails with Safari by simply clicking mail contents of this page. Now whenever I click it, I get the following message.. ''An email message can't be created because Safari can't find an email application. You can

  • " ! Cannot send message - Message has no valid recipient "

    Android version 4.4.4 Sony Xperia Z3 Compact When I use the pre-installed Messaging app I get the following error message for some but not all contacts " [!] Cannot send message  Message has no valid recipient " I can't work out what is is about the

  • Dashcode And iWeb(mobileme) upload problem

    Long story, short, I'm trying to make my Mobile Me Blog RSS feed into an iphone web app. In Dashcode I have the rss template set up and in the Application attributes URL Feed I have : http://web.me.com/username/MyAnnouncesments.rss.xml. I have alread

  • Negative amount in the open deliveries field

    Hi All Iu2019ve also come across a negative amount in the open deliveries field on the business partners master data screen but there are no open deliveries no orange arrow to click on and it's grayed out for this selected business partner could anyb

  • HT1420 Unable to reauthorise my home computer in iTunes...

    I had to deauthorise this computer and my work computer in iTunes because my work computer died.  Now I am trying to reauthorise my home computer again under "my account".  The option to authorise this computer is not available.  How can I do this??