Water Cooler v2
Water Cooler v2

Reputation: 33850

How to set just the left coordinates and the width of a JPanel?

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.

enter image description here

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

Answers (2)

Water Cooler v2
Water Cooler v2

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.

enter image description here

Only, now I have some more things to deal with such as:

  1. Word-wrapping the book descriptions;
  2. Making the content panel scrollable; and
  3. Making sure the label height for the book name label is set so it doesn't clip the content.

But these are not the problems I mentioned in the question, so his suggestion solves my problem adequately.

Upvotes: 2

Pramod CS
Pramod CS

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

Related Questions