Custom Swing Component - Turn node into text

I am writing a custom swing component (something completely new, but I think JTree or JList). I am trying to follow the general design of JTree, JTable, JList, etc. For consistency (I also saw how various poor third-party components abandon the shared model and / or renderer approach).

So, I have a model full of nodes, the component itself and the visualization tool. At some point, the node needs to be converted to text and rendered with a renderer. I do not know how to do that:

  • Pass the node itself (as an object) into the renderer and let the renderer decide how to display it.
    • This is how JList does it.
    • To change the text requires a custom renderer.
    • Allows you to display the node (not necessarily text).
  • Pass the node itself (as an Object) to the renderer, but the component class has a convertValueToText () method.
    • This is how JTree does it.
    • Renderers can be as flexible as before - no need to use this method.
    • You must override the component to change the text conversion.
  • As above, but delegate convertValueTotext () to the model.
    • Here's how JXTable does it.
    • The model is probably the best place for this method - and it is easier to override.

I don’t want to set up the renderer just to change the text, but I would like to customize the visualizer to do more than display a line with the model displayed (otherwise why bother with visualizers). I really don't like the fact that JXTable uses reflection to look for convertValueToText () in the model - it smells like bad magic to me.

- ?

, , :

  • , node. , NULL, , , .
  • . . , .
  • , , , toString - .

, - , , . - , node, "" , .

- "" , , .

!

+3
3
. Swing, .

, ? - , - , - . , - , -, -, ---HTML, CSS .

, , . : , , - . , convertValueToText .

, , .

, , API. , JTable, Swing.

+1

AFAIK JList, JTree , . JComponent, Tree/List, .
. JLabel. , , Formatter TextRender, .

  • Stephan
0

, . , , . . . , . DefaultRenderer, AbstractRenderer , toText (Object o) .. , , . ? , . ?

0

Source: https://habr.com/ru/post/1698437/


All Articles