Display row string on JComponent so you can select single rows / their location getLocation () - maybe?

To be able to map the sentence to a, say, JPanel , using GridLayout(1,0) [i.e. only one line / line], and then be able to draw a syntax tree (or similar) above it, I want to show the sentence as a string of String s, each of which contains one word.

Then a single String must be selected (as in the JList ), or I should at least get their location on the JPanel via getLocation() .

Until now, I tried the following options and had the following problems: - A single String like JLabel s: JLabel stretched to fill the width of the JPanel , reformatting them to match the single String that they display seems complicated. However, I would like to be able to do this so that the proposal looks like a proposal, and not like a poorly laid out table. - JList : all the functionality that I want, but I do not know about the ability to resize the "cells" of a single String (see JLabel above). Also, I am having difficulty limiting the JList display to one line / line (see another one of my questions ). - JTextArea : I couldn't figure out how to get the location of the only String that I added to JTextArea .

I know drawString() may be an option, but I'm afraid to use it since I don't want to mix AWT and Swing. In addition, I will need to calculate int values ​​for x and y for each String . And I'm not sure if I can get their locations at all (although I could, of course, save them int on a map or vector, since I still have to figure them out).

Thanks for any suggestions! Thanks!

+4
source share
2 answers

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

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

+3
source

EDIT: as noted in camickr comments, setSize() not an appropriate way to compose Components (since this is automatically done by the corresponding LayoutManager , I removed the corresponding code from my answer.

By running StanislavL's answer , I found a solution to do this via JTextField , although using one for each String , and not just one (as suggested by Stanislav L.).

Now I can easily getLocation() for each JTextField . Just really!

I would like to thank StanislavL for his answer, without which I would never have thought about it, and camickr for his comment.

+1
source

All Articles