I have a GUI window with a JFrame. On this JFrame, I have a JPanel that completely covers the JFrame. I then have another JPanel within this first JPanel that contains a text box and 3 buttons. Within an event handler, I want to expand the JFrame and 2 JPanels by a height of 20 px. I then want to move down the text box and 3 buttons, 20 px.

I can expand the JFrame and the first JPanel with the following code snip. But, it doesn't work on the second panel nor does it work to move the button down.

Appreciate your help. Thanks..

// Make room for an additional text box
        int heightInc = 20;
        
        Dimension frameSize = new Dimension();
        Rectangle rectangle = new Rectangle();

        //  Adjust JFrame size (This Works)
        frameSize = this.getSize();
        frameSize.height += heightInc;
        this.setSize(frameSize);

        //  Adjust the bounding JPanel size (This Works)
        frameSize = jplFrame.getSize();
        frameSize.height += heightInc;
        jplFrame.setPreferredSize(frameSize);

        //  Adjust the AddSubtitle JPanel size  (This doesn't work)
        frameSize = jplAddSubtitle.getSize();
        frameSize.height += heightInc;
        jplAddSubtitle.setPreferredSize(frameSize);

        //  Move the "Add Another" button down (This doesn't work)
        rectangle = btnAddAnother.getBounds();
        rectangle.y += heightInc;
        btnAddAnother.setBounds(rectangle);

What layout manager are you using?

The default layout for JPanel is FlowLayout witch puts components in a row, If you like to set the element position and size you should use another layout like null (for absolute positioning)

I am not using any explict layout manager. I'm using the "free flow" or whatever netbeans call it when you simply move components onto the panel.

Does this help you on giving me some help?

Thanks for the response.

Ok then, netbeans uses free design witch actually is GroupLayout if you take a look in the generated code for components initialization.
You could change the layout to Absolute for the both Panels and then use:

//  Adjust the AddSubtitle JPanel size 
rectangle = jplAddSubtitle.getBounds();
rectangle.height += heightInc;
jplAddSubtitle.setBounds(rectangle);
//  Move the "Add Another" button down
rectangle = btnAddAnother.getBounds();
rectangle.y += heightInc;
btnAddAnother.setBounds(rectangle);

Thank you for your response. I guess I'm too much of newbie and novice to change the layout to Absolute. I have "struggled" for couple of hours trying to get something to work. No success.

So, if you would, I'd greatly appreciate a little detail on how I can do as you suggests. Thank you....

Did you managed to set the layout to absolute for the panels?

What exactly it's not working?

After setting the layout(you might need to resize and reposition the components after that) you just have to replace your code that doesn't work with the one I've posted.

I was unable to set the layout to Absolute. I just don't know how to do it. I'm too new at the Java stuff. I ended up realizing I had no idea how to proceed. Do I delete everything I have layout with NetBeans "Free Design" and start over specifying Absolute layout? Or, can I insert some code that will over ride the GrouplLayout with the Absolute Layout. You can see, I really don't understand the mechanism of using any of the layout managers other then the "Free Design.: Maybe in a couple of months, I be "smarter."

So, I took the lazy way out and changed my design so I don't need to expand the dialog.

In your Design view, just select the Panel you want to set the layout for then right-click on it -> select Set Layout -> Absolute Layout (or any other layout you want to use).

Read this A Visual Guide to Layout Managers, I'm sure It will help you to get started

GL with your programming

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.