aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Creating nodes in jTree Netbeans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Creating nodes in jTree Netbeans" Watch "Creating nodes in jTree Netbeans" New topic

Creating nodes in jTree Netbeans

nadia ghezaiel
Ranch Hand

Joined: Apr 13, 2013
Posts: 40

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:

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);


* 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.
// <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();



jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
.addGap(29, 29, 29)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(62, 62, 62)
.addContainerGap(80, Short.MAX_VALUE))
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(106, 106, 106)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(14, Short.MAX_VALUE))

}// </editor-fold>

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

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
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
} 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);

/* 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:::

[Thumbnail for Tree.png]

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9993

I am having trouble understanding what you are asking here but, take a look at

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
I agree. Here's the link:
subject: Creating nodes in jTree Netbeans
Similar Threads
requestfocusinwindow going to lostfocus event thrice
java programming
update a row in JTable
problem clicking jbuttons in jtable
Dynamic textfield creation