I’d like to create a JOptionPane that allows a user to select an IP address and port to connect to. It should be structured as such,
IP Address: [textfield here]
Port: [textfield here]
Cancel OK
The labels should be aligned to the left, and the textfields should be left aligned too. I can’t really model the storyboard here, but basically the textfields shouldn’t be misaligned, even if there is a space between the labels and the textfields.
Each label and textfield pair should be on seperate lines, and the Cancel OK buttons should be aligned to the right, below the textboxes.
Is there any way to do this in code?
See this example that seems similar in layout.
The example
Right align
Right aligns the text in the labels, which I think looks better, using this:
For left aligned text, change it to:
Improvement
It is done using a nested layout,
GridLayoutinstances in theWESTandCENTERof aBorderLayout.It might be better done using a more powerful layout such as
MigLayoutor more modern J2SE layout such asBoxLayoutorGroupLayout.GroupLayoutcan provide the type of alignment this UI needs, while not stretching theCENTERfields to the same width (which is also fixable in a nested layout as above, but requires 2 more constraining panels). I believe the other two could do the job as well, but don’t have as much experience with them.