I just need the functionality of being able to click on a node on the left hand side of my GUI, and Display a Card from the cardLayout on the right side.
Example
<Device>
<Module>
<Status>
<Device>
</Device>
<Network>
</Network>
</Status>
<Project Management>
</Project Management>
<Administration>
<Device>
</Device>
<Network>
</Network>
</Administration>
</Module>
</Device>
So if I click on Administration a Card from the cardLayout is displayed. I want a card from each Node element. So, if I choose Administration--> Network. I get a Card for the Network.
Some of the Cards have JTables in them. or even a JTabbedPane.
Here is the Structure
--
| | |
| | |
| | |
| | |
| | |
--
^ ^^
| ||
JTree |CardLayout
|
JSplitPane
here's a simple example
(this is old, not sure where it came from)
import java.awt.*;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.util.*;
class Testing extends JFrame
{
CardLayout cl = new CardLayout();
JPanel panel = new JPanel(cl);
int counter = 0;
public Testing()
{
setSize(400,200);
setLocation(400,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(new GridLayout(1,2));
JTree tree = new JTree();
getContentPane().add(new JScrollPane(tree));
loadCardPanels((DefaultMutableTreeNode)((DefaultTreeModel)tree.getModel()).getRoot());
getContentPane().add(panel);
tree.addTreeSelectionListener(new TreeSelectionListener(){
public void valueChanged(TreeSelectionEvent tse){
if(((TreeNode)tse.getPath().getLastPathComponent()).isLeaf())
{
cl.show(panel,((TreeNode)tse.getPath().getLastPathComponent()).toString());
}}});
}
public void loadCardPanels(DefaultMutableTreeNode dmtn)
{
for(int x = 0; x < dmtn.getChildCount(); x++)
{
if(((DefaultMutableTreeNode)dmtn.getChildAt(x)).isLeaf())
{
String leaf = dmtn.getChildAt(x).toString();
JPanel jp = new JPanel();
jp.add(new JLabel(leaf+" (on Panel "+counter+")"));
panel.add(leaf,jp);
counter++;
}
else loadCardPanels((DefaultMutableTreeNode)dmtn.getChildAt(x));
}
}
public static void main(String[] args){new Testing().setVisible(true);}
}
Actually, this doesn't work like I thought it would. I like that it does have the panels on the right hand side now, but I need to have the panel pre configured. I am actually getting data from xml files, and the panels will pretty much stay the same.
So instead of creating a panel when I click on the node. I need to parse some xml, and display a certain panel.
So, for example.
if node name equals Status
{
Display Status panel;
}
now where it becomes difficult is:
if node name equals Network.
{
I will have to get the parent.
if Parent equals Status
{
get the Network Status panel
}
else
{
display the Network Administration panel
}
}
I would appreciate anyone that might have an example like this.
>>in the demo, the panels are pre-configured in loadCardPanels() then, on clicking a leaf cl.show(...) displays the appropriate panel
I am sorry but I am having a hard time following this code:
please explain or better yet give me an example in this.
Lets say I want a panel "Device", and another Panel "Network".
They both have a save button and some text fields on the panel.
How do I create the panel in loadCardPanels, and display that panel in c1.show()
I have to call the panel by name, because the tree model may change.
in loadCardPanels, if getChildAt(x) is a leaf:
a String leaf (identifier) is created
a JPanel is created
a JLabel with text of identifier is created and added to the created panel
the created panel is added to the cardlayout panel, with leaf as identifier
in valueChanged() of treeSelectionListener (again if it is a leaf), cardLayout.show uses
((TreeNode)tse.getPath().getLastPathComponent()).toString()
to produce the same string as
dmtn.getChildAt(x).toString();
which is used as the identifier (as in hashmap) to show the specified panel
not sure what you're asking about "a panel "Device", and another Panel "Network"."
are they both cardlayouts?, or both contained in a cardlayout?
can you put together a very simple demo program, as described with
Jtree, "Device","Network",textfields and save button, and run through the steps of
what you want to happen
Yeah sure. I modified your program just a little. I loaded the tree that I need.
Here is the program.
run your program with this class
import javax.swing.*;
import javax.swing.tree.*;
/**
*
* @author orozcom
*/
public class ConfigTree
{
/**
* Creates a new instance of ConfigTree
*/
public ConfigTree()
{
}
public Object buildTree()
{
/************ Start of JTree ***************/
DefaultMutableTreeNode top =
new DefaultMutableTreeNode("Devices");
DefaultMutableTreeNode branch1 =
new DefaultMutableTreeNode("Module 1");
top.add(branch1);
/*******************************
* BRANCH 1*
******************************/
DefaultMutableTreeNode node1_b1
=new DefaultMutableTreeNode("Status");
DefaultMutableTreeNode n1_node1_b1 =
new DefaultMutableTreeNode("Device");
DefaultMutableTreeNode n2_node1_b1 =
new DefaultMutableTreeNode("Network");
DefaultMutableTreeNode n3_node1_b1 =
new DefaultMutableTreeNode("Chassis");
DefaultMutableTreeNode n4_node1_b1 =
new DefaultMutableTreeNode("Resources");
node1_b1.add(n1_node1_b1);
node1_b1.add(n2_node1_b1);
node1_b1.add(n3_node1_b1);
node1_b1.add(n4_node1_b1);
DefaultMutableTreeNode node2_b1 =
new DefaultMutableTreeNode("Project Editor");
DefaultMutableTreeNode node3_b1 =
new DefaultMutableTreeNode("Project Manager");
DefaultMutableTreeNode node4_b1 =
new DefaultMutableTreeNode("Administration");
DefaultMutableTreeNode n1_node4_b1 =
new DefaultMutableTreeNode("Device");
DefaultMutableTreeNode n2_node4_b1 =
new DefaultMutableTreeNode("Network");
DefaultMutableTreeNode n3_node4_b1 =
new DefaultMutableTreeNode("Users");
node4_b1.add(n1_node4_b1);
node4_b1.add(n2_node4_b1);
node4_b1.add(n3_node4_b1);
DefaultMutableTreeNode node5_b1 =
new DefaultMutableTreeNode("Logging");
branch1.add(node1_b1);
branch1.add(node2_b1);
branch1.add(node3_b1);
branch1.add(node4_b1);
branch1.add(node5_b1);
return top;
}
}
And
Just add this code into Testing.java
ConfigTree test = new ConfigTree();
DefaultMutableTreeNode mainTree = (DefaultMutableTreeNode)test.buildTree();
JTree tree = new JTree(mainTree);
I'me reading this that you're trying to show different panels with the same leaf name.
This seems to work OK, but there's gotta be an easier way
(click any of the leafs and its panel will display - showing parent/leaf as title)
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.util.*;
class Testing extends JFrame
{
CardLayout cl = new CardLayout();
JPanel panel = new JPanel(cl);
public Testing()
{
JPanel blankPanel = new JPanel();
blankPanel.setBorder(BorderFactory.createTitledBorder("Blank Panel"));
panel.add(blankPanel,"0");
setSize(600,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(new GridLayout(1,2));
ConfigTree test = new ConfigTree();
DefaultMutableTreeNode mainTree = (DefaultMutableTreeNode)test.buildTree();
JTree tree = new JTree(mainTree);
getContentPane().add(new JScrollPane(tree));
loadCardPanels((DefaultMutableTreeNode)((DefaultTreeModel)tree.getModel()).getRoot());
getContentPane().add(panel);
tree.addTreeSelectionListener(new TreeSelectionListener(){
public void valueChanged(TreeSelectionEvent tse){
if(((TreeNode)tse.getPath().getLastPathComponent()).isLeaf())
{
String cardLayoutID = ((NodeWithID) ((DefaultMutableTreeNode)((TreePath)tse.getPath())
.getLastPathComponent()).getUserObject() ).ID;
cl.show(panel,cardLayoutID);
}}});
cl.show(panel,"0");
}
public void loadCardPanels(DefaultMutableTreeNode dmtn)
{
for(int x = 0; x < dmtn.getChildCount(); x++)
{
if(((DefaultMutableTreeNode)dmtn.getChildAt(x)).isLeaf())
{
String cardLayoutID = ((NodeWithID)((DefaultMutableTreeNode)dmtn.getChildAt(x)).getUserObject()).ID;
JPanel jp = new JPanel();
jp.setBorder(BorderFactory.createTitledBorder(cardLayoutID+" Panel"));
panel.add(cardLayoutID,jp);
}
else loadCardPanels((DefaultMutableTreeNode)dmtn.getChildAt(x));
}
}
public static void main(String[] args){new Testing().setVisible(true);}
}
class ConfigTree
{
public Object buildTree()
{
NodeWithID n0 = new NodeWithID("Devices","");
NodeWithID n1 = new NodeWithID("Module 1",n0.nodeName);
NodeWithID n2 = new NodeWithID("Status",n1.nodeName);
NodeWithID n3 = new NodeWithID("Device",n2.nodeName);
NodeWithID n4 = new NodeWithID("Network",n2.nodeName);
NodeWithID n5 = new NodeWithID("Chassis",n2.nodeName);
NodeWithID n6 = new NodeWithID("Resources",n2.nodeName);
NodeWithID n7 = new NodeWithID("Project Editor",n1.nodeName);
NodeWithID n8 = new NodeWithID("Project Manager",n1.nodeName);
NodeWithID n9 = new NodeWithID("Administration",n1.nodeName);
NodeWithID n10 = new NodeWithID("Device",n9.nodeName);
NodeWithID n11 = new NodeWithID("Network",n9.nodeName);
NodeWithID n12 = new NodeWithID("Users",n9.nodeName);
NodeWithID n13 = new NodeWithID("Logging",n1.nodeName);
DefaultMutableTreeNode top =
//new DefaultMutableTreeNode("Devices");
new DefaultMutableTreeNode(n0);
DefaultMutableTreeNode branch1 =
//new DefaultMutableTreeNode("Module 1");
new DefaultMutableTreeNode(n1);
top.add(branch1);
/*******************************
* BRANCH 1*
******************************/
DefaultMutableTreeNode node1_b1
//=new DefaultMutableTreeNode("Status");
=new DefaultMutableTreeNode(n2);
DefaultMutableTreeNode n1_node1_b1 =
//new DefaultMutableTreeNode("Device");
new DefaultMutableTreeNode(n3);
DefaultMutableTreeNode n2_node1_b1 =
//new DefaultMutableTreeNode("Network");
new DefaultMutableTreeNode(n4);
DefaultMutableTreeNode n3_node1_b1 =
//new DefaultMutableTreeNode("Chassis");
new DefaultMutableTreeNode(n5);
DefaultMutableTreeNode n4_node1_b1 =
//new DefaultMutableTreeNode("Resources");
new DefaultMutableTreeNode(n6);
node1_b1.add(n1_node1_b1);
node1_b1.add(n2_node1_b1);
node1_b1.add(n3_node1_b1);
node1_b1.add(n4_node1_b1);
DefaultMutableTreeNode node2_b1 =
//new DefaultMutableTreeNode("Project Editor");
new DefaultMutableTreeNode(n7);
DefaultMutableTreeNode node3_b1 =
//new DefaultMutableTreeNode("Project Manager");
new DefaultMutableTreeNode(n8);
DefaultMutableTreeNode node4_b1 =
//new DefaultMutableTreeNode("Administration");
new DefaultMutableTreeNode(n9);
DefaultMutableTreeNode n1_node4_b1 =
//new DefaultMutableTreeNode("Device");
new DefaultMutableTreeNode(n10);
DefaultMutableTreeNode n2_node4_b1 =
//new DefaultMutableTreeNode("Network");
new DefaultMutableTreeNode(n11);
DefaultMutableTreeNode n3_node4_b1 =
//new DefaultMutableTreeNode("Users");
new DefaultMutableTreeNode(n12);
node4_b1.add(n1_node4_b1);
node4_b1.add(n2_node4_b1);
node4_b1.add(n3_node4_b1);
DefaultMutableTreeNode node5_b1 =
//new DefaultMutableTreeNode("Logging");
new DefaultMutableTreeNode(n13);
branch1.add(node1_b1);
branch1.add(node2_b1);
branch1.add(node3_b1);
branch1.add(node4_b1);
branch1.add(node5_b1);
return top;
}
}
class NodeWithID
{
String nodeName;
String ID;
public NodeWithID(String nn,String parentName)
{
nodeName = nn;
ID = parentName+" - "+nodeName;
}
public String toString(){return nodeName;}
}
Yes, Yes, Yes! This is exactly what I need! Thanks so much! I will reward you the dukes.
Could I also get a little more help. Now I need to create unique fields in each of the panels. Like have a few text fields and labels in one panel, and A table in another.
example.
in the Devices.Module 1.Status.Device panel.
Header Label --> Device Information
Label -->Name TextField -->Yellowfin Demo
Label -->DescriptionTextField -->Beta Tag mapper
Label -->Location TextField -->Jim's 13 slot rack
Label -->Contact TextField -->Jim
Label -->Serial NumberTextField -->
but the panel in Logging is a JTable with different columns.
The data in the text fields I will parse from an xml file an put into these fields.
I will also need a save button on each of the panels so that I can save this information back to the xml file.
I would kind of like something like this:
http://forum.java.sun.com/thread.jspa?threadID=697887&start=10&tstart=0
except I don't need to build my tree. I already have them prebuilt. I would like to have the buttons on top to add, edit, save tags in the xml. And I would like it configured so that when you click on a certain node a panel is displayed that shows the information about that particuler info.
the code so far just displays an empty panel identified by the node/parent,
but each panel is different and needs to be built separately. The problem sounds
like how to load the separate panels in loadCardPanels().
just a thought (untested)
add a JPanel field to NodeWithID, and add additional constructor
class NodeWithID
{
JPanel panel;
String nodeName;
String ID;
public NodeWithID(String nn,String parentName)
{
nodeName = nn;
ID = parentName+" - "+nodeName;
}
public NodeWithID(String nn,String parentName, JPanel p)
{
panel = p;
nodeName = nn;
ID = parentName+" - "+nodeName;
}
public String toString(){return nodeName;}
}
now, when NodeWithID (for the leafs) is created e.g.
NodeWithID n3 = new NodeWithID("Device",n2.nodeName);
becomes
NodeWithID n3 = new NodeWithID("Device",n2.nodeName,new StatusDevicePanel());
and, in loadCardPanels()
if(((DefaultMutableTreeNode)dmtn.getChildAt(x)).isLeaf())
{
String cardLayoutID = ((NodeWithID)((DefaultMutableTreeNode)dmtn.getChildAt(x)).getUserObject()).ID;
JPanel jp = new JPanel();
jp.setBorder(BorderFactory.createTitledBorder(cardLayoutID+" Panel"));
panel.add(cardLayoutID,jp);
}
becomes
if(((DefaultMutableTreeNode)dmtn.getChildAt(x)).isLeaf())
{
NodeWithID node = (NodeWithID)((DefaultMutableTreeNode)dmtn.getChildAt(x)).getUserObject();
String cardLayoutID = node.ID;
panel.add(cardLayoutID,node.panel);
}
probably a few typos in the above, but should work
The error I get is:
Cannot find symbol:
symbol : class StatusDevicePanel
location: class treeTest.ConfigTree
NodeWithID n3 = new NodeWithID("Device", n2.nodeName, new StatusDevicePanel());
So I created a class called: StatusDevicePanel
empty class, but anyway I get: a new error:
C:\PROJ\Configurator\src\treeTest\Testing.java:102: cannot find symbol
symbol : constructor NodeWithID(java.lang.String,java.lang.String,treeTest.StatusDevicePanel)
location: class treeTest.NodeWithID
NodeWithID n3 = new NodeWithID("Device", n2.nodeName, new StatusDevicePanel());
1 error
Do I need to make al the NodeWithID's the same?
> So do I need to create a StatusDevicePanel class? Basically a class for
> each Panel that I want to display?
Yes - either a class, or a method that builds/returns a JPanel e.g getStatusDevicePanel()
If you want the panels to be built as-you-go, there would be little point having a cardlayout
here's the code modified to handle just the single panel (StatusDevicePanel),
which seems to work OK - click the StatusDevicePanel leaf to see the panel with textfield
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.util.*;
class Testing extends JFrame
{
CardLayout cl = new CardLayout();
JPanel panel = new JPanel(cl);
public Testing()
{
JPanel blankPanel = new JPanel();
blankPanel.setBorder(BorderFactory.createTitledBorder("Blank Panel"));
panel.add(blankPanel,"0");
setSize(600,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(new GridLayout(1,2));
ConfigTree test = new ConfigTree();
DefaultMutableTreeNode mainTree = (DefaultMutableTreeNode)test.buildTree();
JTree tree = new JTree(mainTree);
getContentPane().add(new JScrollPane(tree));
loadCardPanels((DefaultMutableTreeNode)((DefaultTreeModel)tree.getModel()).getRoot());
getContentPane().add(panel);
tree.addTreeSelectionListener(new TreeSelectionListener(){
public void valueChanged(TreeSelectionEvent tse){
if(((TreeNode)tse.getPath().getLastPathComponent()).isLeaf())
{
String cardLayoutID = ((NodeWithID) ((DefaultMutableTreeNode)((TreePath)tse.getPath())
.getLastPathComponent()).getUserObject() ).ID;
cl.show(panel,cardLayoutID);
}}});
cl.show(panel,"0");
}
public void loadCardPanels(DefaultMutableTreeNode dmtn)
{
for(int x = 0; x < dmtn.getChildCount(); x++)
{
//if(((DefaultMutableTreeNode)dmtn.getChildAt(x)).isLeaf())
//{
//String cardLayoutID = ((NodeWithID)((DefaultMutableTreeNode)dmtn.getChildAt(x)).getUserObject()).ID;
//JPanel jp = new JPanel();
//jp.setBorder(BorderFactory.createTitledBorder(cardLayoutID+" Panel"));
//panel.add(cardLayoutID,jp);
//}
if(((DefaultMutableTreeNode)dmtn.getChildAt(x)).isLeaf())
{
NodeWithID node = (NodeWithID)((DefaultMutableTreeNode)dmtn.getChildAt(x)).getUserObject();
String cardLayoutID = node.ID;
if(cardLayoutID.equals("Status - Device"))//<--just for testing that one panel works OK
{
panel.add(cardLayoutID,node.panel);
}
else
{
JPanel jp = new JPanel();
jp.setBorder(BorderFactory.createTitledBorder(cardLayoutID+" Panel"));
panel.add(cardLayoutID,jp);
}
}
else loadCardPanels((DefaultMutableTreeNode)dmtn.getChildAt(x));
}
}
public static void main(String[] args){new Testing().setVisible(true);}
}
class ConfigTree
{
public Object buildTree()
{
NodeWithID n0 = new NodeWithID("Devices","");
NodeWithID n1 = new NodeWithID("Module 1",n0.nodeName);
NodeWithID n2 = new NodeWithID("Status",n1.nodeName);
//NodeWithID n3 = new NodeWithID("Device",n2.nodeName);
NodeWithID n3 = new NodeWithID("Device",n2.nodeName,new StatusDevicePanel());
NodeWithID n4 = new NodeWithID("Network",n2.nodeName);
NodeWithID n5 = new NodeWithID("Chassis",n2.nodeName);
NodeWithID n6 = new NodeWithID("Resources",n2.nodeName);
NodeWithID n7 = new NodeWithID("Project Editor",n1.nodeName);
NodeWithID n8 = new NodeWithID("Project Manager",n1.nodeName);
NodeWithID n9 = new NodeWithID("Administration",n1.nodeName);
NodeWithID n10 = new NodeWithID("Device",n9.nodeName);
NodeWithID n11 = new NodeWithID("Network",n9.nodeName);
NodeWithID n12 = new NodeWithID("Users",n9.nodeName);
NodeWithID n13 = new NodeWithID("Logging",n1.nodeName);
DefaultMutableTreeNode top =
//new DefaultMutableTreeNode("Devices");
new DefaultMutableTreeNode(n0);
DefaultMutableTreeNode branch1 =
//new DefaultMutableTreeNode("Module 1");
new DefaultMutableTreeNode(n1);
top.add(branch1);
/*******************************
* BRANCH 1*
******************************/
DefaultMutableTreeNode node1_b1
//=new DefaultMutableTreeNode("Status");
=new DefaultMutableTreeNode(n2);
DefaultMutableTreeNode n1_node1_b1 =
//new DefaultMutableTreeNode("Device");
new DefaultMutableTreeNode(n3);
DefaultMutableTreeNode n2_node1_b1 =
//new DefaultMutableTreeNode("Network");
new DefaultMutableTreeNode(n4);
DefaultMutableTreeNode n3_node1_b1 =
//new DefaultMutableTreeNode("Chassis");
new DefaultMutableTreeNode(n5);
DefaultMutableTreeNode n4_node1_b1 =
//new DefaultMutableTreeNode("Resources");
new DefaultMutableTreeNode(n6);
node1_b1.add(n1_node1_b1);
node1_b1.add(n2_node1_b1);
node1_b1.add(n3_node1_b1);
node1_b1.add(n4_node1_b1);
DefaultMutableTreeNode node2_b1 =
//new DefaultMutableTreeNode("Project Editor");
new DefaultMutableTreeNode(n7);
DefaultMutableTreeNode node3_b1 =
//new DefaultMutableTreeNode("Project Manager");
new DefaultMutableTreeNode(n8);
DefaultMutableTreeNode node4_b1 =
//new DefaultMutableTreeNode("Administration");
new DefaultMutableTreeNode(n9);
DefaultMutableTreeNode n1_node4_b1 =
//new DefaultMutableTreeNode("Device");
new DefaultMutableTreeNode(n10);
DefaultMutableTreeNode n2_node4_b1 =
//new DefaultMutableTreeNode("Network");
new DefaultMutableTreeNode(n11);
DefaultMutableTreeNode n3_node4_b1 =
//new DefaultMutableTreeNode("Users");
new DefaultMutableTreeNode(n12);
node4_b1.add(n1_node4_b1);
node4_b1.add(n2_node4_b1);
node4_b1.add(n3_node4_b1);
DefaultMutableTreeNode node5_b1 =
//new DefaultMutableTreeNode("Logging");
new DefaultMutableTreeNode(n13);
branch1.add(node1_b1);
branch1.add(node2_b1);
branch1.add(node3_b1);
branch1.add(node4_b1);
branch1.add(node5_b1);
return top;
}
}
class NodeWithID
{
JPanel panel;
String nodeName;
String ID;
public NodeWithID(String nn,String parentName)
{
nodeName = nn;
ID = parentName+" - "+nodeName;
}
public NodeWithID(String nn,String parentName, JPanel p)
{
panel = p;
nodeName = nn;
ID = parentName+" - "+nodeName;
}
public String toString(){return nodeName;}
}
class StatusDevicePanel extends JPanel
{
public StatusDevicePanel()
{
add(new JTextField("Status - Device Panel",20));
}
}
Just so you know. It started to look like this:
for(int x = 0; x < dmtn.getChildCount(); x++)
{
if(((DefaultMutableTreeNode)dmtn.getChildAt(x)).isLeaf())
{
NodeWithID node = (NodeWithID)((DefaultMutableTreeNode)dmtn.getChildAt(x)).getUserObject();
String cardLayoutID = node.ID;
if(cardLayoutID.equals("Status - Device"))
{
panel.add(cardLayoutID,node.panel);
}
else if(cardLayoutID.equals("Status - Network"))
{
panel.add(cardLayoutID,node.panel);
}
else if(cardLayoutID.equals("Status - Chassis"))
{
panel.add(cardLayoutID,node.panel);
}
else if(cardLayoutID.equals("Status - Resources"))
{
panel.add(cardLayoutID,node.panel);
}
else
{
JPanel jp = new JPanel();
jp.setBorder(BorderFactory.createTitledBorder(cardLayoutID + " Panel"));
panel.add(cardLayoutID,jp);
}
}
else loadCardPanels((DefaultMutableTreeNode)dmtn.getChildAt(x));
}
then I noticed that the:
panel.add(cardLayoutID,node.panel);
never changes. How can I rewrite this so that its reversed?! in other words.
if (blah, blah, blah)
{
}
else
{
panel.add(cardLayoutID,node.panel);
}
Do you think this would work better?
the
if(cardLayoutID.equals("Status - Device"))
in the modified example was just to isolate the one panel that was supplied to
NodeWithID as an argument.
in the full code, all of the leafs would be constructed like this
NodeWithID n3 = new NodeWithID("Device",n2.nodeName,new StatusDevicePanel());
NodeWithID n4 = new NodeWithID("Network",n2.nodeName,new StatusNetworkPanel());
NodeWithID n5 = new NodeWithID("Chassis",n2.nodeName,new StatusChassisPanel());
NodeWithID n6 = new NodeWithID("Resources",n2.nodeName,new StatusResourcesPanel());
same for all the leafs
all that is required then, instead of a bunch of if/elses, is this part from reply #14
if(((DefaultMutableTreeNode)dmtn.getChildAt(x)).isLeaf())
{
NodeWithID node = (NodeWithID)((DefaultMutableTreeNode)dmtn.getChildAt(x)).getUserObject();
String cardLayoutID = node.ID;
panel.add(cardLayoutID,node.panel);
}
I put that part in, and created Classes for each of my components. It is working fantastic. Better than I thought. I love that I can do whatever I want in each Panel Class.
I can call other classes that parse information, or put it into an xml file.
Thanks again for your help.
I have a few more questions.
I resized the app, because it is just to small. I said:
setSize(1000,700);
this is a good size for what I need, but the app is split right down the middle like so:
1#
-
| | |
| | |
| | |
-
I want it to look more like this.
:-
|||
|||
|||
-
2#
One of my class panel contains a sortable JTable. It works fantastic, unless I get alot of data in the table. I need it to Scroll when it gets to a certain point.
Here is the code that Used for the sortable JTable
setBorder(BorderFactory.createTitledBorder("Logging Panel"));
Object rows[][] = {
{"12/22/06","07:25:53","856h","Socket Error"},
{"12/22/06","07:35:53", "810h","Socket Error"},
{"12/22/06","07:25:53", "856h","Config Error"},
{"12/22/06","09:25:53", "856h","Syntax Error"},
{"12/22/06","07:55:53", "810h","Socket Error"},
{"12/22/06","10:25:53","856h","Config Error"},
{"12/22/06","07:25:33","865h","Syntax Error"},
{"12/21/06","04:25:53","810h","Syntax Error"},
{"12/21/06","07:25:53","810h","Config Error"},
{"12/19/06","12:25:53","856h","Config Error"}
};
String columns[] = {"Date", "Time", "Event", "Description"};
TableModel model =
new DefaultTableModel(rows, columns) {
public Class getColumnClass(int column) {
Class returnValue;
if ((column >= 0) && (column < getColumnCount())) {
returnValue = getValueAt(0, column).getClass();
} else {
returnValue = Object.class;
}
return returnValue;
}
};
JTable table = new JTable(model);
RowSorter<TableModel> sorter =
new TableRowSorter<TableModel>(model);
table.setRowSorter(sorter);
JScrollPane pane = new JScrollPane(table);
add(pane, BorderLayout.CENTER);
3#
and how do I add a menu bar - top, tool bar - top, and status bar at the bottom to this app?
I took out alot of the classes, and put them into there own individual classes. It just makes it easier to read for me.
Thanks alot again
orozcom
#1
> this is a good size for what I need, but the app is split right down the middle like so:
contentPane is set to gridlayout(1,2).
perhaps the easiest alternative is to use a JSplitpane
get rid of these
//getContentPane().setLayout(new GridLayout(1,2));
//getContentPane().add(new JScrollPane(tree));
..
//getContentPane().add(panel);
for this
JSplitPane spt= new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,new JScrollPane(tree),panel);
spt.setResizeWeight(.25);//adjust to where you want the split
spt.setDividerSize(2);//optional - sets width of divider
spt.setEnabled(false);//cannot move divider
getContentPane().add(spt);
#2
> I need it to Scroll when it gets to a certain point.
table.scrollRectToVisible(table.getCellRect([rowNumber], 0, true));
Thanks,
That worked pretty good. I had trouble with the spt.setResizeWeight(.200);//adjust to where you want the split
so I used the spt.setDividerLocation(180);
instead
That seemed to work well.
I still cant get the table.scrollRectToVisible(table.getCellRect([rowNumber], 0, true));
To work though.
Again thanks for all of your help. This is coming along quite nice.
orozcom
Is there a way to not only get the leaf, but the other nodes as well? For example I like the way it is set up now, but I would like to click on the Module, Status folder, and popup a JPanel as well. Just like it is a leaf. I thought of using
if(!((DefaultMutableTreeNode)dmtn.getChildAt(x)).isLeaf())
but that just give me a null pointer exception.
I will keep trying new things, but if you have a solution that would be great
here's the earlier code (just panels with titles) with a few modifications in loadCardPanels() and the listener
(cheated a bit - just changed the 'blankPanel' code to accomodate Devices (root))
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import javax.swing.event.*;
class Testing extends JFrame
{
CardLayout cl = new CardLayout();
JPanel panel = new JPanel(cl);
public Testing()
{
JPanel blankPanel = new JPanel();
blankPanel.setBorder(BorderFactory.createTitledBorder("Devices Panel"));
panel.add(blankPanel,"0");
setSize(600,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(new GridLayout(1,2));
ConfigTree test = new ConfigTree();
DefaultMutableTreeNode mainTree = (DefaultMutableTreeNode)test.buildTree();
JTree tree = new JTree(mainTree);
getContentPane().add(new JScrollPane(tree));
loadCardPanels((DefaultMutableTreeNode)((DefaultTreeModel)tree.getModel()).getRoot());
getContentPane().add(panel);
tree.addTreeSelectionListener(new TreeSelectionListener(){
public void valueChanged(TreeSelectionEvent tse){
String cardLayoutID = "";
if(((DefaultMutableTreeNode)((TreePath)tse.getPath())
.getLastPathComponent()).isRoot()) cardLayoutID = "0";
else cardLayoutID = ((NodeWithID)((DefaultMutableTreeNode)((TreePath)tse.getPath())
.getLastPathComponent()).getUserObject() ).ID;
cl.show(panel,cardLayoutID);
}
});
cl.show(panel,"0");
}
public void loadCardPanels(DefaultMutableTreeNode dmtn)
{
for(int x = 0; x < dmtn.getChildCount(); x++)
{
if(((DefaultMutableTreeNode)dmtn.getChildAt(x)).isLeaf() == false)
{
loadCardPanels((DefaultMutableTreeNode)dmtn.getChildAt(x));
}
String cardLayoutID = ((NodeWithID)((DefaultMutableTreeNode)dmtn.getChildAt(x)).getUserObject()).ID;
JPanel jp = new JPanel();
jp.setBorder(BorderFactory.createTitledBorder(cardLayoutID+" Panel"));
panel.add(cardLayoutID,jp);
}
}
public static void main(String[] args){new Testing().setVisible(true);}
}
class ConfigTree
{
public Object buildTree()
{
NodeWithID n0 = new NodeWithID("Devices","");
NodeWithID n1 = new NodeWithID("Module 1",n0.nodeName);
NodeWithID n2 = new NodeWithID("Status",n1.nodeName);
NodeWithID n3 = new NodeWithID("Device",n2.nodeName);
NodeWithID n4 = new NodeWithID("Network",n2.nodeName);
NodeWithID n5 = new NodeWithID("Chassis",n2.nodeName);
NodeWithID n6 = new NodeWithID("Resources",n2.nodeName);
NodeWithID n7 = new NodeWithID("Project Editor",n1.nodeName);
NodeWithID n8 = new NodeWithID("Project Manager",n1.nodeName);
NodeWithID n9 = new NodeWithID("Administration",n1.nodeName);
NodeWithID n10 = new NodeWithID("Device",n9.nodeName);
NodeWithID n11 = new NodeWithID("Network",n9.nodeName);
NodeWithID n12 = new NodeWithID("Users",n9.nodeName);
NodeWithID n13 = new NodeWithID("Logging",n1.nodeName);
DefaultMutableTreeNode top = new DefaultMutableTreeNode(n0);
DefaultMutableTreeNode branch1 = new DefaultMutableTreeNode(n1);
top.add(branch1);
DefaultMutableTreeNode node1_b1 = new DefaultMutableTreeNode(n2);
DefaultMutableTreeNode n1_node1_b1 = new DefaultMutableTreeNode(n3);
DefaultMutableTreeNode n2_node1_b1 = new DefaultMutableTreeNode(n4);
DefaultMutableTreeNode n3_node1_b1 = new DefaultMutableTreeNode(n5);
DefaultMutableTreeNode n4_node1_b1 = new DefaultMutableTreeNode(n6);
node1_b1.add(n1_node1_b1);
node1_b1.add(n2_node1_b1);
node1_b1.add(n3_node1_b1);
node1_b1.add(n4_node1_b1);
DefaultMutableTreeNode node2_b1 = new DefaultMutableTreeNode(n7);
DefaultMutableTreeNode node3_b1 = new DefaultMutableTreeNode(n8);
DefaultMutableTreeNode node4_b1 = new DefaultMutableTreeNode(n9);
DefaultMutableTreeNode n1_node4_b1 = new DefaultMutableTreeNode(n10);
DefaultMutableTreeNode n2_node4_b1 = new DefaultMutableTreeNode(n11);
DefaultMutableTreeNode n3_node4_b1 = new DefaultMutableTreeNode(n12);
node4_b1.add(n1_node4_b1);
node4_b1.add(n2_node4_b1);
node4_b1.add(n3_node4_b1);
DefaultMutableTreeNode node5_b1 = new DefaultMutableTreeNode(n13);
branch1.add(node1_b1);
branch1.add(node2_b1);
branch1.add(node3_b1);
branch1.add(node4_b1);
branch1.add(node5_b1);
return top;
}
}
class NodeWithID
{
String nodeName;
String ID;
public NodeWithID(String nn,String parentName)
{
nodeName = nn;
ID = parentName+" - "+nodeName;
}
public String toString(){return nodeName;}
}
Michael
This works great. But now I can't seem to load the panels that I have created. Something is wrong with my NodeWithID class? Here is the one that I have.
import javax.swing.*;
public class NodeWithID
{
JPanel panel;
String nodeName;
String ID;
public NodeWithID(String nn,String parentName)
{
nodeName = nn;
ID = parentName + " - " + nodeName;
}
public NodeWithID(String nn, String parentName, JPanel p)
{
panel = p;
nodeName = nn;
ID = parentName + " - " + nodeName;
}
public String toString()
{return nodeName;}
}
and here is the one you gave me.class NodeWithID
{
String nodeName;
String ID;
public NodeWithID(String nn,String parentName)
{
nodeName = nn;
ID = parentName+" - "+nodeName;
}
public String toString()
{return nodeName;}
}
also here is my ConfigTree class that has the buildTree method
import javax.swing.*;
import javax.swing.tree.*;
class ConfigTree
{
public Object buildTree()
{
NodeWithID n0 = new NodeWithID("Devices","");
NodeWithID n1 = new NodeWithID("Module 1",n0.nodeName);
NodeWithID n2 = new NodeWithID("Status", n1.nodeName, new StatusPanel());
NodeWithID n3 = new NodeWithID("Device",n2.nodeName,new StatusDevicePanel());
NodeWithID n4 = new NodeWithID("Network",n2.nodeName, new StatusNetworkPanel());
NodeWithID n5 = new NodeWithID("Chassis",n2.nodeName, new StatusChassisPanel());
NodeWithID n6 = new NodeWithID("Resources",n2.nodeName, new StatusResourcesPanel());
NodeWithID n7 = new NodeWithID("Project Editor",n1.nodeName, new ProjectEditorPanel());
NodeWithID n8 = new NodeWithID("Project Manager",n1.nodeName, new ProjectManagerPanel());
NodeWithID n9 = new NodeWithID("Administration",n1.nodeName, new AdministrationPanel());
NodeWithID n10 = new NodeWithID("Device",n9.nodeName, new AdminDevicePanel());
NodeWithID n11 = new NodeWithID("Network",n9.nodeName, new AdminNetworkPanel());
NodeWithID n12 = new NodeWithID("Users",n9.nodeName, new AdminUsersPanel());
NodeWithID n13 = new NodeWithID("Logging",n1.nodeName, new LoggingPanel());
DefaultMutableTreeNode top =
//new DefaultMutableTreeNode("Devices");
new DefaultMutableTreeNode(n0);
DefaultMutableTreeNode branch1 =
//new DefaultMutableTreeNode("Module 1");
new DefaultMutableTreeNode(n1);
top.add(branch1);
/*******************************
* BRANCH 1*
******************************/
DefaultMutableTreeNode node1_b1
//=new DefaultMutableTreeNode("Status");
=new DefaultMutableTreeNode(n2);
DefaultMutableTreeNode n1_node1_b1 =
//new DefaultMutableTreeNode("Device");
new DefaultMutableTreeNode(n3);
DefaultMutableTreeNode n2_node1_b1 =
//new DefaultMutableTreeNode("Network");
new DefaultMutableTreeNode(n4);
DefaultMutableTreeNode n3_node1_b1 =
//new DefaultMutableTreeNode("Chassis");
new DefaultMutableTreeNode(n5);
DefaultMutableTreeNode n4_node1_b1 =
//new DefaultMutableTreeNode("Resources");
new DefaultMutableTreeNode(n6);
node1_b1.add(n1_node1_b1);
node1_b1.add(n2_node1_b1);
node1_b1.add(n3_node1_b1);
node1_b1.add(n4_node1_b1);
DefaultMutableTreeNode node2_b1 =
//new DefaultMutableTreeNode("Project Editor");
new DefaultMutableTreeNode(n7);
DefaultMutableTreeNode node3_b1 =
//new DefaultMutableTreeNode("Project Manager");
new DefaultMutableTreeNode(n8);
DefaultMutableTreeNode node4_b1 =
//new DefaultMutableTreeNode("Administration");
new DefaultMutableTreeNode(n9);
DefaultMutableTreeNode n1_node4_b1 =
//new DefaultMutableTreeNode("Device");
new DefaultMutableTreeNode(n10);
DefaultMutableTreeNode n2_node4_b1 =
//new DefaultMutableTreeNode("Network");
new DefaultMutableTreeNode(n11);
DefaultMutableTreeNode n3_node4_b1 =
//new DefaultMutableTreeNode("Users");
new DefaultMutableTreeNode(n12);
node4_b1.add(n1_node4_b1);
node4_b1.add(n2_node4_b1);
node4_b1.add(n3_node4_b1);
DefaultMutableTreeNode node5_b1 =
//new DefaultMutableTreeNode("Logging");
new DefaultMutableTreeNode(n13);
branch1.add(node1_b1);
branch1.add(node2_b1);
branch1.add(node3_b1);
branch1.add(node4_b1);
branch1.add(node5_b1);
return top;
}
}
My panels are not loading?
if you need anything else let me know.
orozcom
looking at the code, you want to have panels for the branches Status and
Administration, but not for Module 1 or Devices (root).
I've added back the 'blank panel' for when the program starts (have to show
something - perhaps a good spot for a logo?)
here's the full code again (easier to do this than just posting changes/instructions)
about the only changes you would need in your real code is to change
NodeWithID n2 = new NodeWithID("Status",n1.nodeName,new JPanel());
to
NodeWithID n2 = new NodeWithID("Status", n1.nodeName, new StatusPanel());
etc, for all with a panel
and get rid of the nodePanel.setBorder(...) - only there to distinguish between panels
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import javax.swing.event.*;
class Testing extends JFrame
{
CardLayout cl = new CardLayout();
JPanel panel = new JPanel(cl);
public Testing()
{
JPanel blankPanel = new JPanel();
blankPanel.setBorder(BorderFactory.createTitledBorder("Blank Panel"));
panel.add(blankPanel,"0");
setSize(600,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(new GridLayout(1,2));
ConfigTree test = new ConfigTree();
DefaultMutableTreeNode mainTree = (DefaultMutableTreeNode)test.buildTree();
JTree tree = new JTree(mainTree);
getContentPane().add(new JScrollPane(tree));
loadCardPanels((DefaultMutableTreeNode)((DefaultTreeModel)tree.getModel()).getRoot());
getContentPane().add(panel);
tree.addTreeSelectionListener(new TreeSelectionListener(){
public void valueChanged(TreeSelectionEvent tse){
NodeWithID node =(NodeWithID)((DefaultMutableTreeNode)((TreePath)tse.getPath())
.getLastPathComponent()).getUserObject();
if(node.nodePanel != null)
{
String cardLayoutID = node.ID;
cl.show(panel,cardLayoutID);
}
}
});
cl.show(panel,"0");
}
public void loadCardPanels(DefaultMutableTreeNode dmtn)
{
for(int x = 0; x < dmtn.getChildCount(); x++)
{
if(((DefaultMutableTreeNode)dmtn.getChildAt(x)).isLeaf() == false)
{
loadCardPanels((DefaultMutableTreeNode)dmtn.getChildAt(x));
}
NodeWithID node = (NodeWithID)((DefaultMutableTreeNode)dmtn.getChildAt(x)).getUserObject();
if(node.nodePanel != null)
{
String cardLayoutID = node.ID;
panel.add(cardLayoutID,node.nodePanel);
}
}
}
public static void main(String[] args){new Testing().setVisible(true);}
}
class ConfigTree
{
public Object buildTree()
{
NodeWithID n0 = new NodeWithID("Devices","");
NodeWithID n1 = new NodeWithID("Module 1",n0.nodeName);
NodeWithID n2 = new NodeWithID("Status",n1.nodeName,new JPanel());
NodeWithID n3 = new NodeWithID("Device",n2.nodeName,new JPanel());
NodeWithID n4 = new NodeWithID("Network",n2.nodeName,new JPanel());
NodeWithID n5 = new NodeWithID("Chassis",n2.nodeName,new JPanel());
NodeWithID n6 = new NodeWithID("Resources",n2.nodeName,new JPanel());
NodeWithID n7 = new NodeWithID("Project Editor",n1.nodeName,new JPanel());
NodeWithID n8 = new NodeWithID("Project Manager",n1.nodeName,new JPanel());
NodeWithID n9 = new NodeWithID("Administration",n1.nodeName,new JPanel());
NodeWithID n10 = new NodeWithID("Device",n9.nodeName,new JPanel());
NodeWithID n11 = new NodeWithID("Network",n9.nodeName,new JPanel());
NodeWithID n12 = new NodeWithID("Users",n9.nodeName,new JPanel());
NodeWithID n13 = new NodeWithID("Logging",n1.nodeName,new JPanel());
DefaultMutableTreeNode top = new DefaultMutableTreeNode(n0);
DefaultMutableTreeNode branch1 = new DefaultMutableTreeNode(n1);
top.add(branch1);
DefaultMutableTreeNode node1_b1 = new DefaultMutableTreeNode(n2);
DefaultMutableTreeNode n1_node1_b1 = new DefaultMutableTreeNode(n3);
DefaultMutableTreeNode n2_node1_b1 = new DefaultMutableTreeNode(n4);
DefaultMutableTreeNode n3_node1_b1 = new DefaultMutableTreeNode(n5);
DefaultMutableTreeNode n4_node1_b1 = new DefaultMutableTreeNode(n6);
node1_b1.add(n1_node1_b1);
node1_b1.add(n2_node1_b1);
node1_b1.add(n3_node1_b1);
node1_b1.add(n4_node1_b1);
DefaultMutableTreeNode node2_b1 = new DefaultMutableTreeNode(n7);
DefaultMutableTreeNode node3_b1 = new DefaultMutableTreeNode(n8);
DefaultMutableTreeNode node4_b1 = new DefaultMutableTreeNode(n9);
DefaultMutableTreeNode n1_node4_b1 = new DefaultMutableTreeNode(n10);
DefaultMutableTreeNode n2_node4_b1 = new DefaultMutableTreeNode(n11);
DefaultMutableTreeNode n3_node4_b1 = new DefaultMutableTreeNode(n12);
node4_b1.add(n1_node4_b1);
node4_b1.add(n2_node4_b1);
node4_b1.add(n3_node4_b1);
DefaultMutableTreeNode node5_b1 = new DefaultMutableTreeNode(n13);
branch1.add(node1_b1);
branch1.add(node2_b1);
branch1.add(node3_b1);
branch1.add(node4_b1);
branch1.add(node5_b1);
return top;
}
}
class NodeWithID
{
String nodeName;
String ID;
JPanel nodePanel;
public NodeWithID(String nn,String parentName)
{
nodeName = nn;
ID = parentName+" - "+nodeName;
}
public NodeWithID(String nn,String parentName, JPanel p)
{
nodeName = nn;
ID = parentName+" - "+nodeName;
nodePanel = p;
nodePanel.setBorder(BorderFactory.createTitledBorder(ID + " Panel"));
}
public String toString(){return nodeName;}
}
Michael,
You are the best! Got it running. Yeah I like your idea about a logo. I think I will add that in. I think I will also add in some athentication. Not sure how I am going to do that. You have been most helpfull. Do you mind if I email you? Let me know if there is anything I can do for you.
orozcom
> Do you mind if I email you?
email address in my profile, but it gets flooded with spam so I don't regularly check it.
Include 'java' in the subject and I'll know it's not about adding inches, buying viagra,
hot pics, laptops etc etc - so a good chance I'll not just delete it.
Hey, I would like to put an icon on each of the nodes. The thing is I would like to put a different icon on each one.
So for status I would like to have a status icon, network network icon, etc... one for each of the folders too.
can you help me out with this. I will send you what I have if you need. I am running this under Netbeans 5.5
not exactly sure this is what you're after - so only modified for 2 of the nodes.
if it is OK, do the same for all the nodes you want icons for.
test3.gif and test4.gif are actually windows' open.gif and save.gif (small icons)
if you eventually intend jarring this - you'll need to change the images to a getResource()
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import javax.swing.event.*;
class Testing extends JFrame
{
CardLayout cl = new CardLayout();
JPanel panel = new JPanel(cl);
public Testing()
{
JPanel blankPanel = new JPanel();
blankPanel.setBorder(BorderFactory.createTitledBorder("Blank Panel"));
panel.add(blankPanel,"0");
setSize(600,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(new GridLayout(1,2));
ConfigTree test = new ConfigTree();
DefaultMutableTreeNode mainTree = (DefaultMutableTreeNode)test.buildTree();
JTree tree = new JTree(mainTree);
tree.setCellRenderer(new DefaultTreeCellRenderer(){
public Component getTreeCellRendererComponent(JTree tree,Object value,
boolean sel,boolean expanded,boolean leaf,int row,boolean hasFocus){
JLabel lbl = (JLabel)super.getTreeCellRendererComponent(tree,value,sel,expanded,leaf,row,hasFocus);
NodeWithID node = (NodeWithID)((DefaultMutableTreeNode)value).getUserObject();
if(node.icon != null)lbl.setIcon(node.icon);
return lbl;
}
});
getContentPane().add(new JScrollPane(tree));
loadCardPanels((DefaultMutableTreeNode)((DefaultTreeModel)tree.getModel()).getRoot());
getContentPane().add(panel);
tree.addTreeSelectionListener(new TreeSelectionListener(){
public void valueChanged(TreeSelectionEvent tse){
NodeWithID node =(NodeWithID)((DefaultMutableTreeNode)((TreePath)tse.getPath())
.getLastPathComponent()).getUserObject();
if(node.nodePanel != null)
{
String cardLayoutID = node.ID;
cl.show(panel,cardLayoutID);
}
}
});
cl.show(panel,"0");
}
public void loadCardPanels(DefaultMutableTreeNode dmtn)
{
for(int x = 0; x < dmtn.getChildCount(); x++)
{
if(((DefaultMutableTreeNode)dmtn.getChildAt(x)).isLeaf() == false)
{
loadCardPanels((DefaultMutableTreeNode)dmtn.getChildAt(x));
}
NodeWithID node = (NodeWithID)((DefaultMutableTreeNode)dmtn.getChildAt(x)).getUserObject();
if(node.nodePanel != null)
{
String cardLayoutID = node.ID;
panel.add(cardLayoutID,node.nodePanel);
}
}
}
public static void main(String[] args){new Testing().setVisible(true);}
}
class ConfigTree
{
public Object buildTree()
{
NodeWithID n0 = new NodeWithID("Devices","");
NodeWithID n1 = new NodeWithID("Module 1",n0.nodeName);
NodeWithID n2 = new NodeWithID("Status",n1.nodeName,new JPanel(),new ImageIcon("test3.gif"));
NodeWithID n3 = new NodeWithID("Device",n2.nodeName,new JPanel());
NodeWithID n4 = new NodeWithID("Network",n2.nodeName,new JPanel(),new ImageIcon("test4.gif"));
NodeWithID n5 = new NodeWithID("Chassis",n2.nodeName,new JPanel());
NodeWithID n6 = new NodeWithID("Resources",n2.nodeName,new JPanel());
NodeWithID n7 = new NodeWithID("Project Editor",n1.nodeName,new JPanel());
NodeWithID n8 = new NodeWithID("Project Manager",n1.nodeName,new JPanel());
NodeWithID n9 = new NodeWithID("Administration",n1.nodeName,new JPanel());
NodeWithID n10 = new NodeWithID("Device",n9.nodeName,new JPanel());
NodeWithID n11 = new NodeWithID("Network",n9.nodeName,new JPanel());
NodeWithID n12 = new NodeWithID("Users",n9.nodeName,new JPanel());
NodeWithID n13 = new NodeWithID("Logging",n1.nodeName,new JPanel());
DefaultMutableTreeNode top = new DefaultMutableTreeNode(n0);
DefaultMutableTreeNode branch1 = new DefaultMutableTreeNode(n1);
top.add(branch1);
DefaultMutableTreeNode node1_b1 = new DefaultMutableTreeNode(n2);
DefaultMutableTreeNode n1_node1_b1 = new DefaultMutableTreeNode(n3);
DefaultMutableTreeNode n2_node1_b1 = new DefaultMutableTreeNode(n4);
DefaultMutableTreeNode n3_node1_b1 = new DefaultMutableTreeNode(n5);
DefaultMutableTreeNode n4_node1_b1 = new DefaultMutableTreeNode(n6);
node1_b1.add(n1_node1_b1);
node1_b1.add(n2_node1_b1);
node1_b1.add(n3_node1_b1);
node1_b1.add(n4_node1_b1);
DefaultMutableTreeNode node2_b1 = new DefaultMutableTreeNode(n7);
DefaultMutableTreeNode node3_b1 = new DefaultMutableTreeNode(n8);
DefaultMutableTreeNode node4_b1 = new DefaultMutableTreeNode(n9);
DefaultMutableTreeNode n1_node4_b1 = new DefaultMutableTreeNode(n10);
DefaultMutableTreeNode n2_node4_b1 = new DefaultMutableTreeNode(n11);
DefaultMutableTreeNode n3_node4_b1 = new DefaultMutableTreeNode(n12);
node4_b1.add(n1_node4_b1);
node4_b1.add(n2_node4_b1);
node4_b1.add(n3_node4_b1);
DefaultMutableTreeNode node5_b1 = new DefaultMutableTreeNode(n13);
branch1.add(node1_b1);
branch1.add(node2_b1);
branch1.add(node3_b1);
branch1.add(node4_b1);
branch1.add(node5_b1);
return top;
}
}
class NodeWithID
{
String nodeName;
String ID;
JPanel nodePanel;
ImageIcon icon;
public NodeWithID(String nn,String parentName)
{
nodeName = nn;
ID = parentName+" - "+nodeName;
}
public NodeWithID(String nn,String parentName,JPanel p)
{
this(nn,parentName);
nodePanel = p;
nodePanel.setBorder(BorderFactory.createTitledBorder(ID + " Panel"));
}
public NodeWithID(String nn,String parentName,JPanel p, ImageIcon i)
{
this(nn,parentName,p);
icon = i;
}
public String toString(){return nodeName;}
}
This worked out perfectly. You are awesome!
Next I will be needing some help in reading, editing, writing, etc... xml
How are you with that stuff?
I hope you are pretty good. You have saved me alot of headaches already. Thanks again. If you don't mind I will send you my project in a zip file, and you can see what I am doing.
orozcom
well, thanks anyway. You have been very helpfull. I couldn't have done it without you. I now know who to come too for Swing help.
One other thing. I need to create sort of a combo box. The JTree structure is almost exactly the same as in the main class, but this one is in another JPanel. I need it to look like this.
|- |
| | Drop Down Menu ||
|- |
|
|||
|||
|||
| JTree|XML Properties |
|||
|||
|||
|||
|||
The Drop Down Menu will have a list of xml file that will be loaded when selected. It will first create a JTree, which in turn will display the properties of the XML file. I thought your program may work out pretty good for this. I have a few classes that may work out as well. Here they are.
SaxParserExample.javaimport java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import misc.*;
public class SAXParserExample extends DefaultHandler
{
List myTemplate;
private String tempVal;
//to maintain context
private Template tmpTemplate;
public SAXParserExample()
{
myTemplate = new ArrayList();
}
public void runExample()
{
parseDocument();
printData();
}
private void parseDocument()
{
//get a factory
SAXParserFactory spf = SAXParserFactory.newInstance();
try
{
//get a new instance of parser
SAXParser sp = spf.newSAXParser();
//parse the file and also register this class for call backs
sp.parse("XML/employees.xml", this);
}
catch(SAXException se)
{
se.printStackTrace();
}
catch(ParserConfigurationException pce)
{
pce.printStackTrace();
}
catch (IOException ie)
{
ie.printStackTrace();
}
}
/**
* Iterate through the list and print
* the contents
*/
private void printData()
{
System.out.println("No of Employees '" + myTemplate.size() + "'.");
Iterator it = myTemplate.iterator();
while(it.hasNext())
{
System.out.println(it.next().toString());
}
}
//Event Handlers
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
{
//reset
tempVal = "";
if(qName.equalsIgnoreCase("Employee"))
{
//create a new instance of employee
tmpTemplate = new Template();
tmpTemplate.setType(attributes.getValue("type"));
}
}
public void characters(char[] ch, int start, int length) throws SAXException
{
tempVal = new String(ch,start,length);
}
public void endElement(String uri, String localName, String qName) throws SAXException
{
if(qName.equalsIgnoreCase("Employee"))
{
//add it to the list
myTemplate.add(tmpTemplate);
}
else if (qName.equalsIgnoreCase("Name"))
{
tmpTemplate.setName(tempVal);
}
else if (qName.equalsIgnoreCase("Id"))
{
tmpTemplate.setId(Integer.parseInt(tempVal));
}
else if (qName.equalsIgnoreCase("Age"))
{
tmpTemplate.setAge(Integer.parseInt(tempVal));
}
}
public static void main(String[] args)
{
SAXParserExample spe = new SAXParserExample();
spe.runExample();
}
}
// which works from this class
public class Template
{
private String name;
private int age;
private int id;
private String type;
public Template()
{ }
public Template(String name, int id, int age,String type)
{
this.name = name;
this.age = age;
this.id = id;
this.type = type;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getType()
{
return type;
}
public void setType(String type)
{
this.type = type;
}
public String toString()
{
StringBuffer sb = new StringBuffer();
sb.append("Employee Details - ");
sb.append("Name:" + getName());
sb.append(", ");
sb.append("Type:" + getType());
sb.append(", ");
sb.append("Id:" + getId());
sb.append(", ");
sb.append("Age:" + getAge());
sb.append(".");
return sb.toString();
}
}
// and here is the xml file that it is working off of.
<?xml version="1.0" encoding="UTF-8" ?>
<Personnel>
<Employee type="permanent">
<Name>Seagull</Name>
<Id>3674</Id>
<Age>34</Age>
</Employee>
<Employee type="contract">
<Name>Robin</Name>
<Id>3675</Id>
<Age>25</Age>
</Employee>
<Employee type="permanent">
<Name>Crow</Name>
<Id>3676</Id>
<Age>28</Age>
</Employee>
</Personnel>
I would like to display the XML somehow, maybe put it into some textfields. I would then like to edit a field like say "<Id>3676</Id> to a different #, and save the xml file back where it was before. Do you know of how I could do this?
If you don't I understand you have been more than help full.
one day I might get around to looking into xml, but at present I know zip about
parsing/saving xml stuff - might be time to start a new thread with xml in the subject line.
just looking at your description and pic, the comboBox has the xml file names,
the JTree contains the comboBox selection's file data i.e. if the comboBox selection
changes, so does the tree's model. The leafs would be your Template class,
similar to the NodeWithID - when selected the data would populate the various
textfields in the adjacent panel.
is there a specific need for the comboBox - might be a lot easier having the
fileNames (abbreviated or description of) as the first branches of the tree.