1

Is there a Java lib out there which allows you to programatically build a table, e.g. by adding rows/cols one after each other, and finally returns a string (...that can be printed out on console or so)?

The libs I have found and know address UI components (JTable, Swing, AWT) only...

2
  • 1
    If you want to do it yourself you might want to look at this question (remove the commas): stackoverflow.com/questions/4338521/… Commented Apr 5, 2011 at 12:02
  • You might wanna check this one out: code.google.com/p/lanterna It is a bit different and does a bit more than what you want to, but it really easy to use and since you did not specify on what console you want to print your tables, it might still be what you are looking for. Commented Apr 5, 2011 at 14:45

2 Answers 2

4

I was looking for something like this and ended up writing it myself, which I am happy to share with you. I wanted something that I can use like

ConsoleStringTable table= new ConsoleStringTable();
table.addString(0, 0, "AveryVeryVeryLongWord");
table.addString(0, 1, "AnotherWord");
table.addString(1, 0, "Short");
table.addString(1, 1, "Fast");
System.out.println(table.toString());

Which outputs

AveryVeryVeryLongWord AnotherWord
Short                 Fast       

Implementation

using guava for padding

package util;

import java.util.HashMap;
import java.util.Map;

import com.google.common.base.Strings;

public class ConsoleStringTable {

    private class Index {

        int _row, _colum;

        public Index (int r, int c) {
            _row= r;
            _colum= c;
        }

        @Override
        public boolean equals (Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Index other= (Index) obj;
            if (_colum != other._colum)
                return false;
            if (_row != other._row)
                return false;
            return true;
        }

        @Override
        public int hashCode () {
            final int prime= 31;
            int result= 1;
            result= prime * result + _colum;
            result= prime * result + _row;
            return result;
        }
    }

    Map<Index, String> _strings= new HashMap<ConsoleStringTable.Index, String>();
    Map<Integer, Integer> _columSizes= new HashMap<Integer, Integer>();

    int _numRows= 0;
    int _numColumns= 0;

    public void addString (int row, int colum, String content) {
        _numRows= Math.max(_numRows, row + 1);
        _numColumns= Math.max(_numColumns, colum + 1);

        Index index= new Index(row, colum);
        _strings.put(index, content);

        setMaxColumnSize(colum, content);
    }

    private void setMaxColumnSize (int colum, String content) {
        int size= content.length();
        Integer currentSize= _columSizes.get(colum);
        if (currentSize == null || (currentSize != null && currentSize < size)) {
            _columSizes.put(colum, size);
        }
    }

    public int getColumSize (int colum) {
        Integer size= _columSizes.get(colum);
        if (size == null) {
            return 0;
        } else {
            return size;
        }
    }

    public String getString (int row, int colum) {
        Index index= new Index(row, colum);
        String string= _strings.get(index);
        if (string == null) {
            return "";
        } else {
            return string;
        }
    }

    public String getTableAsString (int padding) {
        String out= "";
        for (int r= 0; r < _numRows; r++) {
            for (int c= 0; c < _numColumns; c++) {
                int columSize= getColumSize(c);
                String content= getString(r, c);
                int pad= c == _numColumns - 1 ? 0 : padding;
                out+= Strings.padEnd(content, columSize + pad, ' ');
            }
            if (r < _numRows - 1) {
                out+= "\n";
            }
        }
        return out;
    }

    @Override
    public String toString () {
        return getTableAsString(1);
    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

2

I don't know such a library but you could go with a template engine like FreeMarker or Velocity. Define your table layout in a template, put your content (e.g. List of List to define your rows and columns) in the template model and 'merge' the template with your content to receive your result (table) String. Might be not the simplest approach but it is also not that complicated. In any case you have the full control of your result!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.