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
I'd like to be able to use a remote shared drive. I know how to make it shared - but not how to then get another machine to share it! I'd like to do remote backups to my mac mini superdrive - I have it there and I can see the disc, but, when I try to [More]
Help! Is anyone else unable to install an older version of Flash on their less-then brand new computer(PowerMacG4)?? I'm running 10.5.8. I've downloaded the Uninstaller(which has a pink x in the upper-right corner) but two versions of Flash remain on [More]
Hi there, I was wanting to know if anyone has tried zipping a Word templage, then saving it into an oracle table using Blobs, and then retrieving it back and unzipping it. I would like to know if there are any issues doing this and also see some exam [More]
Hello! I was wondering if a magic HP angel would like to tell me what drivers I can download for these two Base system devices? I am working on an HP Probook 4730s running Windows 8.1 Professional. Please let me know if there are any other details I [More]
Dear SAP experts, my client is facing with one problem . one service PO is there in other than company code currency   that is in (EURO), Tthy have posted service entry sheet on jan 12th 2010 that exchange rate is 1 euro = 35.5 QAR (Qatra real) and t [More]
Shortly after starting Firefox the "music" reminiscent of Pac-Man fails to start. It is only on Firefox (haven't tried Thunderbird) even following an uninstall and re-install of Firefox. Just started today. Am using XP Home SP3. == This happened [More]
Hi all, I have this situation: 2 entities, country and state country has code and name as fields, code is PK state has code, name and country_code (linked to country code field) as fields, code is PK and country_code should too. @Entity @Table(name=" [More]
Hi,     Can anyone plz explain the link between the output type and the email id maintained in the address data in ME22n ? The output type is configured fo rsending Email .Whenever i maintain the email id in the address data the outptu type gets trig [More]
When booting, the kernel starts to initialize but right after "Probing EDD" it fails, rebooting instead.  If i use the kernel option "edd=off" the "Probing EDD" line doesn't show, but still the kernel reboots at that point.  [More]
I have a brand new Mac with the latest version of Pages, but I cannot record any scripts within the application. Any idea how I can do this? Mini   Mac OS X (10.4.7)  Pages is scriptable. However, it is not recordable, and that can be annoying enough [More]