I have a JTabbedPane inside a JFrame. I've been having a lot of problems with resizing and things like that. . how do I get the JTabbledPane to resize with the JFrame? So when the user drags the window open to a larger size, the JTabbedPane should be dragged to the same size. Can this be done automatically?

Recommended Answers

All 7 Replies

What if when you resize the JFrame, you pass the JTabbedPane the width and height through the

resize(int width, int height)

method while resizing the JFrame?

It depends entirely on the layout manager you have set and how you add the JTabbedPane component. It will behave like any other container component in that context.

JTabbedPane tabPane = new JTabbedPane();
tabPane.add("Test 1",new JPanel());
tabPane.add("Test 2",new JPanel());
getContentPane().add(tabPane);
pack();

will create a tabbed pane that fills the frame.

If you are creating that layout in Netbeans, keep in mind that it just decides to use the GroupLayout by default for a new JFrame rather than the old default of FlowLayout, which may throw you off if you aren't expecting it. If that is the case, you can explicitly set the layout manager to whatever you want through the right-click menu in the Inspector.

(I'm just guessing you might be using Netbeans based on prior history. Disregard if that is not the case :) )

What if when you resize the JFrame, you pass the JTabbedPane the width and height through the

resize(int width, int height)

method while resizing the JFrame?

I already thought about this solution, and decided that I did not want to go the route of adding a WindowListener or something, and resizing the JTabbedPane every time the JFrame is resized. Not out of laziness, but out of the complexity of layers that I have.

@ Ezzaral - You are correct, I have been using netbeans lately. The first time I ever asked for help on a project (this was months back) I was coding by hand. Now I switched to netbeans and I've been using it since you suggested using it for UI prototypes. And I'll check out what you said about the layout managers.

FYI, I have the following hierarchy of windows

JFrame -> JTabbedPane -> (JScrollPane->JPanel). So there is a JPanel which has a JScrollPane for it. The JPanel is one of the tabs on the JTabbedPane, which is in a JFrame. I'm trying to get some sort of sizing going that looks nice (and allows me to view most of the JPanel, which is fairly large) but I can't get anything except the JFrame to resize, even when using the setSize method. I thought that maybe the JScrollPane wouldn't resize because the JTabbedPane was too small, and I thought the JTabbedPane wouldn't resize because the JFrame was too small, but I resized the JFrame, and neither of the other two things will resize. I also read the tutorial on resizing scroll panes by setting the preferred size on the client, then using the revalidate method on the client. . still no luck.

:(

I guess questions I have would be the following:
1. Generally speaking, how are things resized? Is it outermost window to innermost window?
2. In the case of the Java tutorials, it said to resize the client of a JScrollPane, then call the revalidate method on the client. (An example of a client could be the JTextArea that the scroll pane is used to scroll through). But for other things, this cannot be the case.. for example, a JPanel does not have a reference to the JFrame it is in, the JFrame has a reference to the JPanel - so only the JFrame can tell the JPanel to resize, not the other way around - right?

Well I have to use the GroupLayout Manager at this point, because I like how it arranges things, generally. After you made that comment, I've been looking through the java tutorial on it, and there is some stuff that might be useful to me. However, I'm still somewhat concerned about setSize not doing anything.

I have:

tab 1 - tab 2 - tab 3 - tab 4

And at runtime, I want to dynamically change tab 1 to be a completely different JPanel. This JPanel is much larger than the one that was originally there. I'm successfully changing the tab's JPanel to another JPanel... but I cannot resize it at all.

Honestly I've yet to use GroupLayout for anything but fixed layouts that I've thrown together recently. For forms that resize predictably, I'm more used to GridBagLayout and tend to stick with that (.. and you kids get off my lawn! *shaking fist* :P )

Well, I figured out that if I set the size of the JFrame before switching the JPanel on the tab, the JTabbedPane resizes itself according to how large the JFrame is. So although I'm still mad that I can't figure out how to setSize explicitly, this is good enough.

PS

I read in an article called Java pitfalls (or something like that) that the layout managers override calls to setSize because they perform their own sizing afterwards, so overriding the setMinimumSize method works better.

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.