0

In CSS, there is a letter-spacing property that we can use to increase spacing per letter in a word.

Is there a way I can increase a letter space or text space? I was hoping I can increase the space of the text in a field.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Could you please add some code as well – Saikat Saha Aug 14 '20 at 11:50
  • I used the Netbeans GUI Builder so I just drag and drop instead. It's Java Swing. There's a solution below I'd try. –  Aug 14 '20 at 12:03
  • 4
    *"Is there a way I can increase a letter space or text space in **Java Swing using Netbeans IDE?**"* The GUI is irrelevant. If there's a solution to this, it is in the Java code itself. *"I used the Netbeans GUI Builder so I just drag and drop instead."* ..and this 'excuse for not providing code' is yet another reason not to become so dependent on an IDE. Are you learning Java or learning a software tool? Dump the GUI builder and learn how to code GUI's using .. Java code. This will serve you well in future, and is more likely to get help *right now.* Voting to close for lack of a [mre]. – Andrew Thompson Aug 14 '20 at 12:52

1 Answers1

8

Here is an example:

public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> {
        JLabel label = new JLabel("This is a text.");
        Map<TextAttribute, Object> attributes = new HashMap<TextAttribute, Object>();
        attributes.put(TextAttribute.TRACKING, 0.5);

        label.setFont(label.getFont().deriveFont(attributes));
        JOptionPane.showMessageDialog(null, label);
    });
}

Result:

preview

George Z.
  • 6,643
  • 4
  • 27
  • 47
  • 1
    First time I saw _this_ wonderful solution. It merits adding a small techical explanation, especially as you already added an image. And add a line break in `attributes = ...` – Joop Eggen Aug 14 '20 at 11:57
  • 1
    A link to [the documentation](https://docs.oracle.com/en/java/javase/14/docs/api/java.desktop/java/awt/font/TextAttribute.html#TRACKING) would be useful here. Code dumps with no explanation are not good answers. – VGR Aug 14 '20 at 14:07