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]

Setting custom color for selected node in JTree?

Category:DefaultRelease time:-0001-11-30Views:130

Hi, i want to set my own color for selected node in JTree. i don't want to set for all the nodes...how to set Color for Selected node using TreeCellRender class? Thanks ManiI assume you are not setting a custom tree cell renderer... javax.swing.JTree[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 select node in JTree without firing event?

Category:DefaultRelease time:-0001-11-30Views:130

I have got standard situation. JTree in the left panel, and several edit boxes in right panel. Certainly, I have TreeSelectionListener, which on every tree node selection shows corresponding model values in edit boxes. I'd like to implement next logi[More]

Coloring selected nodes in JTree

Category:DefaultRelease time:-0001-11-30Views:130

i am currently using setCellRenderer() for highlighting a selected node in my JTree.but when i select another node, the previously selected node loses its highlight.i would like to know of how i could make the highlight in the selected nodes remain p[More]

JTree: How to get the currently selected node

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

How do I get the currently selected node in JTree? getLastSelectedPathComponent() this method always return the last selected node and not the current one. Thanks in advance SachinUse TreePath selectedPath = tree.getSelectionPath()If your tree allows[More]

HOW TO COUNT IMMEDIATE CHILDREN OF A NODE?

Category:DefaultRelease time:-0001-11-30Views:130

Hi I need count the immediate children for all node of a tree with analitic functions. Somebody have any idea?. tyHi, Sg049 wrote: Frank, OP was asking for Immediate children.That's right. The query I posted shows how many immediate children each nod[More]

Help trying to delete selected node tree.

Category:DefaultRelease time:-0001-11-30Views:130

Hello All, I'm hoping someone can give me some insight into what I'm trying to do. I've got a tree that gets populated through an xml data provider. Basically i'm building an XML editor for a very specific application and I want to be able to delete[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]

How could i get the selected node in the JTree

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

getLastSelectedPathComponent() �returns the parent node of currenr selected, so how could I get the selected node itself in the JTree I will appretiate for any help!i think you can get by.... TreePath treePath = tree.getSelectionPath(); DefaultMutabl[More]

Hot
Hi all, I am stuck at a point I am looking for a system stored procedure named 'sp_dboption' in SQL Server 2012  & R2 but could not find. Searched for it on the net and found that, this stored procedure 'sp_dboption' is now obsolete. So I tried to co [More]
I thought my Superdrive was failing.....but it appears it's a 'software error.' Any program (Toast, DVD2OneX, Disk Utility) that needs to burn DVDs, can't. However, CDs still burn just fine. This problem has been occuring over the last week. Before t [More]
Hello, The mail service on my server is turning itself off automatically. I can turn it back on and for a few moments it will run, but them it shuts-off. The System log is awash with messages relating to postfix and dovecot, but I can't understand wh [More]
Hello SAP Guru, I want to replace one mic in mass for that i got transaction QS27 but when i go to overview screen system show me lock in first coloum i tried mic in refrance mode & copy modal mode also but not able to replace it please suggest. rega [More]
hello all can anyone please help me out to find information on how i can use xml within web appliaction. I am aware about the basics of xml (that it is raw but structured information and can be rendered in different formats) but still i would like to [More]
good evening all, Could you please let me know when the new Lumia 1020 is going to be available in Middel east? Thank you in advanced.. Solved! Go to Solution.sorry to hear that, it may be a case that they don't have any immediate plans to sell the 1 [More]
Hi can anyone tell me if its possible to add a vorder colour and a background to the selected radio button and text on this website http://62.6.176.22/products/leaflets.aspx?Id=145399&Key=F8FA69A7-4AC5-4986-97F3-6C8C9520FA 87. This is a site which is [More]
my word window will not maximise. what can i do?There are two buttons to "maximize", neither of which does the same thing as on Windows. The green button expands the window to the largest extent necessary to display the content. So if your Word [More]
Good evening everyone.  I've been trying to import .xmp files alongside some of my old movies to save me from having to retype the IPTC info such as location, etc.  I have a folder which contains the .mov file I want to import, along with the .xmp fi [More]
Hi: We've recently moved from 5.x to 6.2, and users are complaining that autoreply/vacation messages are coming with the user's mailEquivalentAddress in the From: header rather than the email address that is in the Mail attribute. In our case, Mail i [More]