File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes jTree with the TreeModel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply locked New topic
Author

jTree with the TreeModel

Guddu Dagar
Greenhorn

Joined: Feb 21, 2009
Posts: 3
Hello Sir,

Please help in implementing a JTree in the TreeModel. As i am not clear when to write in the methods of the TreeModel.

Please help me soon its very urgent.


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.Iterator;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.plaf.metal.MetalIconFactory;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;


public class OdiSAPERPMetadataBrowser implements TreeModel{

// The initial width and height of the frame
private static int sWIDTH = 700;
private static int sHEIGHT = 630;
// private static int sWIDTH = 700;
// private static int sHEIGHT = 630;

public DefaultMutableTreeNode jTreeRootNode, jTreeSelectedNode; //rootNode & selected node

private JPanel jPanelTop = new JPanel();
private JPanel jPanelSearch = new JPanel();
private JPanel jPanelTree = new JPanel();
private JPanel jBottomPanel = new JPanel();
private JPanel jPanelGap1 = new JPanel();
private JPanel jPanelGap2 = new JPanel();
private JLabel jLabelgap = new JLabel(" ");

private JLabel jLabelTName = new JLabel("Table Name: ");
private JLabel jLabelSDesc = new JLabel("Short Description: ");
private JLabel jLabelAppComp = new JLabel("Application Component: ");
private JLabel jLabelPkg = new JLabel("Package: ");
private JTextField jTxtFieldTName = new JTextField("",15);
private JTextField jTxtFieldSDesc = new JTextField("",15);
private JTextField jTxtFieldAppComp = new JTextField("",15);
private JTextField jTxtFieldPkg = new JTextField("",15);

private JButton jButtonSearch = new JButton("Search");

private JButton jButtonCancle = new JButton("Cancel");
private JButton jButtonReverse = new JButton("Reverse MetaDeta");

private static final int BORDER = 12; // Window border in pixels.
private Hashtable mModuleName = null; //Hashtable of Module Name & Description
private ArrayList mAppCompList = null; //List of Application Component & Tables
// mSelectedTables - map of keys as selected tables name & values as their list of fields name
private Hashtable mSelectedTables = new Hashtable();

private String moduleKey = null;
private String moduleDesc = null;

public boolean isTableNodeSelected = false;
public ArrayList mDeleselectedNodeList = new ArrayList();

public JFrame frame = null;


private JTree tree =null;
private String ivTable = null;
private String ivDesc = null;
private String ivPackg = null;
private String ivAPComp = null;

/**


public DefaultMutableTreeNode jTreeRootNode, jTreeSelectedNode; //rootNode & selected node


private JPanel jPanelTree = new JPanel();

private static final int BORDER = 12; // Window border in pixels.
private Hashtable mModuleName = null; //Hashtable of Module Name & Description
private ArrayList mAppCompList = null; //List of Application Component & Tables

//mSelectedTables - map of keys as selected tables name & values as their list of fields name
private Hashtable mSelectedTables = new Hashtable();

private String moduleKey = null;
private String moduleDesc = null;

public boolean isTableNodeSelected = false;
public ArrayList mDeleselectedNodeList = new ArrayList();

public JFrame frame = null;
private JTree tree =null;
private String ivTable = null;
private String ivDesc = null;
private String ivPackg = null;
private String ivAPComp = null;

/**
* Default Constructor
* Comments
*/
//public OdiSAPERPMetadataBrowser(OdiSAPERPAdapter odiSapERPAdapterObj, OdiSAPLogger odiSAPLogger, OdiSAPERPMetadataHelper odiSAPERPMetadataHelper) {
public OdiSAPERPMetadataBrowser() { ////For eclipse version

frame = new JFrame();
frame.setTitle("SAP Metadata Browser");

createSearchPanel();
createTreePanel(); //Prepare tree panel

Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screenSize = tk.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
frame.setSize(sWIDTH, sHEIGHT);
frame.setLocation(screenWidth / 4, screenHeight / 13);
frame.setVisible(true);
// sapLogger.log(sapLogger.INFO, "------- OdiSAPERPMetadataBrowser || OdiSAPERPMetadataBrowser() constructor ---------");
}////End of Default Constructor

private void createSearchPanel() {
// sapLogger.log(sapLogger.INFO, "++++++++++ OdiSAPERPMetadataBrowser || createSearchPanel() +++++++++++++");
jPanelSearch.setLayout(new GridBagLayout());
jPanelSearch.setBorder(BorderFactory.createTitledBorder("Search") );

GridBagConstraints gBC = new GridBagConstraints();
gBC.fill = GridBagConstraints.HORIZONTAL;

// ****** First row of search panel ********
//Adding first label
// gBC.weightx = 0;
gBC.gridx = 0;
gBC.gridy = 0;
jPanelSearch.add(jLabelTName, gBC);

//Adding first text box
gBC.gridx = 1;
gBC.gridy = 0;
gBC.weightx = 1.0;;
jPanelSearch.add(jTxtFieldTName, gBC);

//Adding gap between two cell in the first row
gBC.gridx = 2;
gBC.gridy = 0;
jPanelSearch.add(jLabelgap, gBC);

//Adding second label
//gBC.weightx = 1.0;
gBC.gridx = 3;
gBC.gridy = 0;
jPanelSearch.add(jLabelSDesc, gBC);

//Adding second text box
gBC.gridx = 4;
gBC.gridy = 0;
gBC.weightx = 1.0;
jPanelSearch.add(jTxtFieldSDesc, gBC);

// Adding Gap between 1st & 2nd row
gBC.gridx = 0;
gBC.gridy = 1;
jPanelSearch.add(jPanelGap1,gBC);

// ************* Second row of search panel *************
//Adding third label
gBC.gridx = 0;
gBC.gridy = 2;
jPanelSearch.add(jLabelAppComp, gBC);

//Adding third text box
gBC.gridx = 1;
gBC.gridy = 2;
gBC.weightx = 1.0;
jPanelSearch.add(jTxtFieldAppComp, gBC);

//// Adding Gap between 2nd & 3rd row
gBC.gridx = 2;
gBC.gridy = 2;
jPanelSearch.add(jLabelgap, gBC);

//Adding fourth label
gBC.gridx = 3;
gBC.gridy = 2;
gBC.weightx = 3;
jPanelSearch.add(jLabelPkg, gBC);

//Adding fourth text box
gBC.gridx = 4;
gBC.gridy = 2;
gBC.weightx = 1.0;
jPanelSearch.add(jTxtFieldPkg, gBC);

// ************ Adding Gap between 2nd & 3rd row *****************
gBC.gridx = 0;
gBC.gridy = 3;
jPanelSearch.add(jPanelGap2,gBC);

//Adding Search button in third row
gBC.gridx = 2;
gBC.gridy = 4;
jPanelSearch.add(jButtonSearch, gBC);


//Adding Search Panel into Top Panel
jPanelTop.add(jPanelSearch);
jPanelTop.setBorder(BorderFactory.createLoweredBevelBorder());
frame.getContentPane().add(jPanelTop, BorderLayout.NORTH);
}

public Hashtable getALLSAPModules() {
// sapLogger.logp(sapLogger.INFO, "OdiSAPERPAdapter", "getALLSAPModules","Initializing all the SAP modules");
Hashtable sapModList = new Hashtable();
sapModList.put("CA", "Cross-Application Components");
sapModList.put("AC", "Accounting - General");
sapModList.put("FI", "Financial Accounting");
sapModList.put("TR", "Treasury");
sapModList.put("CO", "Controlling");
sapModList.put("IM", "Investment Management");
sapModList.put("EC", "Enterprise Controlling");
sapModList.put("RE", "Real Estate Management");
sapModList.put("LO", "Logistics - General");
sapModList.put("SD", "Sales and Distribution");
sapModList.put("MM", "Materials Management");
sapModList.put("LE", "Logistics Execution");
sapModList.put("QM", "Quality Management");
sapModList.put("PM", "Plant Maintenance");
sapModList.put("CS", "Customer Service");
sapModList.put("PP", "Production Planning and Control");
sapModList.put("PS", "Project System");
sapModList.put("EHS", "Environment, Health and Safety");
sapModList.put("PA", "Personnel Management");
sapModList.put("PT", "Personnel Time Management");
sapModList.put("PY", "Payroll");
sapModList.put("PE", "Training and Event Management");
sapModList.put("BC", "Basis Components");
sapModList.put("SV", "Service");

//sapLogger.logp(sapLogger.INFO, "OdiSAPERPAdapter", "getALLSAPModules","List of available SAP modules", sapModList.toString());

return sapModList;

}//End of getALLSAPModules method

/**
* This method is responsible to create tree node of fetched table metadata from SAP System **
* @param
* @return void
*/
private void createTreePanel() {
//sapLogger.log(sapLogger.INFO, "++++++++++ OdiSAPERPMetadataBrowser || createTreePanel() +++++++++++++");
// Root node and main modules

jTreeRootNode = new DefaultMutableTreeNode("SAP Metadata");
mModuleName = getALLSAPModules(); //getting SAP main module
//enumerate all the contents of the hashtable
Enumeration keys = mModuleName.keys();

while ( keys.hasMoreElements()) {
moduleKey = (String)keys.nextElement();
moduleDesc = (String)mModuleName.get( moduleKey );
DefaultMutableTreeNode moduleNode = new DefaultMutableTreeNode(moduleKey + ", " + moduleDesc);
jTreeRootNode.add(moduleNode); //Adding main module into tree
} // end while
tree = new JTree(jTreeRootNode); //making tree
tree.putClientProperty("JTree.lineStyle", "Angled");

// Setting folder icon for empty node
Icon leafIcon = MetalIconFactory.getTreeLeafIcon();
Icon openIcon = MetalIconFactory.getTreeFolderIcon();

DefaultTreeCellRenderer dtcr = new DefaultTreeCellRenderer();
dtcr.setLeafIcon(openIcon);
dtcr.setOpenIcon(openIcon);
tree.setCellRenderer(dtcr);
// end of setting folder icon


//Add mouse listener to add sub nodes (App. component, tables & fields) in the tree on double click


//Add tree structure into the scrollpane & after that in the container
JScrollPane srollpane = new JScrollPane(tree);
jPanelTree.setLayout(new BoxLayout(jPanelTree, BoxLayout.LINE_AXIS));
jPanelTree.add(srollpane);
jPanelTree.setBorder(BorderFactory.createTitledBorder(""));
frame.getContentPane().add(jPanelTree, BorderLayout.CENTER); //Adding tree in the center
// sapLogger.log(sapLogger.INFO, "-------- OdiSAPERPMetadataBrowser || createTreePanel() ------------");


}//End of createTreePanel() method

/**
* This method is use to add bottom panel and handle the button listener to cancel or reverse metadata
*
*/


/** static void main() method */
public static void main(String args[]) {

OdiSAPERPMetadataBrowser frame = new OdiSAPERPMetadataBrowser();

} //End of static void main() method

public void addTreeModelListener(TreeModelListener arg0) {
// TODO Auto-generated method stub

}

public Object getChild(Object arg0, int arg1) {
// TODO Auto-generated method stub
return null;
}

public int getChildCount(Object arg0) {
// TODO Auto-generated method stub
return 0;
}

public int getIndexOfChild(Object arg0, Object arg1) {
// TODO Auto-generated method stub
return 0;
}

public Object getRoot() {
// TODO Auto-generated method stub
return null;
}

public boolean isLeaf(Object arg0) {
// TODO Auto-generated method stub
return false;
}

public void removeTreeModelListener(TreeModelListener arg0) {
// TODO Auto-generated method stub

}

public void valueForPathChanged(TreePath arg0, Object arg1) {
// TODO Auto-generated method stub

}
}






Thanks & Regards
Arun
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40


This question has nothing to do with threads -- please post in the correct forum, as it will increase the chances of people who are likely to understand your question, in seeing your question.


Also, in order to help improve the chances of getting an answer, please read this...

http://faq.javaranch.com/java/IsolateTheProblem

And these...

http://faq.javaranch.com/java/ShowSomeEffort
http://faq.javaranch.com/java/NotACodeMill
http://faq.javaranch.com/java/DoYourOwnHomework

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jTree with the TreeModel