Java Swing Question

Crusty

Lifer
Sep 30, 2001
12,684
2
81
What is the easiet way to resize all my Components when my JFrame has been resized. Say for instance, the user hits the maximize button and the window maximizes.

I set up a listener for my JFrame getting resized and inside the function I attempt to resize the components I need to scale to the new size.

addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentResized(java.awt.event.ComponentEvent evt) {
formComponentResized(evt);
}
});


jSplitPaneMain.setSize((int)mainPanel.getSize().getWidth(),(int)mainPanel.getSize().getHeight()-100);

is my resize call...but it doesn't change the size of the JSplitPane...what am I doing wrong? Or is there an easier way to do this?
 

Kilrsat

Golden Member
Jul 16, 2001
1,072
0
0
The easier way is to make the layout manager do most of the work for you. Example, if your component is set to be CENTER for a BorderLayout, it will automatically take up as much room as it can.

It may actually be your layout manager that's fiddling with the sizes already, which is why setSize doesn't do what you'd expect.

Since you're using a JSplitPane, this function is rather useful, since it let's you set a relative position for the divider location.

void setDividerLocation(double proportionalLocation)
Sets the divider location as a percentage of the JSplitPane's size.
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Originally posted by: Kilrsat
The easier way is to make the layout manager do most of the work for you. Example, if your component is set to be CENTER for a BorderLayout, it will automatically take up as much room as it can.

It may actually be your layout manager that's fiddling with the sizes already, which is why setSize doesn't do what you'd expect.

Since you're using a JSplitPane, this function is rather useful, since it let's you set a relative position for the divider location.

void setDividerLocation(double proportionalLocation)
Sets the divider location as a percentage of the JSplitPane's size.

Hrmm, well here is how things are setup.

JFrame that has a JMenuBar, and a JTabbedPane. Now in one of my tabs there is JSplitPane that has a horizontal divider. On the top are a few options and whatnot, and on the bottom there is another JSplitPane that is split vertically with a Tree on the left and a Table on the right.

But I will mess with the layout manager and see if I can fix things up :)
 

Kilrsat

Golden Member
Jul 16, 2001
1,072
0
0
Originally posted by: MCrusty
Originally posted by: Kilrsat
The easier way is to make the layout manager do most of the work for you. Example, if your component is set to be CENTER for a BorderLayout, it will automatically take up as much room as it can.

It may actually be your layout manager that's fiddling with the sizes already, which is why setSize doesn't do what you'd expect.

Since you're using a JSplitPane, this function is rather useful, since it let's you set a relative position for the divider location.

void setDividerLocation(double proportionalLocation)
Sets the divider location as a percentage of the JSplitPane's size.

Hrmm, well here is how things are setup.

JFrame that has a JMenuBar, and a JTabbedPane. Now in one of my tabs there is JSplitPane that has a horizontal divider. On the top are a few options and whatnot, and on the bottom there is another JSplitPane that is split vertically with a Tree on the left and a Table on the right.

But I will mess with the layout manager and see if I can fix things up :)
What component isn't resizing the way you expect?

I've worked with something similar before, only my JSplitPane was top and bottom instead of left and right. With no fiddling, the two panels expanded left/right as much as the window resized.

So if the behavior is similar, your components should expand up and down as much as they can already, and I bet if you simply reset the divider location, it'll do what you want.

 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Originally posted by: Kilrsat
Originally posted by: MCrusty
Originally posted by: Kilrsat
The easier way is to make the layout manager do most of the work for you. Example, if your component is set to be CENTER for a BorderLayout, it will automatically take up as much room as it can.

It may actually be your layout manager that's fiddling with the sizes already, which is why setSize doesn't do what you'd expect.

Since you're using a JSplitPane, this function is rather useful, since it let's you set a relative position for the divider location.

void setDividerLocation(double proportionalLocation)
Sets the divider location as a percentage of the JSplitPane's size.

Hrmm, well here is how things are setup.

JFrame that has a JMenuBar, and a JTabbedPane. Now in one of my tabs there is JSplitPane that has a horizontal divider. On the top are a few options and whatnot, and on the bottom there is another JSplitPane that is split vertically with a Tree on the left and a Table on the right.

But I will mess with the layout manager and see if I can fix things up :)
What component isn't resizing the way you expect?

I've worked with something similar before, only my JSplitPane was top and bottom instead of left and right. With no fiddling, the two panels expanded left/right as much as the window resized.

So if the behavior is similar, your components should expand up and down as much as they can already, and I bet if you simply reset the divider location, it'll do what you want.

Well, I was using Absolute Layout :eek:. Once I set it to Border Layout the SplitPanes are resizing to fit the full screen :)

Thx for pointing that out.