Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7499485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:50:25+00:00 2026-05-29T19:50:25+00:00

I have a simple Jython GUI that displays an XML file in a JTree.

  • 0

I have a simple Jython GUI that displays an XML file in a JTree. Is there a method I can override in the tree model that will allow me to customize what the nodes in the JTree are called? Or do I need to do something with a renderer like Java? I’m looking for a Jythonic way to do this (as opposed to straight Java) if possible. I have access to Jython 2.5.0. My simple code looks like this:

from java import awt
from javax import swing
from java.lang import System
from xml.etree import ElementTree

class XmlTreeModel(swing.tree.TreeModel):
    def __init__(self, etree):
        self.etree = etree
    def getRoot(self):
        return self.etree.getroot()
    def getChildCount(self, object):
        return len(object)
    def getChild(self, parent, index):
        return parent[index]

class Viewer(swing.JFrame):
    def __init__(self):
        super(Viewer, self).__init__()
    def display(self, fileName):
        xmlObject = ElementTree.parse(fileName)
        xmlTreeModel = XmlTreeModel(xmlObject)
        jTree = swing.JTree(xmlTreeModel)
        self.contentPane.add(jTree)
        self.contentPane.setPreferredSize( awt.Dimension(100, 200) )        
        self.pack()
        self.setDefaultCloseOperation(swing.JFrame.EXIT_ON_CLOSE)
        self.setLocationRelativeTo(None)
        self.setVisible(True)

if __name__ == "__main__":
    viewer = Viewer()
    viewer.display('my.xml')

Right now the nodes appear as <Element Category at 2> and I’d like to change them to just say “Category” or even better, something custom from the XML attributes.

EDIT:
I was able to extend Chui Tey’s answer so that my tree displays an XML attribute by changing DisplayNode slightly:

class DisplayNode(object):
    def __init__(self, node):
        self.node = node
    def __repr__(self):
        return self.node.get('Name')
    def __getitem__(self, item):
        return self.node[item]
    def __len__(self):
        return len(self.node)

Instances of the class are then created with DisplayNode(self.etree.getroot()) and DisplayNode(parent[index]), respectively. For those of you arriving via search engine, this works for me because I know all of my XML nodes will have an attribute called Name.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T19:50:27+00:00Added an answer on May 29, 2026 at 7:50 pm

    JTree calls repr(node) on each xml node to get a string representation of what to display on the leaves of its tree.

    You can override it by supplying your own repr method.

    In the example below, I have set the value manually in the displaytext attribute.

    from java import awt
    from javax import swing
    from java.lang import System
    from xml.etree import ElementTree
    
    class DisplayNode:
        def __init__(self, node, displaytext):
            self.node = node
            self.displaytext = displaytext
        def __repr__(self):
            return self.displaytext
        def __getitem__(self, item):
            return self.node[item]
        def __len__(self):
            return len(self.node)
    
    class XmlTreeModel(swing.tree.TreeModel):
        def __init__(self, etree):
            self.etree = etree
        def getRoot(self):
            return DisplayNode(self.etree.getroot(), "Tree Root")
        def getChildCount(self, object):
            return len(object)
        def getChild(self, parent, index):
            return DisplayNode(parent[index], "Another Child")
    
    class Viewer(swing.JFrame):
        def __init__(self):
            super(Viewer, self).__init__()
        def display(self, fileName):
            xmlObject = ElementTree.parse(fileName)
            xmlTreeModel = XmlTreeModel(xmlObject)
            jTree = swing.JTree(xmlTreeModel)
            self.contentPane.add(jTree)
            self.contentPane.setPreferredSize( awt.Dimension(100, 200) )        
            self.pack()
            self.setDefaultCloseOperation(swing.JFrame.EXIT_ON_CLOSE)
            self.setLocationRelativeTo(None)
            self.setVisible(True)
    
    if __name__ == "__main__":
        viewer = Viewer()
        viewer.display('my.xml')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have simple query that loads data from two tables into GUI. I'm saving
I have simple win service, that executes few tasks periodically. How should I pass
I have simple form. <form target=_blank action=somescript.php method=Post id=simpleForm> <input type=hidden name=url value=http://...> <input
I have simple SSIS package which reads data from flat file and insert into
I have simple SSIS package where I import data from flat file into SQL
I have simple WinForms application where modifying Windows Registry. The problem is that in
I have simple template that's html mostly and then pulls some stuff out of
I have simple ontology called campus.owl.There is a class calledLecturer and which has two
I have simple JavaScript code that is using the Ajax API for fetching a
I have a simple question how can i show the number 12045678 as 12,045,678

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.