Sitemap

Amicuk Programming Answers

How to show only all children of selected node in JTree??

2015-10-11   Views:3

Advertisement

Dear friends: I have Two Panels, PA and PB, PA has a Jtree as code below, and PB listens to PA, I hope to do following, If I select a node called A in PA, then Node A's all children such as A1, A2, A3 will be displayed in PB, but not display A1, A2,

Dear friends:
I have Two Panels, PA and PB,
PA has a Jtree as code below, and PB listens to PA,
I hope to do following,
If I select a node called A in PA, then Node A's all children such as A1, A2, A3 will be displayed in PB, but not display A1, A2, A3's children such as A3 has C1, C2, C3, C4 & C5, until I select A3 then PB will display only all A3's children: C1, C2, C3, C4 & C5;
i.e, only populate each ONE level of children of Node A or any node I select, not its grandchildren and its grand-grand children;
Please help how to do it??
I tried amny times, failed.
Thanks
[1]. PA panel code:
package com.atest;
     import java.awt.BorderLayout;
     import java.awt.event.MouseAdapter;
     import java.awt.event.MouseEvent;
     import java.util.Enumeration;
     import java.awt.Dimension;
     import javax.swing.JFrame;
     import javax.swing.JPanel;
     import javax.swing.JScrollPane;
     import javax.swing.JTextField;
     import javax.swing.JTree;
     import javax.swing.tree.DefaultMutableTreeNode;
     import javax.swing.tree.TreeModel;
     import javax.swing.tree.TreePath;
     public class DefaultMutableTreeMain extends JPanel {
     protected DefaultMutableTreeNode    top = new DefaultMutableTreeNode("Options");
     protected DefaultMutableTreeNode      selectedNode = null;
     protected final JTree tree;
     protected final JTextField jtf;
    protected Enumeration      vEnum = null;
     private      TreeModel                m;
     protected  DefaultMutableTreeNode      getDefaultMutableTreeNode()  {
          //textArea.getText();
               return selectedNode;
     protected  DefaultMutableTreeNode setDefaultMutableTreeNode(DefaultMutableTreeNode tt)  {
          //textArea.getText();
               selectedNode = tt;
               return selectedNode;
     protected  TreeModel getJTModel()  {
          //textArea.getText();
               return m;
     protected  TreeModel setJTModel(TreeModel ta)  {
               m = ta;
               return m;
       public DefaultMutableTreeMain() {
         setSize(300,300);
         setLayout(new BorderLayout());
         DefaultMutableTreeNode a = new DefaultMutableTreeNode("A");
         top.add(a);
         DefaultMutableTreeNode a1 = new DefaultMutableTreeNode("A1");
         a.add(a1);
         DefaultMutableTreeNode a2 = new DefaultMutableTreeNode("A2");
         a.add(a2);
         DefaultMutableTreeNode a3 = new DefaultMutableTreeNode("A3");
         a.add(a3);
         DefaultMutableTreeNode b = new DefaultMutableTreeNode("B");
         top.add(b);
         DefaultMutableTreeNode b1 = new DefaultMutableTreeNode("B1");
         b.add(b1);
         DefaultMutableTreeNode b2 = new DefaultMutableTreeNode("B2");
         b.add(b2);
         DefaultMutableTreeNode b3 = new DefaultMutableTreeNode("B3");
         b.add(b3);
         DefaultMutableTreeNode c = new DefaultMutableTreeNode("C");
         a3.add(c);
         DefaultMutableTreeNode c1 = new DefaultMutableTreeNode("C1");
         c.add(c1);
         DefaultMutableTreeNode c2 = new DefaultMutableTreeNode("C2");
         c.add(c2);
         DefaultMutableTreeNode c3 = new DefaultMutableTreeNode("C3");
         c.add(c3);
         DefaultMutableTreeNode c4 = new DefaultMutableTreeNode("C4");
         c.add(c4);
         DefaultMutableTreeNode c5 = new DefaultMutableTreeNode("C5");
         c.add(c5);
         tree = new JTree(top);
         JScrollPane jsp = new JScrollPane(tree);
         jsp.setPreferredSize(new Dimension(400,300));
         add(jsp, BorderLayout.CENTER);
         jtf = new JTextField("", 20);
         add(jtf, BorderLayout.SOUTH);
           tree.addMouseListener(new MouseAdapter() {
           public void mouseClicked(MouseEvent me) {
              TreePath   path = tree.getSelectionPath();
              DefaultMutableTreeNode      selectedNode = (DefaultMutableTreeNode)path.getLastPathComponent();
             TreePath tp = tree.getPathForLocation(me.getX(), me.getY());
             setDefaultMutableTreeNode(selectedNode);
                 System.out.println("Current node selected is (tp.toString()=" + tp.toString());
                 System.out.println("Current node selected is getDefaultMutableTreeNode()=" + getDefaultMutableTreeNode());
             if (tp != null){
                 jtf.setText(tp.toString());
                  System.out.println("It Has Children as selectedNode.getChildCount()= " + selectedNode.getChildCount());
                        Enumeration vEnum = selectedNode.children();
                            int i = 0;
                            while(vEnum.hasMoreElements()){
                                System.out.println("2 selectedNode = " +  path.toString() + "  has " + i++ + " Children in vEnum.nextElement(" + i + ") = " + vEnum.nextElement());
             else
               jtf.setText("");
       public static void main(String[] args) {
         JFrame frame = new JFrame();
         frame.getContentPane().add(new DefaultMutableTreeMain());
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setSize(400, 400);
         frame.setVisible(true);
     }[2]. PB Panel code
package com.atest;
import java.awt.BorderLayout;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.JPanel;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.*;
import javax.swing.JButton;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
public class DefaultMutableTreeSub extends JPanel implements java.io.Serializable{
  private JButton removeButton;
  private JButton addButton;
  JTree tree;
  private TreeModel      m;
  protected TreeDragSource ds;
  protected TreeDropTarget dt;
protected  TreeModel getJTModel()  {
          //textArea.getText();
               return m;
protected  TreeModel setJTModel(TreeModel ta)  {
               m = ta;
               return m;
protected DefaultTreeModel model;
protected DefaultMutableTreeNode rootNode;
DefaultMutableTreeMain dmm = null;
JPanel inputPanel  = new JPanel();
  public JPanel SLTreeDNDEditableDynamic(DefaultMutableTreeMain tdnd ) {
    //super("Rearrangeable Tree");
    setSize(400,450);
    dmm = tdnd;
         setLayout(new BorderLayout());
         inputPanel.setLayout(new BorderLayout());
         JPanel outputPanel = new JPanel();
         System.out.println("Sub selectedNode tdnd= " + tdnd);
         tdnd.tree.addTreeSelectionListener(new TreeSelectionListener(){
              public void valueChanged(TreeSelectionEvent evt){
              TreePath[] paths = evt.getPaths();
              TreePath   path = dmm.tree.getSelectionPath();
              DefaultMutableTreeNode      selectedNode = (DefaultMutableTreeNode)path.getLastPathComponent();
             DefaultMutableTreeNode itemNode = dmm.getDefaultMutableTreeNode();
                 System.out.println("Sub node selected is dmm.getDefaultMutableTreeNode()=" + dmm.getDefaultMutableTreeNode());
              model = new DefaultTreeModel(itemNode);
              tree = new JTree(model);
              System.out.println("Sub selectedNode paths= " + paths);
              System.out.println("Sub selectedNode path= " + path);
              System.out.println("Sub selectedNode = " + selectedNode);
              System.out.println("Sub itemNode = " + itemNode);
              tree.putClientProperty("JTree.lineStyle", "Angled");
              tree.setRootVisible(true);
               inputPanel.add(new JScrollPane(tree),BorderLayout.CENTER);
         return inputPanel;
     public DefaultMutableTreeSub() {
          super();
}thanks
sunny

The replay answer
Advertisement
Thanks so much, I use your code and import followig:
import java.util.ArrayList;
import java.awt.List;
but
private static List<Object> getChildNodes(JTree j) {
     Object parent = j.getLastSelectedPathComponent();
     int childNodeCount = j.getModel().getChildCount(parent);
     List<Object> results = new ArrayList()<Object>;
     for (i = 0; i < childNodeCount; i++) {
          results.add(parent, i);
     return results;
here List<Object> and ArrayList()<Object> show red,
Is my JDK version problem??
my one is JKD
C:\temp\swing>java -version
java version "1.4.2_08"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_08-b03)
Java HotSpot(TM) Client VM (build 1.4.2_08-b03, mixed mode)
Error as follows:
C:\temp\swing>javac DefaultMutableTreeSub.java
DefaultMutableTreeSub.java:38: <identifier> expected
private static List<Object> getChildNodes(JTree j) {
^
1 error
any idea??
Thanks

Go to See the other 6 answers

How to show only all children of selected node in JTree??

Category:DefaultRelease time:2015-10-11Views:130

Dear friends: I have Two Panels, PA and PB, PA has a Jtree as code below, and PB listens to PA, I hope to do following, If I select a node called A in PA, then Node A's all children such as A1, A2, A3 will be displayed in PB, but not display A1, A2,[More]

Change icon of a non selected node in JTree

Category:DefaultRelease time:2015-10-11Views:130

Hello I have a swing application contening a JTree. I'd like to know how to change the icon of a non selected node in my tree. I have the information about the node and its path. I try to change userObject information and the icon of the node with th[More]

How to get the selected node value of a tree which is build on java code

Category:DefaultRelease time:2015-10-11Views:130

Hi Experts, How can i get the selected node value if I build the tree programatically. I am using the following code in selectionListener but it is throwing error. RichTreeTable treeTable = (RichTreeTable)getQaReasontreeTable(); CollectionModel _tabl[More]

Viewing a url selection from a JTree

Category:DefaultRelease time:2015-10-11Views:130

Hi, I was just wondering if someone can give me some help. I am trying to get a selected node from a JTree which is a url which has been converted into a string to add to the JTree. I use the following code to obtain the selected node by the user: Tr[More]

Hot
Okay, dont laugh (if it is stupid question) But when i connect my new 2GB ipod to either of my 2 computers i get message with a triangle like on this picture: http://a248.e.akamai.net/7/248/51/610031142535203/www.info.apple.com/images/kbas e/61003/61 [More]
dear Collegues,     I have an issue, my client want me to configure Free goods exclusive ie 10+2 offer, but when i am creating a sales order or billing, the Net value will be for 10 items, Excise will be calculated on 12 pieces  as its a manufacturin [More]
i entered the wrong E-mail address when it asked me for my Apple ID, now my card is useless and i dont have the £15 credit i paid for, i was wondering, if i gave you the code, would you be able to trace what address i typed in (Which will be similar [More]
Hi! I love the animated gif feature and have used it several times successfully. This time, it seems to ignoring my settings for duration. I've set the number as high as 1500/100, but when I preview in browser (Mac Safari or Firefox), the images just [More]
Hi All, We are getting a "file not found error " while using a SFTP sender channel . Our Scenario : We are using SFTP as the source adapter .Presently we are using  two nodes for load balancing for all the sender channels. One of the node's of t [More]
Hi all, wounder if anyone can help me. I have connected my iMac to a Sony Bravia 28" LCD TV, I mirror my mac screen to the TV, my mac screen apares on the TV in widescreen format ok. FR all works fine apart from one thing. Any video's, music video's [More]
I am trying to combine two users from my old computer to just one user on my new computer. what folders of itunes do I need to transfer so we can have just one log in to the computer with two different itunes accounts? Basically, I'm trying to take t [More]
I had already update mi new factory unlocked iPhone 4S to iOS 5.1.1 and I still having problems with "invalid sim". I'm from Argentina and here we don't have official service. PLEASE HELP ME, WHAT CAN I DO??? In Argentina is too expensive an iPh [More]
I made an AppleScript to clean up my calendars and thought I would share. My iMac had a calendar called "Home" and my iPhone had a default calendar "Calendar" so I ended up with all of my events spread across two calendars. Decide to c [More]
So I was about to use bootcamp to install Windows 7 yesterday, but then I saw that I needed either a CD/DVD or a USB drive for the support software (already have a dvd with Windows 7). Instead I updated the OS and then planned to try again today afte [More]