1

this is my problem : i have created a simple program to manage a library , and i want that the program displays in a text area the library in a formatted output(columns's alignment). I use a stringbuilder to set text in textArea but after many attempts i don't understand how separate fields.Many thanks :)

public String toString()
{
    StringBuilder sb = new StringBuilder ( );
    String format ="%s %s %s %s %s";

    for ( libro l : getLibreria ( ) )
    {
    sb.append(String.format(format,                     l.getAutore(),l.getTitolo(),l.getSottoTitolo(),l.getEditore(),l.getDataEdizione() ));
        sb.append("\n");
        }
    return sb.toString();
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Riccardo
  • 129
  • 1
  • 6
  • 1
    what is your output ? and you desired output, if you upload photos to the post will be great – nachokk Jul 16 '13 at 23:29
  • 3
    Why not use a `JTable`? – MadProgrammer Jul 17 '13 at 00:12
  • Had you tried something like `String format = "%-10s %-10s %-10s..."`, here `-10`, will actually represent the max width that you want to give to your column. Else please provide one [SSCCE](http://sscce.org/) to try on – nIcE cOw Jul 17 '13 at 04:11
  • i uploaded the output on tynpic,this is the link : http://tinypic.com/r/1239p2p/5 .Thaks for the answers :) . I can try to use JTable,but in future i want to print this. – Riccardo Jul 17 '13 at 06:49
  • 1. required to use Font(s) - Monospaced where all chars has the same pixels widht and height, 2. align to the JTextAreas columns, – mKorbel Jul 17 '13 at 07:04
  • many thank's,this is ok ?Font font = new Font("Arial", Font.BOLD, 12); textArea.setFont(font); But how can align JTextArea columns? – Riccardo Jul 17 '13 at 07:17
  • sorry , do you means this? Font font = new Font("monospaced", Font.BOLD, 12); – Riccardo Jul 17 '13 at 07:23
  • String format ="%-100s %-100s %-100s %-100s %-100s"; many many thans to all ,expecially to mKorbel , monospaced is the way ,this is the result :) http://tinypic.com/r/35c0dg0/5 – Riccardo Jul 17 '13 at 07:32

0 Answers0