{"id":10245,"date":"2015-08-25T00:25:36","date_gmt":"2015-08-25T04:25:36","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/?p=10245"},"modified":"2015-08-24T08:44:31","modified_gmt":"2015-08-24T12:44:31","slug":"dom-example-that-represents-the-basic-structure-of-an-xml-document-as-a-jtree","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=10245","title":{"rendered":"DOM example that represents the basic structure of an XML document as a JTree"},"content":{"rendered":"<pre>\/\/XMLTree.java\r\n\/\/Uses the following files\r\n\r\nUses the following files:\r\n\r\n\u00a0\u00a0\u00a0 * XMLFrame.java:Swing application to select an XML document and display in a JTree.\r\n\r\nExtensionFileFilter.java Allows you to specify which file extensions will be displayed in a JFileChooser.\r\n\r\ntest.xml Default file loaded if none selected by user. \r\n\r\nperennials.xml and perennials.dtd Data on daylilies and corresponding DTD.\r\n\r\nWindowUtilities.java \u00a0\r\n\r\nExitListener.java. \r\n\r\n\/\/XMLTree.java as follows\r\nimport java.awt.*;\r\nimport javax.swing.*;\r\nimport javax.swing.tree.*;\r\nimport java.io.*;\r\nimport org.w3c.dom.*;\r\nimport javax.xml.parsers.*;\r\n\r\n\/** Given a filename or a name and an input stream,\r\n\u00a0*\u00a0 this class generates a JTree representing the\r\n\u00a0*\u00a0 XML structure contained in the file or stream.\r\n\u00a0*\u00a0 Parses with DOM then copies the tree structure\r\n\u00a0*\u00a0 (minus text and comment nodes).\r\n\u00a0*\r\n\u00a0*\u00a0 Taken from Core Web Programming from \r\n\u00a0*\u00a0 Prentice Hall and Sun Microsystems Press,\r\n\u00a0* \u00a0\r\n\u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown;\r\n\u00a0*\u00a0 may be freely used or adapted. \r\n\u00a0*\/\r\n\r\npublic class XMLTree extends JTree {\r\n\u00a0 public XMLTree(String filename) throws IOException {\r\n\u00a0\u00a0\u00a0 this(filename, new FileInputStream(new File(filename)));\r\n\u00a0 }\r\n\r\n\u00a0 public XMLTree(String filename, InputStream in) {\r\n\u00a0\u00a0\u00a0 super(makeRootNode(in));\r\n\u00a0 }\r\n\r\n\u00a0 \/\/ This method needs to be static so that it can be called\r\n\u00a0 \/\/ from the call to the parent constructor (super), which\r\n\u00a0 \/\/ occurs before the object is really built.\r\n\u00a0 \r\n\u00a0 private static DefaultMutableTreeNode\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 makeRootNode(InputStream in) {\r\n\u00a0\u00a0\u00a0 try {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Use JAXP's DocumentBuilderFactory so that there\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ is no code here that is dependent on a particular\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ DOM parser. Use the system property\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ javax.xml.parsers.DocumentBuilderFactory (set either\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ from Java code or by using the -D option to \"java\").\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ or jre_dir\/lib\/jaxp.properties to specify this.\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 DocumentBuilderFactory builderFactory =\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DocumentBuilderFactory.newInstance();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 DocumentBuilder builder =\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 builderFactory.newDocumentBuilder();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Standard DOM code from hereon. The \"parse\"\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ method invokes the parser and returns a fully parsed\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Document object. We'll then recursively descend the\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ tree and copy non-text nodes into JTree nodes.\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 Document document = builder.parse(in);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 document.getDocumentElement().normalize();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 Element rootElement = document.getDocumentElement();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 DefaultMutableTreeNode rootTreeNode =\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 buildTree(rootElement);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 return(rootTreeNode);\r\n\u00a0\u00a0\u00a0 } catch(Exception e) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 String errorMessage =\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"Error making root node: \" + e;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 System.err.println(errorMessage);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 e.printStackTrace();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 return(new DefaultMutableTreeNode(errorMessage));\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0 }\r\n\r\n\u00a0 private static DefaultMutableTreeNode\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 buildTree(Element rootElement) {\r\n\u00a0\u00a0\u00a0 \/\/ Make a JTree node for the root, then make JTree\r\n\u00a0\u00a0\u00a0 \/\/ nodes for each child and add them to the root node.\r\n\u00a0\u00a0\u00a0 \/\/ The addChildren method is recursive.\r\n\u00a0\u00a0\u00a0 DefaultMutableTreeNode rootTreeNode =\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 new DefaultMutableTreeNode(treeNodeLabel(rootElement));\r\n\u00a0\u00a0\u00a0 addChildren(rootTreeNode, rootElement);\r\n\u00a0\u00a0\u00a0 return(rootTreeNode);\r\n\u00a0 }\r\n\r\n\u00a0 private static void addChildren\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (DefaultMutableTreeNode parentTreeNode,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Node parentXMLElement) {\r\n\u00a0\u00a0\u00a0 \/\/ Recursive method that finds all the child elements\r\n\u00a0\u00a0\u00a0 \/\/ and adds them to the parent node. We have two types\r\n\u00a0\u00a0\u00a0 \/\/ of nodes here: the ones corresponding to the actual\r\n\u00a0\u00a0\u00a0 \/\/ XML structure and the entries of the graphical JTree.\r\n\u00a0\u00a0\u00a0 \/\/ The convention is that nodes corresponding to the\r\n\u00a0\u00a0\u00a0 \/\/ graphical JTree will have the word \"tree\" in the\r\n\u00a0\u00a0\u00a0 \/\/ variable name. Thus, \"childElement\" is the child XML\r\n\u00a0\u00a0\u00a0 \/\/ element whereas \"childTreeNode\" is the JTree element.\r\n\u00a0\u00a0\u00a0 \/\/ This method just copies the non-text and non-comment\r\n\u00a0\u00a0\u00a0 \/\/ nodes from the XML structure to the JTree structure.\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0\u00a0 NodeList childElements =\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 parentXMLElement.getChildNodes();\r\n\u00a0\u00a0\u00a0 for(int i=0; i\r\n\u00a0 \/\/ JTree Node:\u00a0 blah\r\n\u00a0 \/\/ XML Element: \r\n\u00a0 \/\/ JTree Node:\u00a0 blah (foo=bar, baz=quux)\r\n\r\n\u00a0 private static String treeNodeLabel(Node childElement) {\r\n\u00a0\u00a0\u00a0 NamedNodeMap elementAttributes =\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 childElement.getAttributes();\r\n\u00a0\u00a0\u00a0 String treeNodeLabel = childElement.getNodeName();\r\n\u00a0\u00a0\u00a0 if (elementAttributes != null &amp;&amp;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 elementAttributes.getLength() &gt; 0) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 treeNodeLabel = treeNodeLabel + \" (\";\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 int numAttributes = elementAttributes.getLength();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 for(int i=0; i 0) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 treeNodeLabel = treeNodeLabel + \", \";\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 treeNodeLabel =\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 treeNodeLabel + attribute.getNodeName() +\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"=\" + attribute.getNodeValue();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 treeNodeLabel = treeNodeLabel + \")\";\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 return(treeNodeLabel);\r\n\u00a0 }\r\n}\r\n\r\nXMLFrame.java Swing application to select an XML document and display in a JTree.\r\n\r\nimport java.awt.*;\r\nimport javax.swing.*;\r\nimport java.io.*;\r\n\r\n\/** Invokes an XML parser on an XML document and displays\r\n\u00a0*\u00a0 the document in a JTree. Both the parser and the\r\n\u00a0*\u00a0 document can be specified by the user. The parser\r\n\u00a0*\u00a0 is specified by invoking the program with\r\n\u00a0*\u00a0 java -Djavax.xml.parsers.DocumentBuilderFactory=xxx XMLFrame\r\n\u00a0*\u00a0 If no parser is specified, the Apache Xerces parser is used.\r\n\u00a0*\u00a0 The XML document can be supplied on the command\r\n\u00a0*\u00a0 line, but if it is not given, a JFileChooser is used\r\n\u00a0*\u00a0 to interactively select the file of interest.\r\n\u00a0*\r\n\u00a0*\u00a0 Taken from Core Web Programming from \r\n\u00a0*\u00a0 Prentice Hall and Sun Microsystems Press,\r\n\u00a0\r\n\u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown;\r\n\u00a0*\u00a0 may be freely used or adapted. \r\n\u00a0*\/\r\n\r\npublic class XMLFrame extends JFrame {\r\n\u00a0 public static void main(String[] args) {\r\n\u00a0\u00a0\u00a0 String jaxpPropertyName =\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \"javax.xml.parsers.DocumentBuilderFactory\";\r\n\u00a0\u00a0\u00a0 \/\/ Pass the parser factory in on the command line with\r\n\u00a0\u00a0\u00a0 \/\/ -D to override the use of the Apache parser.\r\n\u00a0\u00a0\u00a0 if (System.getProperty(jaxpPropertyName) == null) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 String apacheXercesPropertyValue =\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"org.apache.xerces.jaxp.DocumentBuilderFactoryImpl\";\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 System.setProperty(jaxpPropertyName,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 apacheXercesPropertyValue);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 String filename;\r\n\u00a0\u00a0\u00a0 if (args.length &gt; 0) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 filename = args[0];\r\n\u00a0\u00a0\u00a0 } else {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 String[] extensions = { \"xml\", \"tld\" };\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 WindowUtilities.setNativeLookAndFeel();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 filename = ExtensionFileFilter.getFileName(\".\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"XML Files\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 extensions);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 if (filename == null) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 filename = \"test.xml\";\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 new XMLFrame(filename);\r\n\u00a0 }\r\n\r\n\u00a0 public XMLFrame(String filename) {\r\n\u00a0\u00a0\u00a0 try {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 WindowUtilities.setNativeLookAndFeel();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 JTree tree = new XMLTree(filename);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 JFrame frame = new JFrame(filename);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 frame.addWindowListener(new ExitListener());\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 Container content = frame.getContentPane();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 content.add(new JScrollPane(tree));\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 frame.pack();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 frame.setVisible(true);\r\n\u00a0\u00a0\u00a0 } catch(IOException ioe) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 System.out.println(\"Error creating tree: \" + ioe);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0 }\r\n}\r\n\r\nExtensionFileFilter.java Allows you to specify which file extensions will be displayed in a JFileChooser.\r\n\r\nimport java.io.File;\r\nimport java.util.*;\r\nimport javax.swing.*;\r\nimport javax.swing.filechooser.FileFilter;\r\n\r\n\/** A FileFilter that lets you specify which file extensions \r\n\u00a0*\u00a0 will be displayed. Also includes a static getFileName \r\n\u00a0*\u00a0 method that users can call to pop up a JFileChooser for \r\n\u00a0*\u00a0 a set of file extensions.\r\n\u00a0* \u00a0\r\n\r\n\r\n\u00a0*\u00a0 Adapted from Sun SwingSet demo.\r\n\u00a0*\r\n\u00a0*\u00a0 Taken from Core Web Programming from \r\n\u00a0*\u00a0 Prentice Hall and Sun Microsystems Press,\r\n\u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown;\r\n\u00a0*\u00a0 may be freely used or adapted. \r\n\u00a0*\/\r\n\r\npublic class ExtensionFileFilter extends FileFilter {\r\n\u00a0 public static final int LOAD = 0;\r\n\u00a0 public static final int SAVE = 1;\r\n\u00a0 private String description;\r\n\u00a0 private boolean allowDirectories;\r\n\u00a0 private Hashtable extensionsTable = new Hashtable();\r\n\u00a0 private boolean allowAll = false;\r\n\r\n\u00a0 public ExtensionFileFilter(boolean allowDirectories) {\r\n\u00a0\u00a0\u00a0 this.allowDirectories = allowDirectories;\r\n\u00a0 }\r\n\r\n\u00a0 public ExtensionFileFilter() {\r\n\u00a0\u00a0\u00a0 this(true);\r\n\u00a0 }\r\n\u00a0 \r\n\u00a0 \r\n\u00a0 public static String getFileName(String initialDirectory,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String description,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String extension) {\r\n\u00a0\u00a0\u00a0 String[] extensions = new String[]{ extension };\r\n\u00a0\u00a0\u00a0 return(getFileName(initialDirectory, description, \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 extensions, LOAD));\r\n\u00a0 } \u00a0\r\n\r\n\u00a0 public static String getFileName(String initialDirectory,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String description,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String extension,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 int mode) {\r\n\u00a0\u00a0\u00a0 String[] extensions = new String[]{ extension };\r\n\u00a0\u00a0\u00a0 return(getFileName(initialDirectory, description, \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 extensions, mode));\r\n\u00a0 }\r\n\r\n\u00a0 public static String getFileName(String initialDirectory,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String description,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String[] extensions) {\r\n\u00a0\u00a0\u00a0 return(getFileName(initialDirectory, description, \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 extensions, LOAD));\r\n\u00a0 }\r\n\r\n\r\n\u00a0 \/** Pops up a JFileChooser that lists files with the \r\n\u00a0\u00a0 *\u00a0 specified extensions. If the mode is SAVE, then the \r\n\u00a0\u00a0 *\u00a0 dialog will have a Save button; otherwise, the dialog \r\n\u00a0\u00a0 *\u00a0 will have an Open button. Returns a String corresponding\r\n\u00a0\u00a0 *\u00a0 to the file's pathname, or null if Cancel was selected.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 public static String getFileName(String initialDirectory,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String description,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String[] extensions,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 int mode) {\r\n\u00a0\u00a0\u00a0 ExtensionFileFilter filter = new ExtensionFileFilter();\r\n\u00a0\u00a0\u00a0 filter.setDescription(description);\r\n\u00a0\u00a0\u00a0 for(int i=0; i\r\n\r\n\r\nWindowUtilities.java\r\n\r\nimport javax.swing.*;\r\nimport java.awt.*;\u00a0\u00a0 \/\/ For Color and Container classes.\r\n\r\n\/** A few utilities that simplify using windows in Swing. \r\n\u00a0*\r\n\u00a0*\u00a0 Taken from Core Web Programming from \r\n\u00a0*\u00a0 Prentice Hall and Sun Microsystems Press,\r\n\u00a0* \r\n\u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown;\r\n\u00a0*\u00a0 may be freely used or adapted.\r\n\u00a0*\/\r\n\r\npublic class WindowUtilities {\r\n\r\n\u00a0 \/** Tell system to use native look and feel, as in previous\r\n\u00a0\u00a0 *\u00a0 releases. Metal (Java) LAF is the default otherwise.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 public static void setNativeLookAndFeel() {\r\n\u00a0\u00a0\u00a0 try {\r\n\u00a0\u00a0\u00a0\u00a0 UIManager.setLookAndFeel(\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 UIManager.getSystemLookAndFeelClassName());\r\n\u00a0\u00a0\u00a0 } catch(Exception e) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 System.out.println(\"Error setting native LAF: \" + e);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0 }\r\n\r\n\u00a0 public static void setJavaLookAndFeel() {\r\n\u00a0\u00a0\u00a0 try {\r\n\u00a0\u00a0\u00a0\u00a0 UIManager.setLookAndFeel(\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 UIManager.getCrossPlatformLookAndFeelClassName());\r\n\u00a0\u00a0\u00a0 } catch(Exception e) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 System.out.println(\"Error setting Java LAF: \" + e);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0 }\r\n\r\n\u00a0\u00a0 public static void setMotifLookAndFeel() {\r\n\u00a0\u00a0\u00a0 try {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 UIManager.setLookAndFeel(\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"com.sun.java.swing.plaf.motif.MotifLookAndFeel\");\r\n\u00a0\u00a0\u00a0 } catch(Exception e) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 System.out.println(\"Error setting Motif LAF: \" + e);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0 }\r\n\r\n\u00a0 \/** A simplified way to see a JPanel or other Container. Pops\r\n\u00a0\u00a0 *\u00a0 up a JFrame with specified Container as the content pane.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 public static JFrame openInJFrame(Container content,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 int width,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 int height,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String title,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Color bgColor) {\r\n\u00a0\u00a0\u00a0 JFrame frame = new JFrame(title);\r\n\u00a0\u00a0\u00a0 frame.setBackground(bgColor);\r\n\u00a0\u00a0\u00a0 content.setBackground(bgColor);\r\n\u00a0\u00a0\u00a0 frame.setSize(width, height);\r\n\u00a0\u00a0\u00a0 frame.setContentPane(content);\r\n\u00a0\u00a0\u00a0 frame.addWindowListener(new ExitListener());\r\n\u00a0\u00a0\u00a0 frame.setVisible(true);\r\n\u00a0\u00a0\u00a0 return(frame);\r\n\u00a0 }\r\n\r\n\u00a0 \/** Uses Color.white as the background color. *\/\r\n\r\n\u00a0 public static JFrame openInJFrame(Container content,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 int width,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 int height,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String title) {\r\n\u00a0\u00a0\u00a0 return(openInJFrame(content, width, height,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 title, Color.white));\r\n\u00a0 }\r\n\r\n\u00a0 \/** Uses Color.white as the background color, and the\r\n\u00a0\u00a0 *\u00a0 name of the Container's class as the JFrame title.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 public static JFrame openInJFrame(Container content,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 int width,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 int height) {\r\n\u00a0\u00a0\u00a0 return(openInJFrame(content, width, height,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 content.getClass().getName(),\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Color.white));\r\n\u00a0 }\r\n}\r\n\r\n\r\n\r\nEXITListener.java\r\n\r\nimport java.awt.*;\r\nimport java.awt.event.*;\r\n\r\n\/** A listener that you attach to the top-level JFrame of\r\n\u00a0*\u00a0 your application, so that quitting the frame exits the \r\n\u00a0*\u00a0 application.\r\n\u00a0*\r\n\u00a0*\u00a0 Taken from Core Web Programming from \r\n\u00a0*\u00a0 Prentice Hall and Sun Microsystems Press,\r\n\u00a0* \u00a0\r\n\u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown;\r\n\u00a0*\u00a0 may be freely used or adapted. \r\n\u00a0*\/\r\npublic class ExitListener extends WindowAdapter {\r\n\u00a0 public void windowClosing(WindowEvent event) {\r\n\u00a0\u00a0\u00a0 System.exit(0);\r\n\u00a0 }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/\/XMLTree.java \/\/Uses the following files Uses the following files: \u00a0\u00a0\u00a0 * XMLFrame.java:Swing application to select an XML document and display in a JTree. ExtensionFileFilter.java Allows you to specify which file extensions will be displayed in a JFileChooser. test.xml Default file loaded if none selected by user. perennials.xml and perennials.dtd Data on daylilies and corresponding DTD. &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=10245\">Continue reading<\/a><\/p>\n","protected":false},"author":130,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1417,1424,1425],"tags":[706,308,285],"class_list":["post-10245","post","type-post","status-publish","format-standard","hentry","category-code-programming-samples--","category-javaj2eej2me","category-javascript","tag-code","tag-java","tag-285","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":26856,"url":"http:\/\/bangla.sitestree.com\/?p=26856","url_meta":{"origin":10245,"position":0},"title":"DOM example that represents the basic structure of an XML document as a JTree #Programming Code Examples #Java\/J2EE\/J2ME #JavaScript","author":"Author-Check- Article-or-Video","date":"May 3, 2021","format":false,"excerpt":"\/\/XMLTree.java \/\/Uses the following files Uses the following files: * XMLFrame.java:Swing application to select an XML document and display in a JTree. ExtensionFileFilter.java Allows you to specify which file extensions will be displayed in a JFileChooser. test.xml Default file loaded if none selected by user. perennials.xml and perennials.dtd Data on\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":23184,"url":"http:\/\/bangla.sitestree.com\/?p=23184","url_meta":{"origin":10245,"position":1},"title":"Creating Custom Tags in JSP #Root #By Sayed Ahmed","author":"Author-Check- Article-or-Video","date":"March 26, 2021","format":false,"excerpt":"By Sayed, January 13th, 2008 [use large font in IE\/Firefox] How to create custom tags in JSP? You can create custom tags in JSP. Steps: ----- 1. You have to create a Java file that will define the operation of the custom tag 2. For the Java file, you have\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10343,"url":"http:\/\/bangla.sitestree.com\/?p=10343","url_meta":{"origin":10245,"position":2},"title":"Uses a FileDialog to choose the file to display","author":"","date":"August 27, 2015","format":false,"excerpt":"DisplayFile.java **************** import java.awt.*; import java.awt.event.*; import java.io.*; \/** Uses a FileDialog to choose the file to display. \u00a0*************** \u00a0 public class DisplayFile extends CloseableFrame \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 implements ActionListener { \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 public static void main(String[] args) { \u00a0\u00a0\u00a0 new DisplayFile(); \u00a0 } \u00a0 private Button loadButton; \u00a0 private TextArea\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":7003,"url":"http:\/\/bangla.sitestree.com\/?p=7003","url_meta":{"origin":10245,"position":3},"title":"MongoDB Java","author":"Author-Check- Article-or-Video","date":"March 16, 2015","format":false,"excerpt":"Done By Raju(DU) MongoDB Java Installation \u09aa\u09a6\u09cd\u09a7\u09a4\u09bf\u0983 Java program \u098f MongoDB \u09ac\u09cd\u09af\u09be\u09ac\u09b9\u09be\u09b0 \u0995\u09b0\u09be\u09b0 \u09aa\u09c2\u09b0\u09cd\u09ac\u09c7 \u0986\u09ae\u09be\u09a6\u09c7\u09b0\u0995\u09c7 \u09a8\u09bf\u09b6\u09cd\u099a\u09bf\u09a4 \u0995\u09b0\u09a4\u09c7 \u09b9\u09ac\u09c7 \u09af\u09c7 MongoDB JDBC Driver \u098f\u09ac\u0982 Java \u0986\u09ae\u09be\u09a6\u09c7\u09b0 machine \u098f \u09aa\u09c2\u09b0\u09cd\u09ac\u09c7 \u09a5\u09c7\u0995\u09c7\u0987 \u09b0\u09df\u09c7\u099b\u09c7\u0964 \u0986\u09aa\u09a8\u09bf \u0986\u09b0\u0993 Java tutorial \u09a6\u09c7\u0996\u09a4\u09c7 \u09aa\u09be\u09b0\u09c7\u09a8 \u0986\u09aa\u09a8\u09be\u09b0 machine \u098f Java installation \u0995\u09b0\u09be\u09b0 \u099c\u09a8\u09cd\u09af\u0964 \u098f\u0996\u09a8 \u09a6\u09c7\u0996\u09ac\u09c7 \u0995\u09bf\u09ad\u09be\u09ac\u09c7 MongoDB JDBC driver \u099f\u09bf\u2026","rel":"","context":"In &quot;\u09ae\u0999\u09cd\u0997 \u09a1\u09bf \u09ac\u09bf \u0964 MongoDB&quot;","block_context":{"text":"\u09ae\u0999\u09cd\u0997 \u09a1\u09bf \u09ac\u09bf \u0964 MongoDB","link":"http:\/\/bangla.sitestree.com\/?cat=222"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27020,"url":"http:\/\/bangla.sitestree.com\/?p=27020","url_meta":{"origin":10245,"position":4},"title":"Uses a FileDialog to choose the file to display #Programming Code Examples #Java\/J2EE\/J2ME #AWT Components","author":"Author-Check- Article-or-Video","date":"May 8, 2021","format":false,"excerpt":"DisplayFile.java **************** import java.awt.*; import java.awt.event.*; import java.io.*; \/** Uses a FileDialog to choose the file to display. *************** public class DisplayFile extends CloseableFrame implements ActionListener { public static void main(String[] args) { new DisplayFile(); } private Button loadButton; private TextArea fileArea; private FileDialog loader; public DisplayFile() { super(\"Using FileDialog\");\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":69282,"url":"http:\/\/bangla.sitestree.com\/?p=69282","url_meta":{"origin":10245,"position":5},"title":"JSP: Random Information #37","author":"Author-Check- Article-or-Video","date":"August 16, 2021","format":false,"excerpt":"Three JSP constructs: scripting elements, actions, directives Scripting elements: expressions, scriptlets, declarations Expression = translates to println in servlets in _jspService methods < % = expression %> < % = new java.util.Date() %> inserts values directly to the output scriptlet: block of java code : directly inserted into the related\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/130"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10245"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10245\/revisions"}],"predecessor-version":[{"id":10246,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10245\/revisions\/10246"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10245"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}