Desidero visualizzare il nome e i valori di un attributo accanto al nome del nodo in JTree
. Qualcuno può dirmi come farlo? Ecco il codice che uso per visualizzare la JTree
:Come visualizzare un nome e un valore di attributo specifici accanto a un nodo in JTree?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Event.*;
import java.io.*;
import javax.swing.tree.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import org.apache.xerces.parsers.*;
public class XMLTreeView {
private SAXTreeBuilder saxTree = null;
private static String file = "";
public static void main(String args[]) {
JFrame frame = new JFrame("XMLTreeView: [ games.xml ]");
frame.setSize(400, 400);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent ev) {
System.exit(0);
}
});
file = "example1.xml";
new XMLTreeView(frame);
}
public XMLTreeView(JFrame frame) {
frame.getContentPane().setLayout(new BorderLayout());
DefaultMutableTreeNode top = new DefaultMutableTreeNode(file);
// DefaultMutableTreeNode top = new DefaultMutableTreeNode("XML Document");
saxTree = new SAXTreeBuilder(top);
try {
SAXParser saxParser = new SAXParser();
saxParser.setContentHandler(saxTree);
saxParser.parse(new InputSource(new FileInputStream(file)));
} catch (Exception ex) {
top.add(new DefaultMutableTreeNode(ex.getMessage()));
}
JTree tree = new JTree(saxTree.getTree());
JScrollPane scrollPane = new JScrollPane(tree);
frame.getContentPane().add("Center", scrollPane);
frame.setVisible(true);
}
}
class SAXTreeBuilder extends DefaultHandler {
private DefaultMutableTreeNode currentNode = null;
private DefaultMutableTreeNode previousNode = null;
private DefaultMutableTreeNode rootNode = null;
public SAXTreeBuilder(DefaultMutableTreeNode root) {
rootNode = root;
}
public void startDocument() {
currentNode = rootNode;
}
public void endDocument() {
}
public void characters(char[] data, int start, int end) {
String str = new String(data, start, end);
if (!str.equals("") && Character.isLetter(str.charAt(0))) {
currentNode.add(new DefaultMutableTreeNode(str));
}
}
public void startElement(String uri, String qName, String lName, Attributes atts) {
previousNode = currentNode;
currentNode = new DefaultMutableTreeNode(lName);
// Add attributes as child nodes //
attachAttributeList(currentNode, atts);
previousNode.add(currentNode);
}
public void endElement(String uri, String qName, String lName) {
if (currentNode.getUserObject().equals(lName)) {
currentNode = (DefaultMutableTreeNode) currentNode.getParent();
}
}
public DefaultMutableTreeNode getTree() {
return rootNode;
}
private void attachAttributeList(DefaultMutableTreeNode node, Attributes atts) {
for (int i = 0; i < atts.getLength(); i++) {
String name = atts.getLocalName(i);
String value = atts.getValue(name);
node.add(new DefaultMutableTreeNode(name + " = " + value));
}
}
}
Nel JTree
, voglio visualizzare l'attributo NEType
accanto NE
nodo e il nodo equipmentHolderType
attributo accanto EQHO
NE NEType=WBTS
EQHO equipmentHolderType=Subrack
voglio avere qualcosa del genere:
Ecco un esempio di file XML che ho a che fare con:
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes" ?>
<HWData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="hw_data.xsd">
<Header xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" AdaptationName="NWI3BulkUpload" AccessProtocol="NWI3" time="2004-01-01T00:04:02" uploaded="true" version="1.0" />
<NE xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" vendorName="Nokia Siemens Networks" objectClass="NE" objectClassVersion="1" MOID="NE-RNC-1/DN:NE-WBTS-4183" NEId="PLMN-PLMN/RNC-1/WBTS-4183" NEType="WBTS" operationalState="enabled" locationName="Tun4183" nameFromPlanningSystem="" systemTitle="Nokia Flexi WCDMA Base Station">
<EQHO vendorName="Nokia Siemens Networks" objectClass="EQHO" objectClassVersion="1" MOID="NE-RNC-1/DN:NE-WBTS-4183/EQHO-40448" equipmentHolderId="40448" equipmentHolderType="Subrack" equipmentHolderSpecificType="472100A-40448" identificationCode="472100A" version="101" serialNumber="K9111641678" userLabel="FRGP" state="working">
<UNIT vendorName="Nokia Siemens Networks" objectClass="UNIT" objectClassVersion="1" MOID="NE-RNC-1/DN:NE-WBTS-4183/EQHO-40448/UNIT-158" unitId="158" unitTypeActual="CORE_FRGP" identificationCode="084629A" version="101" serialNumber="K9111641678" />
</EQHO>
<EQHO vendorName="Nokia Siemens Networks" objectClass="EQHO" objectClassVersion="1" MOID="NE-RNC-1/DN:NE-WBTS-4183/EQHO-173" equipmentHolderId="173" equipmentHolderType="Subrack" equipmentHolderSpecificType="471469A-173" identificationCode="471469A" version="" serialNumber="L1104816112" userLabel="FSME" state="working">
<UNIT vendorName="N" objectClass="UNIT" objectClassVersion="1" MOID="NE-RNC-1/DN:NE-WBTS-4183/EQHO-173/UNIT-16" unitId="16" unitTypeActual="CORE_FSME" identificationCode="083833A" version="104" serialNumber="L1104816112" />
<UNIT vendorName="NOKIA SIEMENS NETWORKS" objectClass="UNIT" objectClassVersion="1" MOID="NE-RNC-1/DN:NE-WBTS-4183/EQHO-173/UNIT-225" unitId="225" unitTypeActual="FTLB" identificationCode="471984A" version="103" serialNumber="RY104807867" />
</EQHO>
<EQHO vendorName="NSN" objectClass="EQHO" objectClassVersion="1" MOID="NE-RNC-1/DN:NE-WBTS-4183/EQHO-40192" equipmentHolderId="40192" equipmentHolderType="Subrack" equipmentHolderSpecificType="472083A-40192" identificationCode="472083A" version="101" serialNumber="L6105220714" userLabel="FXDA" state="working" />
</NE>
</HWData>
Avete difficoltà la costruzione del [ 'TreeModel'] (https://docs.oracle.com/javase/ tutorial/uiswing/components/tree.html # data) o accedere a quel modello nel tuo ['TreeCellRenderer'] (https://docs.oracle.com/javase/tutorial/uiswing/components/tree.html#display)? Modifica la tua domanda per includere un [esempio completo] (http://stackoverflow.com/help/mcve) che presenta qualsiasi problema riscontrato. – trashgod
no non ho alcun problema con il codice se cerchi di eseguire il codice nella tua macchina genererà il jtree senza quello che ho scritto su strong nella foto..quello che sto cercando è come modificare il codice per poterlo visualizzare il jtree piace nella foto !! – Hen
Cosa succede con il codice che hai postato? Il modo consigliato è di comporre il testo in un 'TreeCellRenderer 'personalizzato. – trashgod