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

  • Home
  • SEARCH
  • 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 63681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:40:14+00:00 2026-05-10T18:40:14+00:00

I’m writing a custom swing component (something completely new, but think JTree or JList).

  • 0

I’m writing a custom swing component (something completely new, but think JTree or JList). I’m trying to follow the general design of JTree, JTable, JList etc for consistency (I’ve also seen various poor 3rd party components abandon the separable model and/or renderer approach).

So, I have a model full of nodes, the component itself and a renderer. At some point the node has to be turned into text and displayed by a renderer. I’m not clear on the best way to do this:

  • Pass the node itself (as Object) to the renderer, and let the renderer decide how to display it.
    • This is how JList does it.
    • Requires a customised renderer just to change the text.
    • Allows great flexibility in how to display the node (doesn’t even have to be text).
  • Pass the node itself (as Object) to the renderer, but have a convertValueToText() method in the component class.
    • This is how JTree does it.
    • Renderers can be just as flexibile as before – don’t have to use this method.
    • Have to override component to change the text transformation.
  • As above, but delegate convertValueTotext() to the model.
    • This is how JXTable does it.
    • The model is probably the best place for this method – and it’s easier to override there.

I don’t want to have to customise the renderer just to change the text, but I’d like to be able to customise the renderer to do more than display a model-displayed string (else why bother with renderers). I really don’t like the fact that JXTable uses reflection to look for convertValueToText() in the model – this smells of bad magic to me.

Can anyone shed any light on this oft-neglected part of Swing?

SOLUTION

What I ended up doing was this:

  • Add a method to the model which returns a string for the given node. Importantly, this can be null to indicate that the renderer should know what to do or that we simply can’t provide anything useful.
  • The component has the same method, and passes the call on to the model. This is important for view-model separation. The renderer calls this method, so it doesn’t talk to the model directly.
  • The default renderer calls the above method and if it’s not null, it uses it, otherwise it can call toString on the value, or provide a default, or whatever.

This leaves developers a choice when they want to override the displayed value – Override the method with a non-null return value knowing that the default renderer will display this text. – Provide a custom renderer which is passed the actual node object so it can do ‘clever’ things if it needs to.

I’m quite happy with it – it ‘feels’ right, it works, and it’s easy to use.

Thanks for your perspectives!

  • 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. 2026-05-10T18:40:15+00:00Added an answer on May 10, 2026 at 6:40 pm

    Good question. This is not specific to Swing, but a philosophical question about the difference between a model and a view.

    In general, is converting objects into text the job of the model or the view? My purist head says that actually you want a hierarchy of views – one to convert object model to text, and one to display the text. You might even want more than two – for instance, object-to-text, text-to-document-structure, document-structure-to-HTML, and then CSS to present to the user.

    However, pragmatism says this may get too hard to remember and maintain. So in your circumstance I would suggest: think about how likely it is you’ll ever want to extract non-text data from the model. If it is not very likely, then put the equivalent of convertValueToText in the model.

    Otherwise, allow the component to either use a renderer, if it is given one, or else take the object value and convert it to text internally.

    This allows maximum flexibility and probably makes things feel most natural to the users of the API. I believe this is the JTable model though I haven’t used Swing for a long time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.