Reputation: 33850
I'd like to set the left-co-ordinates and the width of a panel that's inside another panel.
innerPanel.setBounds(/* expects a rectangle that
specifies y and height, which I would not like to set */);
setBounds
expects me to specify the y
coordinate and the height
as well, which I don't want to set because I'd like to stack things up inside this panel vertically, like a Stack Layout
(an old layout that was present in VJ++, the Microsoft copy of Java) or a GridLayout(0, 1)
.
See the picture below please.
Each of those books are a panel inside a larger panel. I want them to have some space on the left and some on the right and then I want to word-wrap the descriptions.
How do I set just the left coordinate and the width without touching the other two variables?
Upvotes: 0
Views: 197
Reputation: 33850
As per Andrew Thompson's suggestion, I created an empty border around the panel that contains all the books and I've solved the problem of defining a padding or margin around the edges of the container.
public class BookRecommendationsContentPanel extends JPanel {
public BookRecommendationsContentPanel(JLabel lblStatus) {
super();
...
this.setBorder(new EmptyBorder(20, 20, 10, 20));
}
public void AddBook(Book book) {
JPanel pnlBook = new JPanel();
...
this.add(pnlBook);
}
}
It now looks a lot better.
Only, now I have some more things to deal with such as:
But these are not the problems I mentioned in the question, so his suggestion solves my problem adequately.
Upvotes: 2
Reputation: 45
If you are using any layout manager, setBounds()
has no effect anyway. Just use setPreferredSize()
to specify the width and height.
There is a good chance I have not understood your requirement fully. It would help if you put a picture of how you want the components to be displayed and then I could suggest the best layout to achieve that.
Upvotes: 0