0

I've formatted the result of a query to MySql's style, with all the +----+.

In eclipse, the string looks just as I want, but in my GUI, it is messed up: enter image description here

Any idea how can I make the text inside the JTextArea the same as inside eclipse?

Jjang
  • 11,250
  • 11
  • 51
  • 87
  • 2
    Use a monospaced font. http://stackoverflow.com/questions/3747860/style-a-jtextpane-to-have-console-like-formatting – NoHarmDan Aug 03 '15 at 18:02
  • 3
    Use a `JTable` **instead of** the text area.. – Andrew Thompson Aug 03 '15 at 18:03
  • Use a fixed-width font. See: http://stackoverflow.com/questions/16279781/getting-jtextarea-to-display-fixed-width-font-without-antialiasing – SamC Aug 03 '15 at 18:04
  • JTable is nice option, but the problem is not all queries return tables... just the ones which returns ResultSet. I can also get an integer or an exception. Regarding the email, they're fake, you're welcome to use them :) – Jjang Aug 03 '15 at 21:03
  • 1
    1) Tip: Add @Neal4114 (or whoever, the `@` is important) to *notify* the person of a new comment. 2) *"JTable is nice option, but the problem is not all queries return tables..."* Then the solution is a `CardLayout` with a `JTable` in one card and component(s) better suited to the output received in other card(s). – Andrew Thompson Aug 03 '15 at 21:14

1 Answers1

3

The text is not messed up, it's just using a non-monospaced font so not all characters have the same width.

Just do this to your TextArea:

textArea.setFont(new Font("Courier New", Font.PLAIN, 12));
Renato
  • 12,940
  • 3
  • 54
  • 85
  • 3
    *"Just do this to your TextArea:"* 1) As stated in the question, that is a `JTextArea` 2) `new Font("Courier New", Font.PLAIN, 12)` would better be `new Font(Font.MONOSPACED, Font.PLAIN, 12)` for cross-platform reliability & compile time checking. 3) There is a better solution to this, as expressed in my comment.. – Andrew Thompson Aug 03 '15 at 18:07
  • 1
    Calm down a little. This solution is fine and will solve the problem. It will work in any OS I know of. A JTable is NOT a better solution as the question is not how to make a Table but how to make the text look like in Eclipse. – Renato Aug 03 '15 at 18:09
  • why? This is the right solution!!! I said TextArea, but of course JTextArea has a setFont() method. – Renato Aug 03 '15 at 18:12
  • *"..is not how to make a Table but how to make the text look like in Eclipse."* So if the question was *"how do I shoot myself in the foot?"* you would provide instructions? See [Is “Don't do it” a valid answer?](http://meta.stackexchange.com/questions/8891/is-dont-do-it-a-valid-answer) (yes). – Andrew Thompson Aug 03 '15 at 18:13
  • Printing a Table as text is not the same as shooting yourself in the foot. – Renato Aug 03 '15 at 18:15
  • Guys, read what I answered to Andrew, what about integer / exception results? Not all results are tables. if you have any idea, I'm glad to hear... – Jjang Aug 03 '15 at 21:04
  • 1
    Your answer is not wrong, but @AndrewThompson is correct; as suggested [here](http://stackoverflow.com/a/26090878/230513), `Font.MONOSPACED` will elicit the platform's preferred fixed-width font for variations in weight, slope & locale. Renato: Because formatting with spaces is fragile at best, use the strategy pattern, cited [here](http://stackoverflow.com/a/31764798/230513), to select the correct renderer; `JTable` will avoid having to design your own renderer selection scheme. – trashgod Aug 03 '15 at 22:35