In HTML and CSS, if there's an input type=text, if we used padding-left:10px; for example, the text will be written 10px away from the left border, how can I do this in java?
Asked
Active
Viewed 657 times
0
-
2Do you mean in Java Swing? It doesn't use CSS, of course. – duffymo May 22 '12 at 15:09
-
Empty border : http://stackoverflow.com/questions/5328405/jpanel-padding-in-java – Michael Laffargue May 22 '12 at 15:10
-
I think you should be more specific. – Jeel Shah May 22 '12 at 15:14
2 Answers
0
For console output you can System.out.printf
like this to get left padding:
System.out.printf("%10s%s%n", "", someStr);

anubhava
- 761,203
- 64
- 569
- 643
0
In Swing, you can create an empty border. Here is an example of code adding such padding to a textfield:
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
public class Main {
private JFrame frame;
private JTextField t;
protected void initUI() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t = new JTextField();
Border padding = BorderFactory.createEmptyBorder(0, 10, 0, 0);
if (t.getBorder() != null) {
t.setBorder(BorderFactory.createCompoundBorder(t.getBorder(), padding));
} else {
t.setBorder(padding);
}
frame.add(t);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Main().initUI();
}
});
}
}

Guillaume Polet
- 47,259
- 4
- 83
- 117