Java Swing text as a grid?

R

rupertlssmith

Hi,

I'd like a text UI component that lets me place monospaced text within it, using row/column addressing. The built-in text controls are constructed around a Document model, which models text as a single stream (by stream I mean linear buffer of text), with rendered row/column derived from placement of newlines, wrapping strategy, and lots of other things.

The Document model lets you have multiple root 'elements', each of which isa single stream. I'm thinking I could extend and existing text control, perhaps JTextArea, to render one root element per row, and disable all line wrapping. It might make more sense than having to insert newlines into the text stream to control row position.

Does anyone have any thoughts on how to create a simple text grid UI control with Swing? I suppose I should take a look at lanterna, which must be achieving something similar to what I want, when emulating a console under Swing.

Thanks for your input.

Rupert Smith
 
M

markspace

Hi,

I'd like a text UI component that lets me place monospaced text
within it, using row/column addressing. The built-in text controls


I would just keep a separate structure, an array of lines, and then
index into that.

public class MyTextArray {

private JTextArea text = ...
private ArrayList<String> lines = ...

public void addText( String line ) {
lines.add( line );
text.add( line );
}

public char get( int row, int column ) {
if( row > lines.size() ) throw new IllegalArgumentException();
String s = lines.get( row );
if( column >= s.length() ) throw new IllegalArgumentException();
return s.charAt( column );
}

}

Flavor to taste.
 
J

Jeff Higgins

Hi,

I'd like a text UI component that lets me place monospaced text within it, using row/column addressing. The built-in text controls are constructed around a Document model, which models text as a single stream (by stream I mean linear buffer of text), with rendered row/column derived from placement of newlines, wrapping strategy, and lots of other things.

The Document model lets you have multiple root 'elements', each of which is a single stream. I'm thinking I could extend and existing text control, perhaps JTextArea, to render one root element per row, and disable all line wrapping. It might make more sense than having to insert newlines into the text stream to control row position.

Does anyone have any thoughts on how to create a simple text grid UI control with Swing? I suppose I should take a look at lanterna, which must be achieving something similar to what I want, when emulating a console under Swing.

javax.swing.text is a big, and to my mind, complex package.
<http://docs.oracle.com/javase/8/docs/api/javax/swing/text/View.html>
Sorry I can't offer concrete suggestions.
 
J

Jeff Higgins

The key, I think, to working with the Swing text package is having your
document model well sussed out before considering the views and controller.

You might look at the source of javax.swing.text.PlainView after
you've got your document model completely worked out.
 
R

Rupert Smith

On 04/22/2014 11:45 AM, Jeff Higgins wrote:
You might look at the source of javax.swing.text.PlainView after
you've got your document model completely worked out.

PlainView: The view represents each child element as a line of text.

Sounds like its pretty close to what I'm after, thanks.

Its a beast for sure. 6 chapters in the Swing text book I have, and even that doesn't leave room for the details.

I looked an lanterna and realised that rendering a simple grid of text can be as simple as implementing paintComponent()

for (int i = 0; i < columns; i++) {
for (int j = 0; j < rows; j++) {
graphics2d.drawString(characters[j], ...

So I think I will keep the model very simple for now, and render that way. I can re-implement on top of text.Document and play around with text.View as and when more capability is required.

Rupert
 
R

Rupert Smith

You could just have a GridBag layout of JTextAreas.

I hadn't thought of that. GridLayout I think you mean? with one character per grid cell. Would have been a quick and dirty way to get started.
Most fun is to do it the way you would in AWT with a Canvas (JPanel in
Swing).

Looks like that is the route I am taking. :)

Rupert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top