-1

I am trying that all the point are in the same position. But only I can make look it like this. Could you give me ideas to make it look more tidy.

enter image description here

Andronicus
  • 25,419
  • 17
  • 47
  • 88
Fernando.F
  • 65
  • 8
  • `\t` alone is too dumb for a correct line-up in all situations. You need to correctly compute the offset using the length of the strings and then fill it up with spaces. See [Java printf formatting to print items in a table or columns](https://stackoverflow.com/questions/33466526/java-printf-formatting-to-print-items-in-a-table-or-columns) (you can use the same format syntax with `String#format`, a monospace font makes things easier). – Zabuzard Mar 10 '19 at 17:38

3 Answers3

3

You have several options here:

  • Create a JTextArea (which it looks like you're doing)
    • Give the text area a monospaced font
    • Then display formatted Strings using String.format("%-20s :%20s%n", text1, text2) (the numbers may vary) to allow for proper alignment of the text
  • Use a JTable to hold and display tabular data
  • Create a grid with each line holding a JLabel and a JTextField,
    • Format the placement of the components using a GridBagLayout
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
2

You could also write your own method to format your Strings

public static void main(String[] args) {
    String line1 = "Importe a pagar ";
    String line2 = "Descuento ";
    String line3 = "Importe ";
    String line4 = "Obsequio ";

    String[] correctedStrings = setCorrectAmountOfTabs(line1, line2, line3, line4);

    Arrays.stream(correctedStrings).map(line -> line + " : 50").forEach(System.out::println);
}

private static String[] setCorrectAmountOfTabs(String... lines) {
    if (lines == null || lines.length == 0) return;
    int lengthOfTab = 4;
    int targetAmount = Arrays.stream(lines)
                             .map(line -> line.length() / lengthOfTab)
                             .max(Integer::compare)
                             .get();

    return Arrays.stream(lines)
            .map(line -> {
                int currentAmount = line.length() / lengthOfTab;
                String tabs = IntStream.rangeClosed(0, targetAmount - currentAmount)
                                       .mapToObj(i -> "\t")
                                       .collect(Collectors.joining());
                return line + tabs;
            })
            .toArray(String[]::new);
}

The execution of this method gives the following result

Importe a pagar      : 50
Descuento            : 50
Importe              : 50
Obsequio             : 50

You could then just manipulate the output String array to use it inside of your component. This gives the insurance of having always the same kind of behaviour, no matter the length of your longest String

If I add an extra line to it, it gives the following result

Importe a pagar                  : 50
Descuento                        : 50
Importe                          : 50
Obsequio                         : 50
Adding a very long line to it    : 50
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
1

Those three texts below need another "\t". It happens because the text on the left side in the first row is so long, it overlaps the second column, the next piece (after tab) has to be written in the next "cell".

Andronicus
  • 25,419
  • 17
  • 47
  • 88