Edward Guo
Edward Guo

Reputation: 175

JLabel HTML superscript is messing with formatting. Any tricks to get around this?

I am trying to use HTML formatting in order to display a superscript on my JLabel, but it appears to take up more room than the standard font and so all the JLabels below the one with the superscript are pushed down out of place. Each JLabel is supposed to be aligned with a JTextField, which is why this makes it look a little messed up.

My description might be a little convoluted, but the code below can be compiled so you can see what I am talking about. The 5th label is the one with the superscript and causes the rest to be out of line.

import javax.swing.*;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

class JLabelExample extends JFrame {

    private JPanel wrapper, configInputPanel;
    private GridBagConstraints gbc;

    JLabelExample() {
        configInputPanel();
        wrapper();
        add(wrapper);

        pack();
        setVisible(true);
    }

    private void wrapper() {
        wrapper = new JPanel(new GridBagLayout());
        wrapper.setBorder(BorderFactory.createEmptyBorder(25, 25, 20, 25));
        JPanel inner = new JPanel(new GridBagLayout());
        inner.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createTitledBorder("Configuration"),
            BorderFactory.createEmptyBorder(15, 15, 15, 15)));

        gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        inner.add(configInputPanel, gbc);

        gbc.gridx = 0;
        wrapper.add(inner, gbc);
    }

    private void configInputPanel() {
        configInputPanel = new JPanel(new GridBagLayout());
        gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(0, 5, 0, 5);
        gbc.gridx = 0;
        configInputPanel.add(new JLabel("Fuselage Length (m)"), gbc);
        configInputPanel.add(new JLabel("Fuselage Width (m)"), gbc);
        configInputPanel.add(new JLabel("Fuselage Height (m)"), gbc);
        configInputPanel.add(new JLabel("Wing Span (m)"), gbc);
        configInputPanel.add(new JLabel("<html>Wing Area (m<sup>2</sup>)</html>"), gbc);
        configInputPanel.add(new JLabel("Wing Sweep (degree)"), gbc);
        configInputPanel.add(new JLabel("Nose Gear to Fuselage Tip (m)"), gbc);
        configInputPanel.add(new JLabel("Main Gear to Nose Gear (m)"), gbc);
        configInputPanel.add(new JLabel("Main Gear Separation (m)"), gbc);
        configInputPanel.add(new JLabel("Body Gear to Nose Gear (m)"), gbc);
        configInputPanel.add(new JLabel("Body Gear Separation (m)"), gbc);
        configInputPanel.add(new JLabel("Engine to Centerline (m)"), gbc);

        gbc.gridx = 1;
        for (int i = 0; i < 12; i++) {
            JTextField textfield = new JTextField(5);
            textfield.setHorizontalAlignment(JTextField.RIGHT);
            configInputPanel.add(textfield, gbc);
        }
    }

    public static void main(String[] args) {
        new JLabelExample();
    }
}

Upvotes: 3

Views: 239

Answers (1)

user1803551
user1803551

Reputation: 13427

Since you are looking for tricks, I allowed myself to suggest this. It's not quite a superscript, but rather readable as such.

enter image description here

private void configInputPanel() {

    JPanel trickPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 0, 0));
    JLabel label1 = new JLabel("Wing Area (m");
    JLabel supLabel = new JLabel("2");
    Font font = label1.getFont();
    supLabel.setFont(font.deriveFont(font.getSize() / 1.5f));
    trickPanel.add(label1);
    trickPanel.add(supLabel);
    trickPanel.add(new JLabel(")"));

    configInputPanel = new JPanel(new GridBagLayout());
    gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.WEST;
    gbc.insets = new Insets(0, 5, 0, 5);
    gbc.gridx = 0;
    configInputPanel.add(new JLabel("Fuselage Length (m)"), gbc);
    configInputPanel.add(new JLabel("Fuselage Width (m)"), gbc);
    configInputPanel.add(new JLabel("Fuselage Height (m)"), gbc);
    configInputPanel.add(new JLabel("Wing Span (m)"), gbc);
    configInputPanel.add(trickPanel, gbc);
    configInputPanel.add(new JLabel("Wing Sweep (degree)"), gbc);
    configInputPanel.add(new JLabel("Nose Gear to Fuselage Tip (m)"), gbc);
    configInputPanel.add(new JLabel("Main Gear to Nose Gear (m)"), gbc);
    configInputPanel.add(new JLabel("Main Gear Separation (m)"), gbc);
    configInputPanel.add(new JLabel("Body Gear to Nose Gear (m)"), gbc);
    configInputPanel.add(new JLabel("Body Gear Separation (m)"), gbc);
    configInputPanel.add(new JLabel("Engine to Centerline (m)"), gbc);

    gbc.gridx = 1;
    for (int i = 0; i < 12; i++) {
        JTextField textfield = new JTextField(5);
        textfield.setHorizontalAlignment(JTextField.RIGHT);
        configInputPanel.add(textfield, gbc);
    }
}

The gap-less LEADING-aligned FlowLayout gives the impression of a continuous single label. The smaller font size is an approximation for a superscript size. For different fonts it might need an adjustment, but since its derived from its sister label font, it should be reasonable for any case.

Upvotes: 1

Related Questions