0

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?

Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
  • how about using a decent IDE like netbeans that can help you over that through the gui representation? – Sir. Hedgehog Sep 02 '16 at 13:05
  • @hedgehog I don't like Windows Builder or any other designer to write the code for me. – Water Cooler v2 Sep 02 '16 at 13:06
  • then, Houston we got a problem...... – Sir. Hedgehog Sep 02 '16 at 13:07
  • 3
    Adjust the 'left co-ordinate' using an `EmptyBorder`. Enforce the width by putting the panel in a layout / constraint that respects the width but not the height. 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. – Andrew Thompson Sep 02 '16 at 13:15
  • BTW - the image makes me think a `JList` with an appropriate `ListCellRenderer` would be the best approach. – Andrew Thompson Sep 02 '16 at 13:17
  • @AndrewThompson Thank you. I will give that a try. Right now, I have custom panels inside panels inside panels inside.. with custom event handlers to make those labels look and behave like hyperlinks. It's insane. – Water Cooler v2 Sep 02 '16 at 13:18
  • I feel like a [vertical Box](http://docs.oracle.com/javase/8/docs/api/javax/swing/Box.html#createVerticalBox--) would give you want you want. – VGR Sep 02 '16 at 14:03

2 Answers2

2

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.

Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
0

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.

Pramod CS
  • 45
  • 6
  • Thank you. I just updated the question to put up a picture. – Water Cooler v2 Sep 02 '16 at 13:14
  • 1
    *"Just use `setPreferredSize()` to specify the width and height."* See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) Note that the layout might honor the preferred size, and it might not. – Andrew Thompson Sep 02 '16 at 13:18
  • @AndrewThompson All of the books are in a custom panel named `BookRecommendationsContentPanel`. When I set its `setPreferredSize` like so: `this.setPreferredSize(new Dimension((int)(this.getParent().getWidth() * 0.85), this.getParent().getHeight()));` in the ctor of the `BookRecommendationsContentPanel`, I get a `NullPointerException` for its `this.getParent()`. – Water Cooler v2 Sep 02 '16 at 13:30
  • @WaterCoolerv2 I'll look into it more closely when you post that MCVE as an edit to the question.. – Andrew Thompson Sep 02 '16 at 13:31
  • @AndrewThompson What's MCVE? Sorry, I am a Java newbie. – Water Cooler v2 Sep 02 '16 at 13:32
  • Read all my comments again. Carefully. – Andrew Thompson Sep 02 '16 at 13:32