Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Modification of Jtree at Runtime ...Forurth Posting on this Prb..Help me  RSS feed

 
ritesh r
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ALL:

I am developing a swing application.The initial JTree is populated using the Sql Query(JDBC)
My application have a JPopupItem which has a item namely"Add".
The ADD opens a Component Display Class in the SplitPane(ScrollPane is enabled).The ADD Class has a Saving functionality (The Procees will go to the SQL Server)which will help me add a new node to the JTree.
Can I do this...A Population of Jtree when I add a new node at Runtime and execute the Query again to populate the Jtree.
Help me..This is urgent
 
Renee Zhang
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can do it. I have several different nodes which refer to different classes. I create a node and insert it into the treemodel. It will display the node without refresh or repaint. Please take a look at the example at http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html.
Good luck!
 
ritesh r
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi :
But my problem has to be solve using JDBC.I have to call my Sql query with the fresh value inserted into the Database when I add a value.Can I recall my Swing Constructor again with a boolean flag which would help me identify the refresh of the fresh values inserted by the user.
Help me with how to call the Constructor(The Jtree is built using the Sql Quert initially).
Let me hear from you.
Thanking you.
Sincere Regards
Ritesh
Originally posted by Renee Zhang:
I think you can do it. I have several different nodes which refer to different classes. I create a node and insert it into the treemodel. It will display the node without refresh or repaint. Please take a look at the example at http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html.
Good luck!

 
Renee Zhang
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After you run the DynamicTreeDemo.java and DynamicTree.java that I sent to you yesterday via email. Now let's override DefaultTreeNode.
abstract class customTreeNode extends DefaultMutableTreeNode {
//Add atributes here
protected ResultSet rs;
protected Connection conn;
protected String nodeName;
......
protected void queryDBandAddNode () {};
}
//create as many customTreeNode as you want
class customTreeNodeOne extends customTreeNode {
private customTreeNodeTwo ctnt;
customTreeNodeOne (String NodeName) {
name = NodeName;
}
//override queryDBandAddNode
void queryDBandAddNode () {
try {
//Open database, get resultset
//get the String you want to create child node
......
while (rs.next()) {
String nodeName = rs.getString("ANYNAME");
ctnt = new customTreeNodeTwo(nodeName);
add(ctnt);
}//close conn and catch Exception;
}
class customTreeNodeTwo extends customTreeNode {
customTreeNodeTwo (String NodeName) {
name = NodeName;
}
// This node doesn't have child nodes, simplely donothing.
void queryDBandAddNode () {
//Donothing
}
}
/* Now you have 2 custom treenodes and let's add treeselectionListener to your tree to the DynamicTreeDemo.java file. when user clicks on the customeTreeNodeOne, it will get the data from the database and add to the tree. */
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
// get the last selected treenode
customeTreeNode node =
(customeTreeNode)tree.getLastSelectedPathComponent();
if (node.isLeaf()) {
node.queryDBandAddNode();
}
}

Good luck!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!