Pavlo Viazovskyy
Pavlo Viazovskyy

Reputation: 937

Swing layout with resizable and fixed-size labels

I have JPanel with 2 JLabels which need to be placed int a row one after another:

First View

But when the first JLabel contains very long text I want it to be truncated, but show whole second JLabel, like following:

enter image description here

I've tried BorderLayout for this but it makes Second Label Text be always aligned to the right of the JPanel which is not exactly what I need.

Could you please advise the proper way to achive the desired layout?

Upvotes: 0

Views: 99

Answers (1)

camickr
camickr

Reputation: 324118

You can use a horizontal BoxLayout.

The key is to make the minimum size of the first component (0, 0);

Box box = Box.createHorizontalBox();

JLabel longLabel = new JLabel("this has lots fo text");
longLabel.setMinimumSize( new Dimension(0, 0) );
box.add(longLabel);

box.add(Box.createHorizontalStrut(5));

JLabel shortLabel = new JLabel("little text");
box.add(shortLabel);

Upvotes: 2

Related Questions