Skip to main content

Troubles with JTree and JList

1 reply [Last post]
madhatress
Offline
Joined: 2006-02-17

Hi All,

I am trying to implement an ID Manger for use with XMetal 3.1/4.5. The basic idea is to have the XML document displayed in a JTree so that the user can navigate to the node they want to link to, and then insert the
link into the document. There is also a combo box where they can choose a particular element, and a list of all the occurrences of this element in the documents appears in a JList below it.

The problem I am having is this: I cannot get the JTree to display the document structure, or the JList to display the list of elements! All it displays is the default text (for the JTree its colors, sports, etc and the JList is blank). I know that the tree is being build correctly and that the model is being passed to the JTree. I also know that the array of elements is being passed to the JList correctly. In both cases I test this by retrieving the models from the components and having xmetal display an alert (for the JTree it spit back the root element of the XML document, for the JList, it spit back the text of the first array item).

I have tried several things, including removing the tree from the scroll pane, updating the model then adding it back to the pane. I also tried to get and set the tree model, but nothing is changing the display.

My code is shown below. Can anyone help??!

TIA

<br />
import java.io.*;<br />
import java.util.*;<br />
import javax.swing.*;<br />
import com.sq.types.*;<br />
import com.sq.xmetal.api.*;<br />
import com.sq.xmetal.types.*;<br />
import org.w3c.dom.*;<br />
import javax.swing.tree.*;<br />
import java.awt.event.*;<br />
import javax.swing.event.*;<br />
import javax.swing.UIManager;<br />
import javax.swing.JComboBox;<br />
import javax.swing.JList;<br />
import javax.swing.JTree;<br />
import Lib.XMLDocTree;</p>
<p>public class IDManagerDialog extends javax.swing.JFrame implements ActionListener, FocusListener, ItemListener {</p>
<p>    Application xmetal = new Application();<br />
    _Document doc = xmetal.getActiveDocument();<br />
    Selection startsel = xmetal.getSelection();</p>
<p>    /** Creates new form IDManagerDialog */<br />
    public IDManagerDialog() {</p>
<p>        initComponents();<br />
        UpdateElementList();<br />
        Node rootelem = doc.getDocumentElement();<br />
        jScrollPane1.remove(TreeElements);<br />
        TreeElements = new XMLDocTree(rootelem);<br />
        TreeModel tm = TreeElements.getModel();<br />
//        xmetal.Alert(tm.getRoot().toString());<br />
        DefaultTreeModel dtm = (DefaultTreeModel)tm;<br />
        dtm.reload();<br />
        TreeElements.setModel((TreeModel)dtm);<br />
        TreeElements.revalidate();<br />
        jScrollPane1.add(TreeElements);<br />
        jScrollPane1.validate();<br />
        TreeElements.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);<br />
        TreeElements.setRootVisible(true);<br />
        TreeElements.setShowsRootHandles(true);<br />
        TreeElements.expandRow(1);<br />
        btnLink.addActionListener(this);<br />
        btnBack.addActionListener(this);<br />
        btnRef.addActionListener(this);<br />
        btnGoToID.addActionListener(this);<br />
        btnExit.addActionListener(this);<br />
        lstDispElems.addFocusListener(this);<br />
//        cboDocElems.addActionListener(this);<br />
        cboDocElems.addItemListener(this);<br />
        TreeElements.addFocusListener(this);<br />
    }</p>
<p>    /** This method is called from within the constructor to<br />
     * initialize the form.<br />
     * WARNING: Do NOT modify this code. The content of this method is<br />
     * always regenerated by the Form Editor.<br />
     */<br />
    private void initComponents() {<br />
        java.awt.GridBagConstraints gridBagConstraints;</p>
<p>        jPanel1 = new javax.swing.JPanel();<br />
        btnExit = new javax.swing.JButton();<br />
        btnGoToID = new javax.swing.JButton();<br />
        btnLink = new javax.swing.JButton();<br />
        btnRef = new javax.swing.JButton();<br />
        btnBack = new javax.swing.JButton();<br />
        jSplitPane1 = new javax.swing.JSplitPane();<br />
        jScrollPane1 = new javax.swing.JScrollPane();<br />
        TreeElements = new javax.swing.JTree();<br />
        jPanel2 = new javax.swing.JPanel();<br />
        cboDocElems = new javax.swing.JComboBox();<br />
        jScrollPane2 = new javax.swing.JScrollPane();<br />
        lstDispElems = new javax.swing.JList();</p>
<p>        getContentPane().setLayout(new java.awt.GridBagLayout());</p>
<p>        setTitle("ID Manager");<br />
        setFont(new java.awt.Font("Arial", 0, 12));<br />
        setName("Frame");<br />
        addWindowListener(new java.awt.event.WindowAdapter() {<br />
            public void windowClosing(java.awt.event.WindowEvent evt) {<br />
                exitForm(evt);<br />
            }<br />
        });</p>
<p>        jPanel1.setMaximumSize(new java.awt.Dimension(440, 32767));<br />
        jPanel1.setPreferredSize(new java.awt.Dimension(440, 35));<br />
        btnExit.setFont(new java.awt.Font("Arial", 1, 13));<br />
        btnExit.setText("Exit");<br />
        btnExit.setToolTipText("Close ID Manager");<br />
        btnExit.addMouseListener(new java.awt.event.MouseAdapter() {<br />
            public void mouseClicked(java.awt.event.MouseEvent evt) {<br />
                btnExitMouseClicked(evt);<br />
            }<br />
        });</p>
<p>        jPanel1.add(btnExit);</p>
<p>        btnGoToID.setFont(new java.awt.Font("Arial", 1, 13));<br />
        btnGoToID.setText("Go To ID");<br />
        btnGoToID.setToolTipText("Go to Element with ID");<br />
        jPanel1.add(btnGoToID);</p>
<p>        btnLink.setFont(new java.awt.Font("Arial", 1, 13));<br />
        btnLink.setText("Link");<br />
        btnLink.setToolTipText("Create link to selected element");<br />
        jPanel1.add(btnLink);</p>
<p>        btnRef.setFont(new java.awt.Font("Arial", 1, 13));<br />
        btnRef.setText("References");<br />
        btnRef.setToolTipText("Show All Elements that Reference this ID");<br />
        jPanel1.add(btnRef);</p>
<p>        btnBack.setFont(new java.awt.Font("Arial", 1, 13));<br />
        btnBack.setText("Prev Pos");<br />
        btnBack.setToolTipText("Return to Previous Position in Document");<br />
        jPanel1.add(btnBack);</p>
<p>        gridBagConstraints = new java.awt.GridBagConstraints();<br />
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;<br />
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;<br />
        gridBagConstraints.weightx = 1.5;<br />
        gridBagConstraints.weighty = 2.0;<br />
        getContentPane().add(jPanel1, gridBagConstraints);</p>
<p>        jSplitPane1.setDividerLocation(-3);<br />
        jScrollPane1.setAutoscrolls(true);<br />
        jScrollPane1.setMinimumSize(new java.awt.Dimension(100, 100));<br />
        jScrollPane1.setPreferredSize(new java.awt.Dimension(200, 300));<br />
        TreeElements.setAutoscrolls(true);<br />
        TreeElements.setLargeModel(true);<br />
        TreeElements.setShowsRootHandles(true);<br />
        jScrollPane1.setViewportView(TreeElements);</p>
<p>        jSplitPane1.setLeftComponent(jScrollPane1);</p>
<p>        jPanel2.setLayout(new java.awt.BorderLayout(5, 5));</p>
<p>        jPanel2.setMaximumSize(new java.awt.Dimension(440, 32767));<br />
        jPanel2.setPreferredSize(new java.awt.Dimension(220, 300));<br />
        cboDocElems.setMinimumSize(new java.awt.Dimension(100, 25));<br />
        cboDocElems.setPreferredSize(new java.awt.Dimension(200, 25));<br />
        cboDocElems.addMouseListener(new java.awt.event.MouseAdapter() {<br />
            public void mouseClicked(java.awt.event.MouseEvent evt) {<br />
                cboDocElemsMouseClicked(evt);<br />
            }<br />
        });</p>
<p>        jPanel2.add(cboDocElems, java.awt.BorderLayout.NORTH);</p>
<p>        jScrollPane2.setPreferredSize(new java.awt.Dimension(200, 200));<br />
        jScrollPane2.setMinimumSize(new java.awt.Dimension(150, 150));<br />
        lstDispElems.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.LOWERED, null, null, null, java.awt.Color.black));<br />
        lstDispElems.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);<br />
        lstDispElems.setAlignmentX(4.0F);<br />
        lstDispElems.setAlignmentY(2.0F);<br />
        lstDispElems.setMaximumSize(new java.awt.Dimension(400, 400));<br />
        lstDispElems.setPreferredSize(new java.awt.Dimension(200, 200));<br />
        jScrollPane2.setViewportView(lstDispElems);</p>
<p>        jPanel2.add(jScrollPane2, java.awt.BorderLayout.CENTER);</p>
<p>        jSplitPane1.setRightComponent(jPanel2);</p>
<p>        gridBagConstraints = new java.awt.GridBagConstraints();<br />
        gridBagConstraints.gridx = 0;<br />
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;<br />
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;<br />
        getContentPane().add(jSplitPane1, gridBagConstraints);</p>
<p>        pack();<br />
    }                        </p>
<p>    private void cboDocElemsMouseClicked(java.awt.event.MouseEvent evt) {<br />
        // TODO add your handling code here:<br />
    }                                        </p>
<p>    private void btnExitMouseClicked(java.awt.event.MouseEvent evt) {<br />
        // TODO add your handling code here:<br />
       System.exit(0);<br />
    }                                    </p>
<p>    /** Exit the Application */<br />
    private void exitForm(java.awt.event.WindowEvent evt) {<br />
        System.exit(0);<br />
    }                         </p>
<p>    void UpdateElementList() {<br />
        //Application xmetal = new Application();<br />
        _Document doc = xmetal.getActiveDocument();<br />
        DOMDocumentType docType = (DOMDocumentType) doc.getDoctype(); // get the document type<br />
        SQVariant[] types = docType.getElementTypes().getVariantArray(); // ask the document type for its list of elements</p>
<p>        cboDocElems.removeAllItems();<br />
        for (int i=0; i 0){<br />
//                    xmetal.Alert(Integer.toString(selnodes.getLength()));<br />
                    docnodes = new Node[selnodes.getLength()];<br />
                    for(int i=0; i < selnodes.getLength(); i++){<br />
                        docnodes[i] = selnodes.item(i);<br />
                    }<br />
                    lstDispElems = new JList(docnodes);<br />
                    xmetal.Alert(lstDispElems.getModel().getElementAt(0).toString());<br />
                    lstDispElems.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);<br />
                    lstDispElems.revalidate();<br />
                    lstDispElems.repaint();</p>
<p>                }<br />
            }<br />
        }<br />
    }</p>
<p>    public void focusGained(FocusEvent fe) {<br />
        if(fe.getComponent() != null){<br />
            if(fe.getComponent().getName() == "lstDispElems"){<br />
                TreeElements.clearSelection();<br />
            }<br />
            if(fe.getComponent().getName() == "TreeElements"){<br />
                lstDispElems.clearSelection();<br />
            }<br />
        }<br />
    }</p>
<p>    public void focusLost(FocusEvent fe) {<br />
    }</p>
<p>    // Variables declaration - do not modify<br />
    private javax.swing.JTree TreeElements;<br />
    private javax.swing.JButton btnBack;<br />
    private javax.swing.JButton btnExit;<br />
    private javax.swing.JButton btnGoToID;<br />
    private javax.swing.JButton btnLink;<br />
    private javax.swing.JButton btnRef;<br />
    private javax.swing.JComboBox cboDocElems;<br />
    private javax.swing.JPanel jPanel1;<br />
    private javax.swing.JPanel jPanel2;<br />
    private javax.swing.JScrollPane jScrollPane1;<br />
    private javax.swing.JScrollPane jScrollPane2;<br />
    private javax.swing.JSplitPane jSplitPane1;<br />
    private javax.swing.JList lstDispElems;<br />
    // End of variables declaration                   </p>
<p>}</p>
<p>/*<br />
 * XMLDocTree.java<br />
 *<br />
 * Created on January 26, 2005, 10:07 AM<br />
 */</p>
<p>package Lib;</p>
<p>import javax.swing.*;<br />
import javax.swing.tree.*;<br />
import com.sq.xmetal.api.*;<br />
import com.sq.xmetal.types.*;<br />
import org.w3c.dom.*;</p>
<p>public class XMLDocTree extends JTree {</p>
<p>        public XMLDocTree(Node docroot){<br />
            super(makeRootNode(docroot));<br />
//              makeRootNode(docroot);<br />
        }</p>
<p>        private static DefaultTreeModel makeRootNode(Node docroot){<br />
            DefaultMutableTreeNode rootTreeNode = buildTree(docroot);<br />
            DefaultTreeModel treeModel = new DefaultTreeModel(rootTreeNode);<br />
            return(treeModel);<br />
        }</p>
<p>        private static DefaultMutableTreeNode buildTree(Node docroot){<br />
            DefaultMutableTreeNode rootTreeNode = new DefaultMutableTreeNode(docroot.getNodeName());<br />
            addChildren(rootTreeNode,docroot);<br />
            return(rootTreeNode);<br />
        }</p>
<p>        private static void addChildren(DefaultMutableTreeNode parentTreeNode, Node parentXMLElement){<br />
            NodeList childElements = parentXMLElement.getChildNodes();</p>
<p>            if(childElements.getLength() > 0){<br />
                for(int i=0; i < childElements.getLength(); i++){<br />
                    Node childElement = childElements.item(i);<br />
                    if (childElement.getNodeType() == Node.ELEMENT_NODE){<br />
                        DefaultMutableTreeNode childTreeNode = new DefaultMutableTreeNode(childElement.getNodeName());<br />
                        parentTreeNode.add(childTreeNode);<br />
                        addChildren(childTreeNode, childElement);<br />
                    }<br />
                }<br />
            }<br />
        }</p>
<p>//        private static String treeNodeLabel(Node childElement){<br />
//<br />
//            String content = "";<br />
//            NodeList childnodes = childElement.getChildNodes();<br />
//<br />
//            if(childnodes.getLength() > 0){<br />
//                for(int i=0; i < childnodes.getLength (); i++){<br />
//                    if(childnodes.item (i).getNodeType() == Node.TEXT_NODE){<br />
//                        content = childnodes.item(i).getNodeValue ();<br />
//                        return content;<br />
//                    }<br />
//                    else if(childnodes.item(i).getNodeType() == Node.ELEMENT_NODE){<br />
//                        if(childnodes.item(i).hasChildNodes()){<br />
//                            Node firstchild = childnodes.item(i).getFirstChild();<br />
//                            content = treeNodeLabel(firstchild);<br />
//                            return content;<br />
//                        }<br />
//                    }<br />
//                }<br />
//            }<br />
//            return content;<br />
//        }<br />
    }<br />

Message was edited by: shan-man (formatted code)

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
madhatress
Offline
Joined: 2006-02-17

Nevermind. I figured out the JTree.

I am using netbeans 4 with the form designer, and I could not figure out how to load the TreeModel when the JTree was initialized (For those that don't know, Netbeans doesn't allow you to edit the form designer code). But I used the pre-creation code and custom creation code fields in the form designer to do my bidding!

I am still having trouble getting the list to refresh and load new data when the combo box value changes. Anyone got any ideas?