JToolBar from XML
E are trying to generate a JToolBar from XML Files. Unfortunatel it's not working and we have no clue why not. Could someone take a look at the code and please give us a hint a what we are doing wrong? We do the same thing with a JMenu and that is working.
Example XML file:
<?xml version="1.0"?>
<jmenu caption="Wijzigen">
<!--<jmenu caption="Exit">-->
<jmenuitem caption="Therapeut Wijzigen" action-class="actions.Therapeut.TherapeutWijzigenAction" imageurl="images/icon/therapeutwijzigen.gif">
</jmenu>
XMLParser file:
package XML;
import java.io.IOException;
import java.net.URL;
import javax.swing.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import stresslabo.Main;
publicclass XMLBouwer
{
publicstaticvoid buildToolbarFromXML(JToolBar bar, String file){
Class clazz = stresslabo.HoofdForm.class;
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(clazz.getResourceAsStream(file));
treeWalker(doc.getFirstChild(), bar);
}
catch(ParserConfigurationException e)
{
e.printStackTrace();
}
catch(SAXException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
publicstaticvoid buildJMenuFromXML(JMenuBar bar, String file)
{
Class clazz = stresslabo.HoofdForm.class;
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(clazz.getResourceAsStream(file));
treeWalk(doc.getFirstChild(), bar);
}
catch(ParserConfigurationException e)
{
e.printStackTrace();
}
catch(SAXException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
//for JMENU
privatestaticvoid treeWalk(Node node, JComponent component)
{
String name = node.getNodeName();
if(node.hasChildNodes())
{
if(name.equals("jmenu"))
{
Element e = (Element)node;
JMenu mnu =new JMenu(e.getAttribute("caption"));
NodeList list = node.getChildNodes();
component.add(mnu);
int len = list.getLength();
for(int i = 0; i < len; i++)
{
treeWalk(list.item(i), ((JComponent) (mnu)));
}
}
}else
if(name.equals("jmenuitem"))
{
Element e = (Element)node;
JMenuItem item =new JMenuItem();
try
{
Action action = (Action)Thread.currentThread().getContextClassLoader().loadClass(e.getAttribute("action-class")).newInstance();
item.setAction(action);
item.setText(e.getAttribute("caption"));
}
catch(InstantiationException e1)
{
e1.printStackTrace();
}
catch(IllegalAccessException e1)
{
e1.printStackTrace();
}
catch(ClassNotFoundException e1)
{
e1.printStackTrace();
}
component.add(item);
}else
if(name.equals("separator"))
{
component.add(new JSeparator());
}
}
//for JToolbar
privatestaticvoid treeWalker(Node node, JComponent component)
{
String name = node.getNodeName();
if(node.hasChildNodes())
{
if(name.equals("jmenu"))
{
Element e = (Element)node;
NodeList list = node.getChildNodes();
int len = list.getLength();
for(int i = 0; i < len; i++)
{
treeWalker(list.item(i), ((JComponent) (component)));
}
}
}else
if(name.equals("jmenuitem"))
{
Element e = (Element)node;
JButton item =new JButton();
try
{
Action action = (Action)Thread.currentThread().getContextClassLoader().loadClass(e.getAttribute("action-class")).newInstance();
item.setAction(action);
item.setText(e.getAttribute("caption"));
URL iconURL = Thread.currentThread().getContextClassLoader().getResource(e.getAttribute("imageurl"));
ImageIcon icon =new ImageIcon(iconURL);
item.setIcon(icon);
component.add(item);
}
catch(InstantiationException e1)
{
e1.printStackTrace();
}
catch(IllegalAccessException e1)
{
e1.printStackTrace();
}
catch(ClassNotFoundException e1)
{
e1.printStackTrace();
}
component.add(item);
}else
if(name.equals("separator"))
{
component.add(new JSeparator());
}
}
}
Form where Toolbar is generated:
/*
* HoofdForm.java
*
* Created on 22 februari 2007, 13:29
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package stresslabo;
import ejb.Arts;
import ejb.HoofdArts;
import ejb.Persoon;
import ejb.SuperFacadeBean;
import ejb.Therapeut;
import java.awt.Container;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import java.awt.*;
import XML.XMLBouwer;
import login.LoginGegevens;
/**
*
* @author 0301981
*/
publicclass HoofdFormextends JFrame{
private JMenuBar bar =new JMenuBar();
private JToolBar toolbar =new JToolBar();
private JPanel mainPanel=new JPanel();
private JPanel sidePanel=new JPanel();
private JSplitPane splitPane=new JSplitPane();
publicstatic JDesktopPane dskp =new JDesktopPane();
Container container;
private Persoon p;
privatestatic LoginGegevens geg=null;
/** Creates a new instance of HoofdForm */
public HoofdForm(){
}
public HoofdForm(Persoon p)
{
super ("Stresslabo");
this.p=p;
this.init();
}
privatevoid init(){
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
container = getContentPane();
container.setBackground(Color.white);
dskp.setBackground(Color.gray);
super.setContentPane(dskp);
setSize(1024,768);
if(p!=null)
if(pinstanceof Arts)
{
XMLBouwer.buildJMenuFromXML(bar,"xml/jmenuFile.xml");
XMLBouwer.buildJMenuFromXML(bar,"xml/ArtsWijzigen.xml");
XMLBouwer.buildJMenuFromXML(bar,"xml/HoofdArtsConsultaties.xml");
setJMenuBar(bar);
}
elseif(pinstanceof Therapeut)
{
XMLBouwer.buildJMenuFromXML(bar,"xml/jmenuFile.xml");
XMLBouwer.buildJMenuFromXML(bar,"xml/TherapeutWijzigen.xml");
setJMenuBar(bar);
}
elseif(pinstanceof HoofdArts)
{
XMLBouwer.buildJMenuFromXML(bar,"xml/jmenuFile.xml");
XMLBouwer.buildJMenuFromXML(bar,"xml/HoofdArtsToevoegen.xml");
XMLBouwer.buildJMenuFromXML(bar,"xml/HoofdArtsWijzigen.xml");
//toolbar.add(new JButton("dsqmfjqsdfjqmdsjf"));
XMLBouwer.buildToolbarFromXML(toolbar,"xml/jmenuFile.xml");
XMLBouwer.buildToolbarFromXML(toolbar,"xml/HoofdArtsToevoegen.xml");
XMLBouwer.buildToolbarFromXML(toolbar,"xml/HoofdArtsWijzigen.xml");
XMLBouwer.buildToolbarFromXML(toolbar,"xml/HoofdArtsConsultaties.xml");
setJMenuBar(bar);
toolbar.setVisible(true);
add(toolbar, BorderLayout.PAGE_START);
}
super.setVisible(true);
}
}

