I new in Java UI design and I try to use SpringLayout
to create form.
I put JLabel
in JFrame
, added some constraints. I need JLabel
that stratches to JPanel
width. But it has zero height. And too small width.
Here is code fragment:
imageContainer = new JLabel();
imageContainer.setBorder(BorderFactory.createLineBorder(Color.BLACK));
filterButton = new JButton("Filter");
filterButton.addActionListener(filter);
layout.putConstraint(SpringLayout.WEST, imageContainer, 5, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, imageContainer, 5, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.EAST, imageContainer, 5, SpringLayout.EAST, this);
layout.putConstraint(SpringLayout.WEST, filterButton, 5, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, filterButton, 5, SpringLayout.SOUTH, imageContainer);
layout.putConstraint(SpringLayout.SOUTH, filterButton, 5, SpringLayout.SOUTH, this);
And this is the result
Where did I go wrong? How to stretch JLabel
size to parent container?
My problem not in JLablel. I attempted to use this in constraints but really it's not correct way. You need to get container like this:
Container contentPane = this.getContentPane();
After that you can use contentPane in constraints:
layout.putConstraint(SpringLayout.WEST, imageContainer, 5, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, imageContainer, 5, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.EAST, imageContainer, -5, SpringLayout.EAST, contentPane);
layout.putConstraint(SpringLayout.SOUTH, imageContainer, -5, SpringLayout.NORTH, filterButton);
layout.putConstraint(SpringLayout.WEST, filterButton, 5, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.SOUTH, filterButton, -5, SpringLayout.SOUTH, contentPane);
And this work.