JTree view of my table date

Hi

I have 2 tables in my Data Base:-

1)Section Table

section_id Description

0Electrical

1Mechanical

2)Parts Table

Parts_IdSection_IdPart_Desc

00 Wire

11 Gearbox

I want to show a JTree

Root

!Electrical

!!

!!Wire

!

! Mechanical

!

!__Gearbox

a)Nowif I right click a"section" node I want list of popupmenu items which allow me to select edit,delete ornew . description of section

b)Nowif I right click a"leaf" node I want list of popupmenu items which allow me to select edit,delete ornew part.

I know how to handle JDialog, but I want my Jtree to be refreshed once I update and close.

I have read most of the jtree, treemodelinterface , but I am not clear as how to wire all ofthis area up.

maybe just a starting point how to wire would be greatly aprecaiated with some code examples

thanks in advance

Gurmej

[1229 byte] By [gurmej40a] at [2007-11-26 16:07:23]
# 1
you need to create a function to translate a tablemodel into a treemodel and another one to do the opposite.It's not that hard. Just dig in with the javadoc api and you'll be fine
tjacobs01a at 2007-7-8 22:29:39 > top of Java-index,Desktop,Core GUI APIs...
# 2

I am not quite getting the picture,

so in your eyes,

step:-

so I have POJOS for Sections and Parts

1. I would extend AbstratcTableModel for

a) SectionTableModel(which are going to be my nodes)

b) PartsTableModel(which would be my leafs)

2. Have a function "ConvertSectionToNodes" and "ConvertPartToLeaf"

so what would these fuctions be extending?

thanks again

Gurmej

gurmej40a at 2007-7-8 22:29:39 > top of Java-index,Desktop,Core GUI APIs...
# 3
Read the JTree turorial: http://java.sun.com/docs/books/tutorial/uiswing/components/tree.htmlAnd build the tree hierarchy using the table data.
Rodney_McKaya at 2007-7-8 22:29:39 > top of Java-index,Desktop,Core GUI APIs...
# 4
Believe me when I say I have been there and have tried hard and re-read a million times but I can't seem to understand how I can apply it to my situation. I can create literals and put them into a JTree and put up a JTree.Maybe all I need is an overview of the classes/design I
gurmej40a at 2007-7-8 22:29:39 > top of Java-index,Desktop,Core GUI APIs...
# 5
onyone shed some more light on this one
gurmej40a at 2007-7-8 22:29:39 > top of Java-index,Desktop,Core GUI APIs...
# 6

ok guys I have managed to achieve this. here is my code:-

my problem is if I use a popup to launch a JDIALOG(SectionMaintEdit class) to update/change the description of my section, upon pressing the "update" button , I want my tree to show the updated value. Is it wise then to have a refresh button on my PartJTreePanel or some how listen to "reload" action from the JDIALOG where I have been updating my section description. ?

public class PartsJTreePanel extends JPanel implements WorkbenchPanel, ActionListener, MouseListener, TreeSelectionListener {

JScrollPane scrollpane;

JTree partsTree;

JPopupMenu popUpMenu;

int pathCount;

public PartsJTreePanel() {

super();

setLayout(new BorderLayout());

popUpMenu= new JPopupMenu();

popUpMenu.add(createMitem("New"));

popUpMenu.add(createMitem("Edit"));

popUpMenu.add(createMitem("Delete"));

DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");

BuildTreeBusiness b = new BuildTreeBusiness(root);

partsTree = new JTree(root);

partsTree.addMouseListener(this);

partsTree.addTreeSelectionListener(this);

scrollpane= new JScrollPane(partsTree);

add(scrollpane, BorderLayout.CENTER);

setName("Parts");

}

private JMenuItem createMitem(String o) {

JMenuItem item=new JMenuItem(o);

item.addActionListener(this);

return item;

}

public JMenuBar getMenubar() {

// TODO Auto-generated method stub

return null;

}

public void actionPerformed(ActionEvent e) {

System.out.println(e.getActionCommand());

DefaultMutableTreeNode node = (DefaultMutableTreeNode) partsTree

.getLastSelectedPathComponent();

if (node == null)

return;

Object nodeInfo = node.getUserObject();

if (e.getActionCommand().equals("New")) {

if (pathCount > 2) {

System.out.println("Create New Part for section:" + nodeInfo );

} else {

new SectionMaintPanelNew();

}

}

if (e.getActionCommand().equals("Edit")){

if (nodeInfo.equals("root")) return;

if (pathCount > 2) {

System.out.println("Edit Part for section:" + nodeInfo );

} else {

Section sec= new Section();

sec.setSectionDescription(nodeInfo.toString());

new SectionMaintPanelEdit(sec);

}

}

if (e.getActionCommand().equals("Delete")){

if (pathCount > 2) {

System.out.println("Delete Part :" + nodeInfo );

} else {

Section sec= new Section();

sec.setSectionDescription(nodeInfo.toString());

new SectionMaintPanelDelete(sec);

}

}

}

public void mouseClicked(MouseEvent e) {

// TODO Auto-generated method stub

}

public void mouseEntered(MouseEvent e) {

// TODO Auto-generated method stub

}

public void mouseExited(MouseEvent e) {

// TODO Auto-generated method stub

}

public void mousePressed(MouseEvent e) {

// TODO Auto-generated method stub

}

public void mouseReleased(MouseEvent e) {

if (SwingUtilities.isRightMouseButton(e)){

popUpMenu.show(e.getComponent(),

e.getX(), e.getY());

}

}

public void valueChanged(TreeSelectionEvent e) {

TreePath treepath= e.getPath();

System.out.println("path:"+treepath);

pathCount=treepath.getPathCount();

System.out.println("pathcnt:"+pathCount);

}

/**

* @param args

*/

public static void main(String[] args) {

JFrame frame= new JFrame("MaintainParts");

frame.getContentPane().add(new PartsJTreePanel());

frame.setSize(30, 100);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

}

Now here is my code to build load tree from the database

public class BuildTreeBusiness {

public BuildTreeBusiness(DefaultMutableTreeNode top) {

Statement st=null;

ResultSet rs=null;

Statement st2=null;

ResultSet rs2=null;

try {

st = MerciaDB.getConnection().createStatement();

rs = st.executeQuery("Select * from section");// run the query

// the result set is a cursor into the data. You can only

// point to one row at a time

// assume we are pointing to BEFORE the first row

// rs.next() points to next row and returns true

// or false if there is no next row, which breaks the loop

for (; rs.next(); ) {

Section sec=new Section((Integer) rs.getObject(1),rs.getObject(2).toString());

DefaultMutableTreeNode node= new DefaultMutableTreeNode(sec.getSectionDescription());

st2 = MerciaDB.getConnection().createStatement();

rs2 = st2.executeQuery("Select * from part where sectionId='"+ sec.getSectionId() + "'");// run the query

for (; rs2.next(); ) {

DefaultMutableTreeNode child= new DefaultMutableTreeNode(rs2.getObject(3).toString());

node.add(child);

}

st2.close();

top.add(node);

}

st.close();// NOTE!! if you close a statement the associated ResultSet is

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return ;

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

}

}

thanks in advance

gurmej40a at 2007-7-8 22:29:39 > top of Java-index,Desktop,Core GUI APIs...
# 7
anybody fancy making a contribution
gurmej40a at 2007-7-8 22:29:39 > top of Java-index,Desktop,Core GUI APIs...
# 8
nice coding and very clear mr gurmej ^__^.thanks for the info..its very clear...(im not yet tested the code...but just by looking on it..i clearly understand the point)^__^.NICE bro...
supercoinsa at 2007-7-8 22:29:39 > top of Java-index,Desktop,Core GUI APIs...