Skip to content
Advertisement

How to align multiple lines of JOptionPane output dialog messages by “:”?

I need to make the output in 3 lines and also aligned by “:”, using JOptionPane.showdialogmessage(): *note the alignment of the “:”

JavaScript

I have tried manually adding spaces before each “:” but still these 3 lines do not strictly align:

JavaScript

Here is the output screenshot:

enter image description here

What is the easiest way to realize this alignment? Thanks

P.S.: This is my first post. Sorry, I am still struggling with how to edit the post in a way that shows you the correct format of the output…I have been driven crazy by formatting… so I just post the screenshots instead… lol

Advertisement

Answer

Avoid using monospaced fonts, or strange tricks.
Construct a JPanel which uses a GridBagLayout, and remember showMessageDialog accepts other Components, not only text.

enter image description here

Relevant code. I opted for a separated label for : so that you can customize it as you prefer, while still keeping it aligned.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement