Two Laptop Bag*
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


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Creating nodes in jTree Netbeans" Watch "Creating nodes in jTree Netbeans" New topic
Author

Creating nodes in jTree Netbeans

nadia ghezaiel
Ranch Hand

Joined: Apr 13, 2013
Posts: 40
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:::



[Thumbnail for Tree.png]

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10425
    
    8

I am having trouble understanding what you are asking here but, take a look at http://docs.oracle.com/javase/tutorial/uiswing/components/tree.html


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating nodes in jTree Netbeans