Setting a fixed width and a changing height for a component within a panel

Alright guys here's my next question. I'm now having trouble trying to figure out how to only resize the height of a JTabbedPane yet keep the width fixed. In other words, as the JPanel its in changes in size, I'd like to have the width of my tabbed pane stay a certain value yet let the height of it adjust to the panel's height as it changes. Is there a way to somehow extract the panel's height into a int or something and then when using the setPreferredSize() function simply implement this retrieved value into the function's height parameter? I've tried messing around with Dimensions and getHeight() functions and the sort but to no success. For instance, within the constructor of one of my JFrames using System.out.println(this.getHeight()); prints 0, when of course this is not the case in reality for the frame's height. The panel's changing size is due to the user resizing the window its in. In one of my previous threads in this forum I learned how to use a GridLayout (it was 2 rows and 1 column in size) to keep the height of the top area in a BorderLayout fixed, yet as the user resized the window, the layout would stretch the area width-wise accordingly. However, that was because I had 2 cells in the layout to control, and I know this wouldn't work with only 1. Any ideas?

[1310 byte] By [Jason102a] at [2007-10-3 4:05:52]
# 1

You still apparently haven't read the tutorial on using Layout Managers to understand the capabilites of each Layout Manager.

> For instance, within the constructor of one of my JFrames using

> System.out.println(this.getHeight()); prints 0, when of course this is not the case in reality for the frame's height

Components don't have a size until the GUI is visible on the screen. That is until you've done a pack(), or a setVisible( true ). You should never be using setSize().

Layout Managers use setPreferredSize() ,setMinimumSize() and setMaximumSize() as suggestions for determining the layout. Although most layout managers don't use all the suggestions, which is why you need to read the tutorial to find out how the layout managers work.

> I'm now having trouble trying to figure out how to only resize the height

> of a JTabbedPane yet keep the width fixed

Well, I don't understand the requirement. The preferred size of a tabbed pane is equal to the preferred size of the largest tab added to the tabbed pane. So I don't understand the concept of artificially changing the vertical height.

Anyway reread the tutorial on "How to Use the Border Layout". Two of the 5 areas respect the width of the component but alter the height to fill all the available space.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

camickra at 2007-7-14 22:05:15 > top of Java-index,Desktop,Core GUI APIs...
# 2

Ugh, sorry camickr. The problem is that this is an idea in my head, and that it's not a compile or run-time error with code I can offer you. Let me try this: I've made a prototype with a image editing program to help me create this: http://www.file.sc/cb1a9a/Directory View.jpg See the tabbed pane on the left? That's what I'm talking about. I'd like it's width to stay the same but have the ability to change height-wise when the user resizes the window. I don't know what else to say. See why I can't use BorderLayout? There's 4 distinct main areas, and the center portion of the layout messes it all up. Do you sort of get what I'm asking now?

It looks like the link doesn't light up all the way so you're going to have to paste it in the address bar. Then your going to have to click the download button and then you can view the picture.

Jason102a at 2007-7-14 22:05:15 > top of Java-index,Desktop,Core GUI APIs...
# 3

> It looks like the link doesn't light up all the way

That would be because you have a space in the file name.

> See why I can't use BorderLayout?

No. You have a panel you add to the North. You tabbed pane goes in the West. Your table goes in the Center. Finally you have your button panel in the South.

camickra at 2007-7-14 22:05:15 > top of Java-index,Desktop,Core GUI APIs...
# 4

Ah, so I don't need to have all 5 areas! That to me answers a ton of questions. As for the file name I knew that. You need the space to get to the file hosting site.

I can see how many people could get frustrated with you and the other experts with your somewhat aggressively short replies and then tend to post back equally frustrated and aggressively. I'm not going to do that. camickr, please know that what you are doing is a great service to the community and in an indirect way we all appreciate it in the end. I can see how it could get dreadfully tedious having to explain in depth to everyone how to do this or that and why you guys respond in this way. None of us are perfect. Thanks for helping one of those imperfect people. ;-)

Jason102a at 2007-7-14 22:05:15 > top of Java-index,Desktop,Core GUI APIs...
# 5

> Ah, so I don't need to have all 5 areas! That to me answers a ton of questions.

This is where I get frustrated. You assumed something didn't work before even trying it.

Thats why I always ask for a simple demo program. It shows that you have tried it.

If you tried creating a demo program and adding 4 components to it you would have had one of two outcomes:

a) it worked, in which case there would be no need to post a question

b) it didn't work, in which case you have a demo program to post on the forum so we can see what you did and it would allow us to make suggestions to fix the problem.

camickra at 2007-7-14 22:05:15 > top of Java-index,Desktop,Core GUI APIs...