views:

40

answers:

2

Is there a library that would give me 2D graphics with a focus on Text for Swing. I am building a simple form designer and need to position text correctly on a 2D display. It would be nice if there was a library that handled the newlines and possibly editing.

+2  A: 

An approach I've used successfully in the past is to draw your form with Graphics2D into a single panel. Graphics2D gives you precise text and line positioning. When you want to edit the text, have the user click on the text and place a JTextArea or similar into the panel where the text is while the user edits.

DJClayworth
+1  A: 

I like the answer that suggests using a JTextArea when the user is going to edit text, since it is simple. However, if you want true in-place editing, while also rendering exactly your text content, you could investigate creating your own View implementation for a JEditorPane. This gives you a ready-made model and controller for editing, allowing you provide the presentation, so you can render as accurately as necessary. And with the JEditorPane.DefaultEditorKit available as source, you have plenty of guidance about how to implement your own View.

This may be overkill for your needs, or it may give you the fine level of control needed.

mdma
I can do the editing, but still, I see issues with displaying text on a Graphics2D displaying.
Berlin Brown
Can you elaborate - what issues do you see? Many renderers in Swing itself are built on Java2D - you may even be able to leverage the existing View implementations, with "tweaks" to get exactly what you are looking for.
mdma