-1

What's the best way to define a default for this example so I don't have to define the border and font for each object again.

Border infoLabelMargin = new EmptyBorder(10, 10, 10, 10);

JLabel usernameInfoLabel = new JLabel("Benutzer: " + GlobalValues.USERNAME);
Border usernameInfoLabelBorder = usernameInfoLabel.getBorder();
usernameInfoLabel.setFont(new Font("SANS_SERIF", Font.BOLD, 35));
usernameInfoLabel.setBorder(new CompoundBorder(usernameInfoLabelBorder, infoLabelMargin));
infoBox.add(usernameInfoLabel);

JLabel versionInfoLabel = new JLabel("Version: " + GlobalValues.VERSION_NUMBER);
Border versionInfoLabelBorder = versionInfoLabel.getBorder();
versionInfoLabel.setBorder(new CompoundBorder(versionInfoLabelBorder, infoLabelMargin));
infoBox.add(versionInfoLabel);
RHo
  • 3
  • 3
  • 2
    Create a Factory class/method that will create those `JLabel`s with the same properties? – Laf Feb 26 '16 at 16:08
  • For [example](http://stackoverflow.com/a/10360374/230513). – trashgod Feb 26 '16 at 17:43
  • that's exactly what i looked for, thx – RHo Feb 29 '16 at 09:39
  • 1
    Possible duplicate of [how to create own file with icon that inherit from JFrame icon, that I set it, in java and my own file use FileOutputStream and ObjectOutputStream](http://stackoverflow.com/questions/10359883/how-to-create-own-file-with-icon-that-inherit-from-jframe-icon-that-i-set-it-i) – TylerH Mar 03 '16 at 16:17

1 Answers1

0

Code to create labels

infoBox.add(DefaultLayouts.infoLabel("Benutzer: " + GlobalValues.USERNAME));
infoBox.add(DefaultLayouts.infoLabel("Version: " + GlobalValues.VERSION_NUMBER));

Class that defines the default label

public class DefaultLayouts {
    public static JLabel infoLabel(String text) {
        JLabel label = new JLabel(text);

        Border margin = new EmptyBorder(10, 10, 10, 10);
        Border border = label.getBorder();
        label.setBorder(new CompoundBorder(border, margin));

        return label;
    }
}
RHo
  • 3
  • 3