I'm working with Swing.I have to construct a SplitPane with JTree on the leftside and its details which is stored in the database on the right side of the pane.
I have stored the Node Details,Slot details,line details in database(separate table) like Node_name,id..
Now, my problem is, If I click the Node "Node1", it has to display the database details for that corresponding Node in that right side of the pane. Similarly for other nodes also..
How to write code for this? How to connect JTree with database.(MySql).
Give 1 suggestion for this plz...
Thanks
August 3, 2010 at 6:01 PM
Hi Friend,
Try the following code:
import javax.swing.*;
import javax.swing.tree.*;
import java.sql.*;
import java.awt.*;
import java.util.*;
import javax.swing.event.*;
public class JTreeDatabase {
public static void main(String[] args){
JFrame frame = new Database();
frame.show();
}
}
class Database extends JFrame implements TreeSelectionListener{
JTextArea description;
public Database(){
setSize(400, 300);
DefaultMutableTreeNode parent = new DefaultMutableTreeNode("Students", true);
DefaultMutableTreeNode n1 = new DefaultMutableTreeNode("1");
DefaultMutableTreeNode n2 = new DefaultMutableTreeNode("2");
DefaultMutableTreeNode n3 = new DefaultMutableTreeNode("3");
DefaultMutableTreeNode n4 = new DefaultMutableTreeNode("4");
DefaultMutableTreeNode n5 = new DefaultMutableTreeNode("5");
DefaultMutableTreeNode n6 = new DefaultMutableTreeNode("6");
parent.add(n1);
parent.add(n2);
parent.add(n3);
parent.add(n4);
parent.add(n5);
parent.add(n6);
JTree tree = new JTree(parent);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.addTreeSelectionListener(this);
description = new JTextArea();
JSplitPane innerPane= new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, tree, description);
getContentPane().add(innerPane, "Center");
}
public void valueChanged(TreeSelectionEvent se) {
JTree tree = (JTree) se.getSource();
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
String selectedNodeName = selectedNode.toString();
try{
Class.forName( "com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:
mysql://localhost:3306/test";, "root", "root");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("Select * from student where rollNo='"+selectedNodeName+"'");
String data="";
while (rs.next()) {
data=rs.getString("name")+" "+rs.getString("address")+" "+rs.getString("age");
}
description.setText(data);
}
catch(Exception e){}
}
}
Thanks
Related Tutorials/Questions & Answers:
Tree View with database - Java BeginnersTree View with database
Hi,
I'm working with Swing.I have... in the
database on the right side of the pane.
For ex:
Network
|
|Node1...
I have stored the Node Details,Slot details,line details in
database tree viewtree view display a
tree view in flex?nodes will be in the form
Advertisements
tree view - Java Beginnerstree view
I m using swing for the treeview in my application and also the
database(Hsqldb).
I want to connect the
tree view with the
database fields.
If u want more clarification ,let me know.
Thanks
tree view - Java Beginnerstree view Hello Deepak,
Thanks for your help last time regarding java.
I have another problem, Actually I want a
tree view in my application
which will display the files
tree view - Java Beginnerstree view I want to use
tree view in my example code to display data in hierarchical form. how ??
Thanks
Hi friend,
Please visit the following links:
http://www.roseindia.net/java/example/java/swing
Populate Menus In Tree View - Struts" as prefix.In the same way i am thinking to bring the menus in a
tree view in my application "is it possible to bring menu that is stored in a
database in a
tree...Populate Menus In
Tree View Hi all,
i am writing one simple
JSP tree view - JSP-ServletJSP
tree view Hi...
In my project i need to display the data from
database in the form of
tree structure...
i.e. like horizontal binary
tree
I've to dynamically get the values...
Can you plssssss suggest me how
Reg: Tree view in Struts using ajax - StrutsReg:
Tree view in Struts using ajax HI all,
Can you figure out the
tree and treenode attribute belong to which .jar file or tld ?
Example... the example on it.
Example for
Tree view in Struts to visit this link..
http
viewview what is the use
view in
database view data from database using drop down listview data from
database using drop down list hi i want to
view the data from
database by selecting a value in a drop down list. for an example drop down list have picture element.when click it select pictures from the
database Creating Database using B+Tree in Java - Java BeginnersCreating
Database using B+
Tree in Java I'm doing a project in which I have to create an Object Oriented
Database using B+
Tree in Java..It is not a
Database connectivity..I have to create a seperate
Database(like Oeacle)my-self
TreeTree print("code sample");1) Write Java code to create the following
tree using new
Tree state-
ments:
1
viewview hi
iam writing some struts application by using dyanafalidator form in place of actionform bean classes i can enter data by using some jsp file and it will insert to
database by using business logic.
now my requirement
DatabaseDatabase is it possible to
view the query code after executing it.if yes tell me the procedure.
Yes, if you want to retrieve the data that is stored in
database, then use the given query:
select * from student
DatabaseDatabase hi i don't know any thing about
database please help me and explain it for me . thank you
you can get more data base programs from here
View Programs here
What is View? one or more existing
database tables or other views. In effect every
view...What is
View? What is
View?
Hi,
A SQL
View is a virtual table, which is based on SQL SELECT query. Essentially a
view is very close
DatabaseDatabase How to represent data from
database in a
tree?
Please visit the following link:
Retrieve data from
database in
tree using Java Swing
databasedatabase the code for initializing the
database connection
Nitobi Tree coding and
effort. Nitobi
Tree adds just like folder
view in Windows...Nitobi
Tree
Nitobi
Tree
Nitobi
Tree, the Ajax component of User Interface Suite is used