[nbdev] Creating nodes in jTree Netbeans

  • From: "samouhanadia" < >
  • To:
  • Subject: [nbdev] Creating nodes in jTree Netbeans
  • Date: Fri, 03 May 2013 15:20:15 -0400

Hello, 

I find a bid pb after using JTree ((((

all the JTree tutorial deals are now creates the JTree in Java classes 
without using the JFrame, but I want to know how to add a bow for tying in a 
JTree created already by the pallet, not created by coding:

Exemple of tutoriel 
finded:http://docs.oracle.com/javase/tutorial/uiswing/components/tree.html

I try to do my only code: and this is the code:


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javatree;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

/**
*
* @author Nadia
*/
public class NewJFrame extends javax.swing.JFrame {

private static String ADD_COMMAND = "add";
private int newNodeSuffix = 1;
private JavaTree treePanel;
protected DefaultMutableTreeNode rootNode;
protected DefaultTreeModel treeModel;
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
treePanel = new JavaTree();

rootNode = new DefaultMutableTreeNode("Root Node");
treeModel = new DefaultTreeModel(rootNode);

initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane();
jTree1 = new javax.swing.JTree();
jButton1 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jScrollPane1.setViewportView(jTree1);

jButton1.setText("Ajouter");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new 
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(29, 29, 29)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 160, 
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(62, 62, 62)
.addComponent(jButton1)
.addContainerGap(80, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, 
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(106, 106, 106)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 70, 
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(14, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

jButton1.setActionCommand(ADD_COMMAND);
//jButton1.addActionListener(this);
jButton1.addActionListener((ActionListener) this);

// TODO add your handling code here:
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();

if (ADD_COMMAND.equals(command)) {
// Add button clicked
treePanel.addObject("New Node " + newNodeSuffix++);

}
}




public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,Object 
child, boolean shouldBeVisible) {
DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child);

if (parent == null) {
parent = rootNode;
}

// It is key to invoke this on the TreeModel, and NOT DefaultMutableTreeNode
treeModel.insertNodeInto(childNode, parent, parent.getChildCount());

// Make sure the user can see the lovely new node.
if (shouldBeVisible) {
jTree1.scrollPathToVisible(new TreePath(childNode.getPath()));
}
return childNode;
}








class MyTreeModelListener implements TreeModelListener {
public void treeNodesChanged(TreeModelEvent e) {
DefaultMutableTreeNode node;
node = (DefaultMutableTreeNode) (e.getTreePath().getLastPathComponent());

/*
* If the event lists children, then the changed node is the child of the
* node we've already gotten. Otherwise, the changed node and the
* specified node are the same.
*/

int index = e.getChildIndices()[0];
node = (DefaultMutableTreeNode) (node.getChildAt(index));

System.out.println("The user has finished editing the node.");
System.out.println("New value: " + node.getUserObject());
}

public void treeNodesInserted(TreeModelEvent e) {
}

public void treeNodesRemoved(TreeModelEvent e) {
}

public void treeStructureChanged(TreeModelEvent e) {
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code 
(optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the 
default look and feel.
* For details see 
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : 
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,
 null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,
 null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,
 null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,
 null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTree jTree1;
// End of variables declaration
}


Also,this is my Tree creeted after runnig,but this code can't do any 
thing,just displaying my Frame!!!

Please I need help:::




Attachments: 
http://forums.netbeans.org//files/tree_126.png



[nbdev] Creating nodes in jTree Netbeans

samouhanadia 05/03/2013

Project Features

About this Project

www was started in November 2009, is owned by jpirek, and has 21 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20140418.2d69abc). © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close