0

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?

Ali Bassam
  • 9,293
  • 22
  • 65
  • 112

2 Answers2

0

For console output you can System.out.printf like this to get left padding:

System.out.printf("%10s%s%n", "", someStr);
anubhava
  • 713,503
  • 59
  • 514
  • 593
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
  • 46,469
  • 4
  • 81
  • 115