4

In order to be able to display a sentence on a, say, JPanel with a GridLayout(1,0) [i.e., only one line/row] and then be able to draw a syntax tree (or similar) above it, I want to display the sentence as a row of Strings, which each include one word.

The single Strings should then be either selectable (as in a JList), or I should at least be able to get their Location on the JPanel via getLocation().

Up to this point I have tried the following options, and had the following issues: - Single Strings as JLabels: The JLabels are stretched out to fill the JPanel width, re-sizing them to fit the single String they're displaying seems complicated. I would want to be able to do this, however, to make the sentence look like a sentence and not like a badly layed out table. - JList: All the functionality I want, but I'm unaware of an option to re-size the "cells" of a single String (cf. JLabel above). Also, I'm having difficulties restricting display of the JList to a single line/row (cf. another of my questions). - JTextArea: I couldn't get my head round how to get the Location of the single Strings that I had appended to the JTextArea.

I'm aware that drawString() might be an option, but I'm afraid to use it since I don't want to mix AWT and Swing. Also, I would need to calculate the int values for x and y for every single String. And I'm not sure whether I'd be able to get their Locations at all (although I could of course save their ints in a Map or Vector since I have to calculate them anyway).

Thankful for any suggestions! Thanks!

Community
  • 1
  • 1
s.d
  • 4,017
  • 5
  • 35
  • 65

2 Answers2

3

I would use JTextArea and method modelToView()/viewToModel() to get x,y for position in nthe string and position in the string for coordinates x and y.

Also use Utilities class getWordStart() getWordEnd() getRowStart() getRowEnd() methods.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
1

EDIT: As noted by camickr in the comments, setSize() is not an appropriate way to lay out Components (as this is automatically done by the respective LayoutManager, I have removed the respective code from my answer.

Triggered by StanislavL's answer, I have found a solution to do it via JTextField, albeit by using one for each String rather than just one (as suggested by StanislavL).

I can now easily getLocation() for each JTextField. Simple, really!

I'd like to thank StanislavL for his answer, without which I'd never have though about this, and camickr for his comment.

Community
  • 1
  • 1
s.d
  • 4,017
  • 5
  • 35
  • 65
  • -1, Why aren't you using a layout manager? Just use the default FlowLayout and the components will align properly in a row. – camickr Aug 22 '11 at 14:49
  • Sorry, but I think -1 is inappropriate. Usually, whenever you are using JPanel, you are using a LayoutManager (see my edit). And of course the components will align in a row, that wasn't what I'm after. In fact, I *am* using FlowLayout. What I wanted was to be able to get the location (via `getLocation()` for each "String" ("componentified" in both StanislavL's and my example as JTextFields)), or be able to make the single *words* selectable. And for this my solution was more workable for *me*, although StanislavL's is obviously just as good, and only triggered mine. Hence I think it's not -1. – s.d Aug 22 '11 at 19:53
  • Sorry, I down voted the posting because your code is using setSize() and you said you haven't specified a layout manager which I interpreted to mean that you where using a null layout. However, if you are just using the default layout manager, which is a FlowLayout then the setSize() is not required since FlowLayout will respect the preferred size of the text field. It is the job of ANY layout manager to set the size of a component. You should NEVER invoke the setSize() method. I'll remove the down vote when you remove the setSize() code from the example. – camickr Aug 22 '11 at 20:08
  • Ah okay, will remove the `setSize()` code then. I was wondering whether `validate()`ing a component wouldn't be enough for all cases, but haven't checked. Thanks for the explanation. – s.d Aug 23 '11 at 07:34
  • I've simply chosen this as accepted answer because for me it is easier to use several `JTextField`s and `getLocation()` for each (as I need them) than to use one large `JTextField`. **However**, I want to stress that StanislavL's answer is just as valid and will perhaps be more suitable for other users. – s.d Aug 24 '11 at 14:12
  • +1 for elaborating. You might still accept @StanislavL's answer, for the reason suggested in my comment on the accepted answer to this [question](http://stackoverflow.com/questions/5048852/using-jfreechart-to-display-recent-changes-in-a-time-series). – trashgod Nov 11 '11 at 15:59