2011-04-05 85 views
1

是否有Java lib,允許您以編程方式構建表,例如通過一個接一個地添加rows/cols,最後返回一個字符串(...可以在控制檯上打印出來等等)?Java庫在控制檯上構建和打印表格?

我已經找到了庫和知道地址的UI組件(JTable中,鞦韆,AWT)只有...

+1

如果你想自己,你可能做到這一點,其輸出想看看這個問題(刪除逗號):http:// stacko verflow.com/questions/4338521/is-there-an-easy-way-to-output-a-column-wise-csv – dacwe 2011-04-05 12:02:19

回答

3

我正在尋找這樣的東西,並最終自己寫了,我很高興與大家分享。我想要的東西,我可以用這樣的

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()); 

AveryVeryVeryLongWord AnotherWord 
Short     Fast  

實施

利用番石榴用於填充

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); 
    } 

} 
2

我不知道這樣的庫,但你可以用像FreeMarkerVelocity模板引擎去。在模板中定義表格佈局,在模板模型中放置內容(例如列表列表以定義行和列),並將模板與內容「合併」以接收結果(表格)字符串。可能不是最簡單的方法,但也並不複雜。無論如何,你可以完全控制你的結果!