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 just bought a Y2P in Sweden and there is no Yoga Chef installed in the laptop and neither can I find it somewhere. Anyone noticed this also?Zoot wrote: No Swedish Chef program? That's borked. Z. Maybe some other holds the copyrights for Swedish Che [More]
After updating to the current version of reader the ability to save PDFs I am viewing has essentially gone away.  This is horribly and completely unacceptable. For a while clt shift s seemed to work; but even that no longer seems to work. How and I s [More]
I've tried searching, both here and in Safari, but can't find this specific gripe - the 'new and improved' formatting buttons don't seem to be working for me in Safari. For example, when I'm *trying* for bold text (with that word selected and clickin [More]
HI SAP GURUS please tell me what is the standard report for TDS IN SAP ?Hi if for employee PC00_M40_F16 PC00_M40_F24Q-Quater reports PC00_M40_F24 with regards parthaRead other 2 answers [More]
Dear Gurus, I am creating a Release order with reference to Contract. Ex. order qty is 100 in Contract, while doing sale order it should not allow to change the order qty more than 100 [not more than Ref. document qty]. I have tried in VTAA, completi [More]
Depuis que j'ai téléchargé la nouvelle mise à jour 6 pour l'iPhone 4, certaine de les applications bugg ! Lorsque je veux y aller sur l'une d'elles , ils me demande mon identifiant et mon mot de passe Apple , je le rentre , et l'application se ferme [More]
Hi, I moved a database (physical files) from one server to another. I need to modify the contents of the control files since the directory structure of the servers are not the same (and I can't change that). I know I can use ALTER DATABASE BACKUP CON [More]
Hi everybody, I need to copy data from one cube in productive system into the same cube in the test system. How can I do it? Regards ErwinIf you need to copy data from one system to another maybe you can do a Data Refresh or a SAP_DATA System Copy.   [More]
hello i bought my first mac and i want to become an iOS developer and creat app for iPhone iPad but now i have only iPad 4 and i want to buy iPhone 5s but I'm not sure to buy iphone5s now or wait for iPhone 6 because iphone6 may have bigger screen if [More]
Hi There, I have recently observed the slow in my MBP and found that they are 2 processes which are most of the time eating up processor. Which are Syslogd and MRT.  I really don't know about MRT process but i have searched the cause of syslogd. In m [More]