0

I am writing SWING GUI for some applications. In my application, I have two fields showing some numbers. This is the current result on my JFrame:

12345678   -12,231

1234       -123.000

However, I want it to be like this:

12345678   -12,231

1234           -123.000

I first calculate the length of the first column and pad the whitespace to make the length I want. But the result is the first one I showed above. It seems that different characters occupy different length when displayed on the JFrame. Any idea about this? Or does it have something to do with the font? Thank you!

4
  • Not sure if Andrew's edit helped. What are you trying to achieve? I'm betting that a combination of layouts is what you need, ... once you're able to articulate your need to us -- or JTable as Andrew suggests. Commented Dec 22, 2013 at 14:52
  • 3
    Use a textual component like a JTable for this! Commented Dec 22, 2013 at 14:52
  • For those fields, users can type any numbers for the two fields and all I have to do is to show those numbers on the frame. Therefore, I have to left justify the second column. My approach now is padding whitespace for the first column so that I can guarantee the second column always start at some fixed position. However, the result is like the first one I showed above instead of the second one. Commented Dec 22, 2013 at 14:59
  • Please show or link to an image showing what you currently have and what you currently desire. Let's see once and for all what we're dealing with here. Commented Dec 22, 2013 at 15:44

2 Answers 2

1

Based on the this image

enter image description here

I would suggest the problem you're having is the fact that the font is a variable width font, meaning that each character has it's own width (so 1 is smaller the 2).

In this case, you would probably be better of using a GridLayout or GridBagLayout

For example...

enter image description here

JFrame frame = new JFrame("Testing");

frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets  = new Insets(4, 4, 4, 4);
gbc.anchor = gbc.WEST;

frame.add(new JLabel("12345678"), gbc);
gbc.gridx++;
frame.add(new JLabel("-12,231"), gbc);

gbc.gridy++;
gbc.gridx = 0;
frame.add(new JLabel("1234"), gbc);
gbc.gridx++;
frame.add(new JLabel("-123.000"), gbc);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

Or if that's just a little too much, you could just try formating the text as HTML...

enter image description here

JFrame frame = new JFrame("Testing");
frame.setLayout(new BorderLayout());

StringBuilder sb = new StringBuilder(128);
sb.append("<html><table>");
sb.append("<tr><td>12345678</td>-12,231<td></tr>");
sb.append("<tr><td>1234</td>-123.000<td></tr>");
sb.append("</table></html>");

frame.add(new JLabel(sb.toString()));

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

Or just use a JTable

Sign up to request clarification or add additional context in comments.

Comments

0

its really easy, take a look:

public static String padRight(String s, int n) {
     return String.format("%1$-" + n + "s", s);  
}

public static String padLeft(String s, int n) {
    return String.format("%1$" + n + "s", s);  
}


public static void main(String args[]) throws Exception {
 System.out.println(padRight("Howto", 20) + "*");
 System.out.println(padLeft("Howto", 20) + "*");
}
/*
  output :
     Howto               *
                    Howto*
*/

3 Comments

No, I have tried this before. This wouldn't work on the JFrame. It still looks like the one I showed above
try again, it creates a string with whitespaces, it will work on cli or gui

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.