Java Swing text as a grid?

Discussion in 'Java' started by rupertlssmith, Apr 22, 2014.

  1. 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
     
    rupertlssmith, Apr 22, 2014
    #1
    1. Advertisements

  2. rupertlssmith

    markspace Guest


    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.
     
    markspace, Apr 22, 2014
    #2
    1. Advertisements

  3. rupertlssmith

    Jeff Higgins Guest

    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.
     
    Jeff Higgins, Apr 22, 2014
    #3
  4. rupertlssmith

    Jeff Higgins Guest

    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.
     
    Jeff Higgins, Apr 22, 2014
    #4
  5. rupertlssmith

    Rupert Smith Guest

    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
     
    Rupert Smith, Apr 23, 2014
    #5
  6. rupertlssmith

    Roedy Green Guest

    You could just have a GridBag layout of JTextAreas.

    Most fun is to do it the way you would in AWT with a Canvas (JPanel in
    Swing). See http://mindprod.com/products1.html#JDISPLAY for sample
    code.
     
    Roedy Green, Apr 24, 2014
    #6
  7. rupertlssmith

    Rupert Smith Guest

    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.
    Looks like that is the route I am taking. :)

    Rupert
     
    Rupert Smith, Apr 25, 2014
    #7
  8. rupertlssmith

    Arne Vajhøj Guest

    Arne Vajhøj, Apr 29, 2014
    #8
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.