DejanLekic
DejanLekic

Reputation: 19787

What is the correct way of getting the default JLabel dimensions?

In order to solve a problem I am currently working on, I need to find out what is the default height of a JLabel. I am using a dummy approach:

JLabel label = new JLabel();
Dimension dim = label.getPreferredSize();

Is there a way to get the default height without instantiating JLabel?

Upvotes: 1

Views: 457

Answers (1)

mKorbel
mKorbel

Reputation: 109815

I need to find out what is the default height of a JLabel. I am using a dummy approach:

JLabel label = new JLabel();
Dimension dim = label.getPreferredSize();
  • is possible but you have to accept that this is done by LayoutManager, then there are two options

    1. Swing/AWT GUi is already visible on the screen

    2. after JFrame.pack() is called

  • (I don't suggest, just for wroting a complete answer) is possible to get getPreferredSize for most of JComponents by using/invoke

    1. NullLayout by using Insets

    2. revalidate() and repaint() in already visible Swing/AWT GUI

Is there a way to get the default height without instantiating JLabel?

  • without incorectly settting by setSize, setPreferredSize, setBounds only by override getPreferredSize

Upvotes: 3

Related Questions