How to hide a tree node from the GUI but still keep it in the tree model?

Hi, All
I used a JTree in my project in which I have a DefaultTreeModel to store all the tree structure and a JTree show it on the screen. But for some reason, I want to hide some of the nodes from the user, but I don't want to remove them from the tree model because later on I still need to use them.
I searched on the web, some people suggested method to hide the root node, but that's not appliable to my project because I want to hide some non-root nodes; Some people also suggested to collapse the parent node when there are child to hide, it is not appliable to me either, because there still some other childnodes (sibling of the node to hide) I want to show.
How can I hide some of the tree node from the user? Thanks for any information.
Linda

Here's an example using a derivation of DefaultTreeModel that shows (or does not show) two types of Sneech (appologies to the good Dr Zeus) by overiding two methods on the model.
Now, there are many things wrong with this example (using instanceof, for example), but it's pretty tight and shows one way of doing what you want.
Note: to make it useful, you''d have to change the implementation of setShowStarBelliedSneeches() to do something more sophisticated than simply firing a structure change event on the root node. You'd want to find all the star bellied sneech nodes and call fireTreeNodesRemoved(). That way the tree would stay expanded rather than collapse as it does now.
import javax.swing.JTree;
import javax.swing.JScrollPane;
import javax.swing.JOptionPane;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.tree.TreePath;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
class FilteredTree
     private class PlainBelliedSneech {
          public String toString() { return "Plain Bellied Sneech"; }
     private class StarBelliedSneech {
          public String toString() { return "Star Bellied Sneech"; }
     private class FilteredTreeModel
          extends DefaultTreeModel
          private boolean mShowStarBelliedSneeches= true;
          private DefaultMutableTreeNode mRoot;
          FilteredTreeModel(DefaultMutableTreeNode root)
               super(root);
               mRoot= root;
          public Object getChild(Object parent, int index)
               DefaultMutableTreeNode node=
                    (DefaultMutableTreeNode) parent;
               if (mShowStarBelliedSneeches)
                    return node.getChildAt(index);
               int pos= 0;
               for (int i= 0, cnt= 0; i< node.getChildCount(); i++) {
                    if (((DefaultMutableTreeNode) node.getChildAt(i)).getUserObject()
                                        instanceof PlainBelliedSneech)
                         if (cnt++ == index) {
                              pos= i;
                              break;
               return node.getChildAt(pos);
          public int getChildCount(Object parent)
               DefaultMutableTreeNode node=
                    (DefaultMutableTreeNode) parent;
               if (mShowStarBelliedSneeches)
                    return node.getChildCount();
               int childCount= 0;
               Enumeration children= node.children();
               while (children.hasMoreElements()) {
                    if (((DefaultMutableTreeNode) children.nextElement()).getUserObject()
                                        instanceof PlainBelliedSneech)
                         childCount++;
               return childCount;
          public boolean getShowStarBelliedSneeches() {
               return mShowStarBelliedSneeches;
          public void setShowStarBelliedSneeches(boolean showStarBelliedSneeches)
               if (showStarBelliedSneeches != mShowStarBelliedSneeches) {
                    mShowStarBelliedSneeches= showStarBelliedSneeches;
                    Object[] path= { mRoot };
                    int[] childIndices= new int[root.getChildCount()];
                    Object[] children= new Object[root.getChildCount()];
                    for (int i= 0; i< root.getChildCount(); i++) {
                         childIndices= i;
                         children[i]= root.getChildAt(i);
                    fireTreeStructureChanged(this, path, childIndices, children);
     private FilteredTree()
          final DefaultMutableTreeNode root= new DefaultMutableTreeNode("Root");
          DefaultMutableTreeNode parent;
          DefaultMutableTreeNode child;
          for (int i= 0; i< 2; i++) {
               parent= new DefaultMutableTreeNode(new PlainBelliedSneech());
               root.add(parent);
               for (int j= 0; j< 2; j++) {
                    child= new DefaultMutableTreeNode(new StarBelliedSneech());
                    parent.add(child);
                    for (int k= 0; k< 2; k++)
                         child.add(new DefaultMutableTreeNode(new PlainBelliedSneech()));
               for (int j= 0; j< 2; j++)
                    parent.add(new DefaultMutableTreeNode(new PlainBelliedSneech()));
               parent= new DefaultMutableTreeNode(new StarBelliedSneech());
               root.add(parent);
               for (int j= 0; j< 2; j++) {
                    child= new DefaultMutableTreeNode(new PlainBelliedSneech());
                    parent.add(child);
                    for (int k= 0; k< 2; k++)
                         child.add(new DefaultMutableTreeNode(new StarBelliedSneech()));
               for (int j= 0; j< 2; j++)
                    parent.add(new DefaultMutableTreeNode(new StarBelliedSneech()));
          final FilteredTreeModel model= new FilteredTreeModel(root);
          JTree tree= new JTree(model);
tree.setShowsRootHandles(true);
tree.putClientProperty("JTree.lineStyle", "Angled");
          tree.setRootVisible(false);
          JScrollPane sp= new JScrollPane(tree);
          sp.setPreferredSize(new Dimension(200,400));
          final JCheckBox check= new JCheckBox("Show Star Bellied Sneeches");
          check.setSelected(model.getShowStarBelliedSneeches());
          check.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e) {
                    model.setShowStarBelliedSneeches(check.isSelected());
          JPanel panel= new JPanel(new BorderLayout());
          panel.add(check, BorderLayout.NORTH);
          panel.add(sp, BorderLayout.CENTER);
          JOptionPane.showOptionDialog(
               null, panel, "Sneeches on Beeches",
               JOptionPane.DEFAULT_OPTION,
               JOptionPane.PLAIN_MESSAGE,
               null, new String[0], null
          System.exit(0);
     public static void main(String[] argv) {
          new FilteredTree();

Similar Messages

  • I am getting a pop up on my iPhone 4S asking password of my iCloud even though I feed the password but still keep on asking the same thing every second so I am not able to use my iPhone.

    I am getting a pop up on my iPhone 4S asking password of my iCloud even though I feed the password but still keep on asking the same thing every second so I am not able to use my iPhone.at various times it asks different passwords.

    Curretly, there is an iCloud blackout.  It started this morning around 2AM California time (Pacific), 5AM New York (Eastern).  A lot of people have been having issues all day, including me.  No iCloud services are online.  Even Find My iPhone is down.

  • Deactivate Portal server node from Web dispatcher, but still keep online

    We want to deactivate a Portal server node from the Web dispatcher. We also need to connect directly to the specific node. 
    We configured the Server Node using  Config Tool --> Server_ID####### --> Debug --> Debuggable --> Restricted Load Balancing. The node is not available to the Web dispatcher, However we are unable to explicitly connect to that node. We get the error message in the browser:
    503 Service Unavailable
    SAP WebAS engine is starting...
    If this state does not change within a few minutes,
    please contact your system administrator.
    Check the recommendations in SAP Notes: 943498, 764417
    Message: Dispatcher running but no server connected!
    I have also read through the Web dispatcher online Help. They mention creating groups but they all seem to pertain to ABAP groups using SMLG. Our Web dispatcher is used in a strictly J2EE environment.
    There must be a Profile setting or some config to restrict specified nodes... Let me know if you have found out?
    Thank You,
    Jon Sells

    Hi PR,
    Yes I understand that the Web Dispatchers Job ends at that point. That's why I want to stop the requests at the Web dispatcher by not allowing those nodes to be available.
    Here is the situation. We have 3 Windows Servers with 10 Server Nodes spread accros them.
    Host A - 2 Server Nodes, Central instance (Message Server) and SQL Server DB.
    Host B - 4 Server Nodes.
    Host C - 4 Server Nodes.
    The Going Live Analysis suggested we move the DB off of the CI. We cannot do that at this time. What we can do is shut down the 2 nodes on Host A. Instead of shutting them down, we would rather remove them from the WD group. that way, our users never connect to the 2 nodes on the CI but Basis can still use them for Administration and those 2 nodes on the CI are never used for production purposes. Those nodes will just be used for monitoring and maintenance or even emergency purposes.
    Right now our process to remove the 2 nodes from the Web dispatcher is to connect to the Admin page for Web Dispatcher --> Monitor Server Groups --> Right Click on the Host and select Deactivate. We have to do this whenever the Message Server is retarted. The million dollar question: How can that be set via a startup parameter?
    Thanks
    Jon Sells
    Here is an example of how you can connect to a specific node:
    https://<Host_name>.domain.local/b2b/b2b/init.do;sapj2ee_irj=7501753

  • I added a photo from camera roll to a created album,how can I delete that photo from camera roll and still keep photo in album

    I added a photo from camera roll to a created album, how can I delete that photo from camera roll and still keep same photo in album?

    You can't. It doesn't work that way.
    the new album that you created doesn't contain a copy of the actual photo. It contains a pointer to that photo. If you delete it from the camera roll, you will delete it from the album that you created as well. Albums that you create on the iPad in the Photos app are for local organization only and cannot be transfer or imported to your computer, because there really are no photos in those albums

  • I am trying to install the updates from Mac AppStore but it keeps aborting saying the it cannot verify the PreFlight File and that it's not signed by Apple. What is going on?

    I am trying to install the updates from Mac AppStore but it keeps aborting saying the it cannot verify the PreFlight file and that it's not signed by Apple. What is going on?

    Just attempted to run the update and still rec'd the same message. I also see that there are many others who are havig the exact same problem. How can I get Apple support to look at it?

  • I've followed all the instructions to the letter but still can't access the signatures in my Mail account. Any ideas?

    I've followed all the instructions to the letter but still can't access the signatures in my Mail account. Any ideas?

    Hello,
    In Mail Preferences>Signatures, what shows for All Sigs, And the ones assigned to a particular account?
    Then in Compose a new Mail, what shows for Sig choices?

  • How can I upload pictures onto my icloud account on my computer from my phone, then delete them on my phone but still keeping them on the computer?

    It seems absolutely so easy but I don't know how to upload the pictures from my phone onto my icloud account on my computer. I used the Beta but then I was using too much data. I only want some pictures available on icloud from my laptop and I want to delete the same pictures from my phone to save space. I'm scared if I delete them and they are gone from both my laptop and phone I won't be able to get them back!

    If you do not have access to the iTunes Library on your Old computer... or its Backup...
    See these 2 Links...
    Syncing to a New Computer...
    https://discussions.apple.com/docs/DOC-3141
    Recovering your iTunes library from your iPod or iOS device
    https://discussions.apple.com/docs/DOC-3991
    If you do have access... See Here...
    iTunes: How to move your music to a new computer

  • I had to reset my computer and redownload Itunes. My iphone 5 will not connect to the new itunes i have trusted the computer but it keeps bringing up the same question each time i reconnect to the computer. How can i get my phone to connect to itunes

    I had to reset my computer because of a virus i had. When i re installed itunes my music was gone. I went to plug in my Iphone 5 and know my phone does not recognize the computer. I also have hit the trust button on my iphone but have to continue trusting the computer each time it gets plugged in. Still the phone wont connect to itunes at all to view what is on the phone. How do i fix this connection problem?

    Hey ebe301,
    Sorry to hear about your troubles with your PC. If you are not seeing your iPhone on your computer, then take a look at the link below to walk you through getting that working again. It will have you verify that drivers are installed and working as well as any third party software that may be causing conflicts. 
    iPhone, iPad, or iPod not recognized in iTunes for Windows
    https://support.apple.com/en-us/HT204095
    Take care,
    -Norm G.  

  • Itunes does not open. I have tried to uninstall it and install it many times as i have seen on some communities but still keep on getting the Itunes was not install correctly. Please reinstall itunes. Error 7 (Windows 193). I have windows 7.

    iTunes will not open, I keep getting the error 7 (windows error 193).
    I have tried many recommendations, erasing folders, reinstalling it many time but nothing.
    I have a windows 7 Lenovo ideapad.

    For general advice see Troubleshooting issues with iTunes for Windows updates.
    The steps in the second box are a guide to removing everything related to iTunes and then rebuilding it which is often a good starting point unless the symptoms indicate a more specific approach. Review the other boxes and the list of support documents further down the page in case one of them applies.
    Your library should be unaffected by these steps but there is backup and recovery advice elsewhere in the user tip.
    If you are running 64-bit Windows and the above steps haven't helped try installing the version of iTunes that can be downloaded from this support document. iTunes 12.1 for Windows (64-bit — for older video cards)
    tt2

  • Is it possible to delete photos from an iPhone but still keep them on iCloud?

    I currently have a lot of photos on my iPhone(5c) and I need to have more storage. If I delete directly from my phone will they still be available on iCloud?

    before i was using my photo stream and i could access the photos from all the devices but since the last update I only have one album "all photos" and every time i try to delete a photos it says that it will be deleted from iCloud as well. I also tried importing them on my macbook but i can only import 600 photos and on my iPhone there are over 2000 photos.

  • I need to download Firefox 3.6 and have tried to do so from your site, but it keeps going to the update. How can I get 3.6?

    The newer versions of Firefox are incompatible with my website builder. Tech support advised me that Firefox 3.6 would work.

    To add onto what Seburo says:
    Firefox 3.6 came out in 2010. It is VERY old in terms of software and especially web browsers. Firefox is currently on version 36.0.4 and version 37 comes out next week. You should ALWAYS be running the latest version of a web browser. Don't believe me? If you are using 3.6, you are vulnerable to most of the bugs at https://www.mozilla.org/en-US/security/known-vulnerabilities/firefox/, and your browser can't use all the new features http://en.wikipedia.org/wiki/History_of_Firefox#Version_4.0 from version 4 down (there are lots). You are stuck in technology 5 years old and can't use all the modern awesomeness.
    Honestly, if your website building tool only supports Firefox 3.6, you should consider finding a new tool. There are lots of modern web technologies (like HTML5, CSS3, etc.) that that tool is probably forcing you to miss out on.
    If you really need to use that tool, can't upgrade, and firefox 3.6 is the only version that works (I'd try using it with modern Firefox, it might work) then use the portable version of Firefox 3.6 and use regular Firefox for all your regular surfing. (http://sourceforge.net/projects/portableapps/files/Mozilla%20Firefox,%20Portable%20Ed./Mozilla%20Firefox,%20Portable%20Edition%203.6.28/)

  • How can i set up a new apple id but still keep all my details from my old id

    i have a new i phone 5s and an old 4s as well as my husband 4s linked to the same apple id, i want to set up the new apple id on my husbands 4s but am scared that i will lose all thee data i.e photos contacts and such like. is there anyway that i can do this ??

    Discussions on using purchases from multiple AppleIDs in one iTunes library - https://discussions.apple.com/message/19543804
    As I mentioned earlier, the main time when this becomes an issue is if you need to do something involving associating a computer with a particular AppleID.  Careful management of your collection should minimize this situation.
    iTunes Store: Associating a device or computer to your Apple ID - http://support.apple.com/kb/HT4627 -  In connection with, "When you turn on iTunes Match or Automatic Downloads, or when you download past purchases on an iOS device or computer, that device or computer becomes associated with your Apple ID." "Your Apple ID can have up to 10 devices and computers (combined) associated with it. Each computer must also be authorized using the same Apple ID. Once a device or computer is associated with your Apple ID, you cannot associate that device or computer with another Apple ID for 90 days." - Additionally instructions for "Removing an associated device or computer from an Apple ID"
    So the first account is really "yours" and you are setting her up with her own account?  It helps to know this because if both are "hers" then there isn't an issue with her having full access to both accounts.  If in 10 years she moves 2000 miles away and she is pretty much independent then you may not want her to have full access to your AppleID just so she can authorize a device.

  • Have deleted a song from I tunes but still it remains on the I phone 4

    Latest version of I tunes,on a I Phone 4 IOS5,have deleted certain tracks from within I tunes but they remain on my I phone any ideas please.

    When did you buy/download it ? If it's still in your country's store, and it's not an iPad-only game, then it should show in the Purchased tab in the App Store app on your iPhone, and the Purchased link under Quicklinks on the right-hand side of the iTunes store homepage on your computer's iTunes. If it's not showing there, and it's still in the store, then check to see if it's hidden : http://support.apple.com/kb/HT4919

  • Re: How can I delete an email on my phone but still keep it on my computer?

    Allen,
    Not very computer savy old man has the opposite question. How can I a delete an email from either laptop or iPhone and also have it deleted on the other automatically?
    Jumping between the compaq laptop with vista and new iPhone 3Gs is driving me nuts.
    Thanks, Dick Ripper
    <Email Edited by Host>

    With an IMAP account which is designed for this. With an IMAP account, all server stored mailboxes are kept synchronized with the server with each email client used to access the account.

  • I just bought my iPad Air and I'm trying to use iBooks but it's saying that  I need iOS device with iBooks.  I downloaded the app but still can't open the book

    How do I enable my iPad Air to use iBooks

    You should download and install iBooks first and then buy books from the iBooks Store. You worked backwards. Where did you but the book? Or where did you download it from?

Maybe you are looking for

  • After 1 year, I finally solved the Flash Player 100% CPU issue!

    I created this guide to help people with this problem. http://sgsoft.bravehost.com/flashcpuproblem.html My problem was after watching/doing Flash videos or anything Flash related, my CPU would go up to 100% and whole computer slows down to a point I

  • PDF characters displayed backwards

    I am using cfreport to generate PDF reports. When I view these reports in my browser with Adobe Reader 7, all the characters are displayed in reverse|mirror image. It seems to be just me, because my coworker can view the files with his Adobe Reader 7

  • Ipad app that will fill in pdf form and capture signature with stylus

    Hello.  I am looking for an application which will allow me to read, edit and fill out pdf forms AND capture a signature on the pdf form with a stylus.  Any help would be greatly appreciated.  I currently am using an ipad, second generation.

  • Problem of getting user defined element

    Hello collegues ! I have following problem I need to check, if my element was creanted on form or not For this i do following : if(oOrderForm.Items.Item("myItem")!=null) but this give me an exception are there another ways to check, if element exists

  • Iphone 6 double tap home button freeze

    I've just switched from iPhone 5 to iPhone 6 and have everything restored.  But notice that when i double tap the home screen to access other apps, the screen freezes for at least 2 secs before i may swipe anything. This did not happen when i was usi