k_s_ashok
asked on
create a tree structure (like windows explorer) in java-swing?
my project technology : java - swing
how to create a tree structure (like windows explorer) in java-swing?
( need the source code)
how to create a tree structure (like windows explorer) in java-swing?
( need the source code)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
//REFERENCES CLASSES------
import java.awt.*;
import java.awt.event.*;
/** A listener that you attach to the top-level Frame or JFrame of
* your application, so quitting the frame exits the application.
* 1998-99 Marty Hall, http://www.apl.jhu.edu/~hall/java/
*/
public class ExitListener extends WindowAdapter {
public void windowClosing(WindowEvent event) {
System.exit(0);
}
}
//------------------------ ---------- ---------- --------
import javax.swing.*;
import java.awt.*;
/** A few utilities that simplify using windows in Swing.
* 1998-99 Marty Hall, http://www.apl.jhu.edu/~hall/java/
*/
public class WindowUtilities {
/** Tell system to use native look and feel, as in previous
* releases. Metal (Java) LAF is the default otherwise.
*/
public static void setNativeLookAndFeel() {
try {
UIManager.setLookAndFeel(U IManager.g etSystemLo okAndFeelC lassName() );
} catch(Exception e) {
System.out.println("Error setting native LAF: " + e);
}
}
public static void setJavaLookAndFeel() {
try {
UIManager.setLookAndFeel(U IManager.g etCrossPla tformLookA ndFeelClas sName());
} catch(Exception e) {
System.out.println("Error setting Java LAF: " + e);
}
}
public static void setMotifLookAndFeel() {
try {
UIManager.setLookAndFeel(" com.sun.ja va.swing.p laf.motif. MotifLookA ndFeel");
} catch(Exception e) {
System.out.println("Error setting Motif LAF: " + e);
}
}
/** A simplified way to see a JPanel or other Container.
* Pops up a JFrame with specified Container as the content pane.
*/
public static JFrame openInJFrame(Container content,
int width,
int height,
String title,
Color bgColor) {
JFrame frame = new JFrame(title);
frame.setBackground(bgColo r);
content.setBackground(bgCo lor);
frame.setSize(width, height);
frame.setContentPane(conte nt);
frame.addWindowListener(ne w ExitListener());
frame.setVisible(true);
return(frame);
}
/** Uses Color.white as the background color. */
public static JFrame openInJFrame(Container content,
int width,
int height,
String title) {
return(openInJFrame(conten t, width, height, title, Color.white));
}
/** Uses Color.white as the background color, and the
* name of the Container's class as the JFrame title.
*/
public static JFrame openInJFrame(Container content,
int width,
int height) {
return(openInJFrame(conten t, width, height,
content.getClass().getName (),
Color.white));
}
}
import java.awt.*;
import java.awt.event.*;
/** A listener that you attach to the top-level Frame or JFrame of
* your application, so quitting the frame exits the application.
* 1998-99 Marty Hall, http://www.apl.jhu.edu/~hall/java/
*/
public class ExitListener extends WindowAdapter {
public void windowClosing(WindowEvent event) {
System.exit(0);
}
}
//------------------------
import javax.swing.*;
import java.awt.*;
/** A few utilities that simplify using windows in Swing.
* 1998-99 Marty Hall, http://www.apl.jhu.edu/~hall/java/
*/
public class WindowUtilities {
/** Tell system to use native look and feel, as in previous
* releases. Metal (Java) LAF is the default otherwise.
*/
public static void setNativeLookAndFeel() {
try {
UIManager.setLookAndFeel(U
} catch(Exception e) {
System.out.println("Error setting native LAF: " + e);
}
}
public static void setJavaLookAndFeel() {
try {
UIManager.setLookAndFeel(U
} catch(Exception e) {
System.out.println("Error setting Java LAF: " + e);
}
}
public static void setMotifLookAndFeel() {
try {
UIManager.setLookAndFeel("
} catch(Exception e) {
System.out.println("Error setting Motif LAF: " + e);
}
}
/** A simplified way to see a JPanel or other Container.
* Pops up a JFrame with specified Container as the content pane.
*/
public static JFrame openInJFrame(Container content,
int width,
int height,
String title,
Color bgColor) {
JFrame frame = new JFrame(title);
frame.setBackground(bgColo
content.setBackground(bgCo
frame.setSize(width, height);
frame.setContentPane(conte
frame.addWindowListener(ne
frame.setVisible(true);
return(frame);
}
/** Uses Color.white as the background color. */
public static JFrame openInJFrame(Container content,
int width,
int height,
String title) {
return(openInJFrame(conten
}
/** Uses Color.white as the background color, and the
* name of the Container's class as the JFrame title.
*/
public static JFrame openInJFrame(Container content,
int width,
int height) {
return(openInJFrame(conten
content.getClass().getName
Color.white));
}
}
This question is LOCKED with a Proposed Answer. If it helps you, great, accept it and grade it to close. If not, reject it and comment as to why or what else is needed.
Few additional experts will join this collaboration effort, once a question has been locked. Just want to confirm this to you. If more than one expert helps you, you can always split points or award additional help in a new question, within the same topic area. If you need help from us, post a zero point question here, and include the link:
https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
Moondancer
Community Support Moderator @ Experts Exchange
Few additional experts will join this collaboration effort, once a question has been locked. Just want to confirm this to you. If more than one expert helps you, you can always split points or award additional help in a new question, within the same topic area. If you need help from us, post a zero point question here, and include the link:
https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
Moondancer
Community Support Moderator @ Experts Exchange
Force Accepted
SpideyMod
Community Support Moderator @Experts Exchange
SpideyMod
Community Support Moderator @Experts Exchange
import javax.swing.*;
import javax.swing.tree.*;
/** Example tree built out of DefaultMutableTreeNodes.
* 1999 Marty Hall, http://www.apl.jhu.edu/~hall/java/
*/
public class SimpleTree extends JFrame {
public static void main(String[] args) {
new SimpleTree();
}
public SimpleTree() {
super("Creating a Simple JTree");
WindowUtilities.setNativeL
addWindowListener(new ExitListener());
Container content = getContentPane();
Object[] hierarchy =
{ "javax.swing",
"javax.swing.border",
"javax.swing.colorchooser"
"javax.swing.event",
"javax.swing.filechooser",
new Object[] { "javax.swing.plaf",
"javax.swing.plaf.basic",
"javax.swing.plaf.metal",
"javax.swing.plaf.multi" },
"javax.swing.table",
new Object[] { "javax.swing.text",
new Object[] { "javax.swing.text.html",
"javax.swing.text.html.par
"javax.swing.text.rtf" },
"javax.swing.tree",
"javax.swing.undo" };
DefaultMutableTreeNode root = processHierarchy(hierarchy
JTree tree = new JTree(root);
content.add(new JScrollPane(tree), BorderLayout.CENTER);
setSize(275, 300);
setVisible(true);
}
/** Small routine that will make node out of the first entry
* in the array, then make nodes out of subsequent entries
* and make them child nodes of the first one. The process is
* repeated recursively for entries that are arrays.
*/
private DefaultMutableTreeNode processHierarchy(Object[] hierarchy) {
DefaultMutableTreeNode node =
new DefaultMutableTreeNode(hie
DefaultMutableTreeNode child;
for(int i=1; i<hierarchy.length; i++) {
Object nodeSpecifier = hierarchy[i];
if (nodeSpecifier instanceof Object[]) // Ie node with children
child = processHierarchy((Object[]
else
child = new DefaultMutableTreeNode(nod
node.add(child);
}
return(node);
}
}