Fix the height of a JPanel, but not the width
I know I can set and hold the overall size of a JPanel...
But I want to set just the height.
I'm putting two tables on a panel, and I want the upper table to stay a fixed height that I specify, and I want the lower table to vary with the size of the frame. I want the width of both tables to vary with the width of the Frame.
Is the only way to do this to extend JPanel and override the sizing?
[420 byte] By [
jneaua] at [2007-11-27 8:36:08]

# 1
Use a BorderLayout. Add the first table to the North and the second to the Center.
# 2
I am using a BorderLayout, but I need to do more.Problem with that is that the top table takes up *way* too much room...NORTH gives it all it wants. I want it to be a lot shorter, but have the width still vary dynamically.
jneaua at 2007-7-12 20:32:57 >

# 3
Done. Here's the code:
package jneau.swingExtensions;
import java.awt.Dimension;
import java.awt.LayoutManager;
import javax.swing.JPanel;
public class FixedHeightJPanel extends JPanel {
public static final long serialVersionUID = 0;
int preferredHeight = 400;
public FixedHeightJPanel () {
super ();
}
public FixedHeightJPanel (boolean isDoubleBuffered) {
super (isDoubleBuffered);
}
public FixedHeightJPanel (LayoutManager layout) {
super (layout);
}
public FixedHeightJPanel (LayoutManager layout, boolean isDoubleBuffered) {
super (layout, isDoubleBuffered);
}
public int getPreferredHeight () {
return preferredHeight;
}
public void setPreferredHeight(int preferredHeight) {
this.preferredHeight = preferredHeight;
}
public Dimension getPreferredSize () {
return new Dimension(super.getPreferredSize().width, this.preferredHeight);
}
}
jneaua at 2007-7-12 20:32:57 >

# 4
> NORTH gives it all it wantsNo it doesn't. The height is determined by the preferred height of the component you add to the panel.The width will be set to the maximum width of the parent container.So this is exactly what you want, without writing any custom code.
# 5
Dude,
"NORTH gives it all it wants" = "The height is determined by the preferred height". NORTH gives it (the contained component) all (height) it (the contained component) wants.
And thus the problem, how do you set the preferred height of a JPanel (or a JTable, or a JScrollPane)? Components have a "setPreferredSize()" method, but not a "setPreferredHeight".
I've got my solution...override JPanel.
I'm interested in your solution though. Show me an example of a JPanel that contains a JTable and stays set at a height of 100, without any custom code.
Message was edited by:
jneau
jneaua at 2007-7-12 20:32:57 >

# 6
> Show me an example of a JPanel that contains a JTable and stays set at a height of 100
Well, usually you use a scroll pane for a table or list so you can control the amount of space that is taken up by the component. (ie. a table of 1,000 rows would never fit on your screen). So you add the table to a scroll pane. Then you assign a preferred size to the scroll pane (the width is really irrelevant because it will change as the frame is resized, but at least you get a starting width for the pack() method). Then you add the scroll pane to the North. The entire demo program would be:
import java.awt.*;
import javax.swing.*;
public class Test
{
public static void main(String args[])
{
JTable table = new JTable(300, 10);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setPreferredSize( new Dimension(500, 100) );
JFrame frame = new JFrame();
frame.getContentPane().add(scrollPane, BorderLayout.NORTH);
frame.pack();
frame.setVisible( true );
}
}
Now add a second table to the center of the content pane and see how the height varies with the frame height.
Learn how to create simple demo programs and try things before saying it won't work and my name is not Dude.
# 7
Thanks, Dude.
And I swear I've tried this before, but I must've been doing something else wrong (Like embedding the component in an inner JPanel on the west or something stupid).
BTW, I do actually try things before posting questions...and I also search for solutions. This isn't the first time a questions like this one has been asked, and in the threads I've found nobody offered up a solution other than extending JTable or JPanel.
jneaua at 2007-7-12 20:32:57 >

# 8
> Thanks, Dude.
You really don't want help in the future do you? Did you read my last comment above?
> BTW, I do actually try things before posting questions...
Then post your code so we can see what you are doing. A verbal description can't describe exactly what you are doing. Maybe we can spot the mistake. Its called a SSCCE:
see http://homepage1.nifty.com/algafield/sscce.html,
# 9
Look,
I realize you are the self-appointed forum cop.
Not every question warrants someone boiling their application down to a 10-line program for your evaluation.
Sometimes, people feel like they can explain it well enough for people to understand without having to see code.
And sometimes, as in this case, people try to think about how to do something before coding it. I can't provide code for something that I'm not sure how to do.
In this case, you were able to understand my question and you provided an answer. After your post, I tried it and it worked. No need to post a SSCCE.
Sorry to offend.
jneaua at 2007-7-12 20:32:57 >

# 10
> Sometimes, people feel like they can explain it well enough for people to understand without having to see code.
Obviously that is not true. Twice I told you how to solve the problem. Twice you replied stating you didn't understand and that the better way was to create a custom panel. It was not until I posted the simple example that you understood.
So don't assume that everybody understands the question. I made the mistake of assuming that you would understand my simple suggestion and you didn't. So I ended up wasting time with multiple responses to the posting when the problem should have been resolved the first time.
The forum is not about making life easy for you. Its about giving us the necessary information we need to solve a problem. Its about making our life easier, since we help others on the forum, not just yourself.
Yes sometimes a verbal description will do. But a simple 10 line program removes all doubt about what you are trying to do and gives us a better chance to help you. If you can't take the 10 minutes it takes to create a SSCCE, then I guess I won't take the 10 minutes in the future to guess exactly what you are talking about.
# 11
Listen Jasper,
I don't need code to understand you when you say, "Use a BorderLayout. Add the first table to the North and the second to the Center."
I simply thought I had tried it in the past and it didn't work. And when I looked this up in the past, people said, "Extend and override". I never said I didn't understand you.
jneaua at 2007-7-12 20:32:57 >

# 12
> I don't need code to understand you when you say, "Use a
> BorderLayout. Add the first table to the North and the second to the Center."
Apparently you do. Your reply was:
I'm interested in your solution though. Show me an example of a JPanel that contains a JTable and stays set at a height of 100, without any custom code.
I've been answering question a bit longer than you have been asking them. Did you ever think that maybe I know a little about what I'm talking about. So learn from the advice given and move on.