How do I anchor JPanel location?

T

Todd

Hello all,

The subject to this may make you think that I have a rather simple
problem - and hopefully it is for some guru out there.

I have created a JTabbedPane into which I place JPanel's to create the
tabs. Each of these JPanel's contains three more panels which are
extensions of JPanel.

The class for the sub-panels provides a JPanel with the ability to be
"collapsed" - the height of the JPanel is adjusted and other objects
within the parent pane are relocated appropriately.

Here is the problem: When I expand one of the collapsed sub-panels in
one tab, the other tabs take on the expanded size of the original tab
(which I really don't want, so if you have ideas how to prevent that,
please feel free to expound), and move their corresponding sub-panels
to the center of the tabbed JPanel. How do I prevent the sub-panels
from centering? If the sub-panels could stay anchored to the upper
left of the parent JPanel, then I would have a way to ensure
consistent layout.

I have tried multiple layout schemes, but none of them maintain the
positioning of the sub-panels (furthermore, only the GridBagLayout
seems to layout my sub-panels appropriately). Here is the code that I
am currently using:


/** Creates a new instance of TrajectoryTabJPanel */
public rajectoryTabJPanel()
{
StateVectorJPanel stateVectorJPanel = new StateVectorJPanel();
PerturbationJPanel perturbationJPanel = new
PerturbationJPanel();
ConstraintJPanel ConstraintJPanel = new ConstraintJPanel();

// Add the subpanels to the subpanel vector
addSubPanel( stateVectorJPanel );
addSubPanel( perturbationJPanel );
addSubPanel( constraintJPanel );

// Define the layout for the subpanels
GridBagLayout layout = new GridBagLayout();
this.setLayout( layout );

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = GridBagConstraints.RELATIVE;

gbc.insets = new Insets( 10, 0, 5, 0 );
this.add( stateVectorJPanel, gbc );

gbc.insets = new Insets( 5, 0, 5, 0 );
this.add( perturbationJPanel, gbc );

gbc.insets = new Insets( 5, 0, 10, 0 );
this.add( deboostConstraintJPanel, gbc );
}

Thanks for any suggestions (beyond SSCP - I always try that before
posting),
Todd
 
D

derek

Try placing a scrollpane around the panel that can collapse.
In other words the scrollpane would contain the panel that can collapse.

http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JScrollPane.html

That way the scrollpane still takes up the space, and the other panels wont take it.
If you set the panel that can collapse to a static width and height and turn off the scrollbars on the scrollpane,
it should appear normal when it is to its full size.
 
T

Todd

Try placing a scrollpane around the panel that can collapse.
In other words the scrollpane would contain the panel that can collapse.

http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JScrollPane.html

That way the scrollpane still takes up the space, and the other panels wont take it.
If you set the panel that can collapse to a static width and height and turn off the scrollbars on the scrollpane,
it should appear normal when it is to its full size.

Derek,

I am afraid that I don't understand. Do you mean that the collapsable
JPanel should have a scrollpane inside of it, or the panel surrounding
the collapsable JPanels?

If you mean the parent (surrounding) panel, are you suggesting that
once the subpanels are fully expanded, the user access the scrollbars
to view the expanded panel(s)?

Todd
 
D

derek

Derek,
I am afraid that I don't understand. Do you mean that the collapsable
JPanel should have a scrollpane inside of it, or the panel surrounding
the collapsable JPanels?
If you mean the parent (surrounding) panel, are you suggesting that
once the subpanels are fully expanded, the user access the scrollbars
to view the expanded panel(s)?
Todd

Well, from your description, i was envisioning something that looked like this:

_______________________________________________
| | |
| | |
| | |
|______________________________________________
| |
| |
| |
| |
|______________________________________________|

Two panels on the top and one on the bottom.
You wanted to the top two to hold their position and
the bottom to "collapse". But the top two would still
stay in the same position?

I was saying you could use a scrollpane to do that.
But, now that i think about it some more. Actually
I would just use a JPanel inside another JPanel.
Then when you want it to "collapse", just hide the
contained JPanel.

For example to build it you would do something like this:

JPanel container = new JPanel();
JPanel collapsable = new JPanel();
container.setLayout(new BorderLayout());
container.add(collapsable, BorderLayout.CENTER);

container is your lower JPanel so you would need to add it in appropriately also.

Then when you need to "collapse" it, you would just call collapsable.setVisible(false);

That would be my first way to try that.

BTW - I'm assuming that "collapse" is the same as "hide".
 
R

Roedy Green

Here is the problem: When I expand one of the collapsed sub-panels in
one tab, the other tabs take on the expanded size of the original tab
(which I really don't want, so if you have ideas how to prevent that,
please feel free to expound), and move their corresponding sub-panels
to the center of the tabbed JPanel. How do I prevent the sub-panels
from centering? If the sub-panels could stay anchored to the upper
left of the parent JPanel, then I would have a way to ensure
consistent layout.

I need some pictures here to understand what you want, but most
usegroup denizens fight tooth and nail to prevent people from using
them to expound.

So I will answer blind.

There are two sorts of things you can do to fudge layouts.

1. lock sizes with setMinimumSize setMaximumSize setPreferredSize.
With AWT you must override the corresponding getMethods.

2. Write your own layout manager that does what you want. This is
MUCH easier than you would imagine. All a layout manager does is
assign positions and sizes to each Component in the Container.
See http://mindprod.com/jgloss/layout.html
Have a look at the source for my "StarLayout" a minimalist layout to
get the general idea.

There are two benefits to writing your own layout manager.
1. you neatly separate business from layout logic making maintenance
easier.
2. you can easily reuse the layout elsewhere without having to
remember how it works.
 
T

Todd

All,

Here is an attempt at an ascii representation of my issue (BTW, if you
know how to post jpgs, I can do screen captures).

There is a panel within each option pane tab. There are three
collapsable subpanels within each panel. The ^ indicates collapsed;
the > indicates expanded.


Nominal screen (Tab 1 selected, Tab 2 identical; all subpanels
collapsed ):

-------/ Tab 1 \ / Tab 2 \-------------------------
| --------------------------------------------- |
| | --------------------------------------- | |
| | | ^ Subpanel 3 | | |
| | --------------------------------------- | |
| | | |
| | --------------------------------------- | |
| | | ^ Subpanel 2 | | |
| | --------------------------------------- | |
| | | |
| | --------------------------------------- | |
| | | ^ Subpanel 3 | | |
| | --------------------------------------- | |
| --------------------------------------------- |
---------------------------------------------------

Problem views:
View 1: Tab 1, Subpanel 1 expanded
-------/ Tab 1 \ / Tab 2 \-------------------------
| --------------------------------------------- |
| | --------------------------------------- | |
| | | > Subpanel 1 | | |
| | | | | |
| | | Any kind of components, in here, | | |
| | | toggles, labels, radio buttons | | |
| | | | | |
| | --------------------------------------- | |
| | | |
| | --------------------------------------- | |
| | | ^ Subpanel 2 | | |
| | --------------------------------------- | |
| | | |
| | --------------------------------------- | |
| | | ^ Subpanel 3 | | |
| | --------------------------------------- | |
| --------------------------------------------- |
---------------------------------------------------

View 2: Tab 2; Tab 1/Subpanel 1 still expanded
-------/ Tab 1 \ / Tab 2 \-------------------------
| --------------------------------------------- |
| | (Parent panel) | |
| | | |
| | | |
| | --------------------------------------- | |
| | | ^ Subpanel 1 | | |
| | --------------------------------------- | |
| | | |
| | --------------------------------------- | |
| | | ^ Subpanel 2 | | |
| | --------------------------------------- | |
| | | |
| | --------------------------------------- | |
| | | ^ Subpanel 3 | | |
| | --------------------------------------- | |
| | | |
| | | |
| | | |
| --------------------------------------------- |
---------------------------------------------------

On tab 2, the subpanels center themselves vertically, with whatever
spacing is defined by the expanded subpanel(s) on the first tab.

As I mentioned above, I would prefer to have each tab maintain it's
own sizing, but am content with just keeping the subpanels aligned
to the top left of the parent JPanel (which is within the
JOptionPane).

Thanks for all of the help,
Todd
 
T

Todd

BTW, I have tried the GridBagConstraints.anchor set to NORTH with no
luck either.

Todd
 
T

Todd

All,

Here is the "solution" that I came up with:


GridBagLayout layout = new GridBagLayout();
this.setLayout( layout );

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = GridBagConstraints.RELATIVE;

gbc.insets = new Insets( 10, 0, 5, 0 );
this.add( targetAltitudeJPanel, gbc );

gbc.insets = new Insets( 5, 0, 5, 0 );
this.add( perturbationJPanel, gbc );

gbc.insets = new Insets( 5, 0, 1, 0 );
this.add( constraintJPanel, gbc );

JPanel padPanel = new JPanel();
padPanel.setOpaque( false );
gbc.weighty = 1;
gbc.insets = new Insets( 0, 0, 0, 0 );
this.add( padPanel, gbc );

By placing a padPanel at the end of the gridbag with a non-zero
weighty, it will absorb all spacing developed in the other tabs. BTW,
according to the tutorial, the tabs will always have the size needed
to contain the tallest and widest element in any of the tabs.

Thanks for the ideas,
Todd
 
R

Roedy Green

Here is an attempt at an ascii representation of my issue (BTW, if you
know how to post jpgs, I can do screen captures).

In Agent you can include attachments, but most participants go berserk
if you use the feature.
see http://mindprod.com/jgloss/forteagent.html

What you pretty well have to do is use a program like FastStone
Capture then post the pngs on a website somewhere and post the URL.
 
R

Roedy Green

There is a panel within each option pane tab. There are three
collapsable subpanels within each panel. The ^ indicates collapsed;
the > indicates expanded.

It looks to me that you could do this my overriding getPreferredSize
on your subpanels so that it gives one size for collapsed and another
for expanded.
 
R

RedGrittyBrick

WTodd said:
There is a panel within each option pane tab. There are three
collapsable subpanels within each panel. > On tab 2, the subpanels center themselves vertically, with whatever
spacing is defined by the expanded subpanel(s) on the first tab.
[snip]

As I mentioned above, I would prefer to have each tab maintain it's
own sizing, but am content with just keeping the subpanels aligned
to the top left of the parent JPanel (which is within the
JOptionPane).

Maybe you should create an SSCCE

Here's one I threw together for you
-----------------------------------8<----------------------------------------
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class Shrinkables {

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Shrinkables();
}
});
}

Shrinkables() {

JTabbedPane tabPane = new JTabbedPane();
tabPane.add("Tab 1", new JScrollPane(new GroupPanel()));
tabPane.add("Tab 2", new JScrollPane(new GroupPanel()));

JFrame f = new JFrame("Layout problem");
f.add(tabPane);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setPreferredSize(new Dimension(500, 300)); // ugh!
f.pack();
f.setVisible(true);
}

class GroupPanel extends JPanel {
GroupPanel() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
add(new ShrinkablePanel("subPanel1", Color.CYAN));
add(new ShrinkablePanel("subPanel2", Color.GREEN));
add(new ShrinkablePanel("subPanel3", Color.YELLOW));
//add(Box.createVerticalGlue());
}
}

class ShrinkablePanel extends JPanel implements ActionListener {
boolean shrunk = true;

JTextArea area = new JTextArea();

String essay =
"Lorem ipsum dolor sit amet, consetetur sadipscing\n"
+ " elitr, sed diam nonumy eirmod tempor invidunt ut\n"
+ "labore et dolore magna aliquyam erat, sed diam\n"
+ "voluptua. At vero eos et accusam et justo duo\n"
+ "dolores et ea rebum. Stet clita kasd gubergren, no\n"
+ "sea takimata sanctus est Lorem ipsum dolor sit amet.\n"
+ "Lorem ipsum dolor sit amet, consetetur sadipscing";

ShrinkablePanel(String name, Color color) {
setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
setBackground(color);
JButton b = new JButton(name);
b.addActionListener(this);
b.setAlignmentY(Component.TOP_ALIGNMENT);
area.setAlignmentY(Component.TOP_ALIGNMENT);
area.setBackground(color);
add(b);
add(area);
}

public void actionPerformed(ActionEvent e) {
if (shrunk) {
area.setText(essay);
} else {
area.setText("");
}
shrunk = !shrunk;
}

} // ShrinkablePanel

} // Shrinkables
------------------------------------- 8< -------------------------------
 
R

Roedy Green

Here is an attempt at an ascii representation of my issue (BTW, if you
know how to post jpgs, I can do screen captures).

I am dense. I need to understand what appearance you are getting vs
what you want.

If you want to freeze the positions of three subpanels at say exactly
1/3 of the space each all the time, You could write an EquispaceLayout
that positions at 1/n vertical units where n is the number of
components.
 
T

Todd

Roedy,

Wow!! You are full of useful information!! I like your solution to a
problem I was making way more difficult than it apparently needed to
be. I will see if I can incorporate your ideas into my existing
framework.

Thanks for all of your help and suggestions,
Todd
 
T

Todd

BTW, I forgot to mention, I tried several Latin translators in an
attempt to understand your "essay." However, none of them performed
very well. If it is your whim, would you mind letting me know what it
means?

Thanks again,
Todd
 
R

RedGrittyBrick

Todd said:
BTW, I forgot to mention, I tried several Latin translators in an
attempt to understand your "essay." However, none of them performed
very well. If it is your whim, would you mind letting me know what it
means?

My newsreader shows the above as posted in reply to Roedy Green but the
content seems to refer to my posting. Assuming the latter is correct ...

http://en.wikipedia.org/wiki/Lorem_ipsum

What I find interesting is that it's meaning and origins were lost for
perhaps hundreds of years, during which time it was widely used, and
passed down, by printers.
 
R

Roedy Green

BTW, I forgot to mention, I tried several Latin translators in an
attempt to understand your "essay." However, none of them performed
very well. If it is your whim, would you mind letting me know what it
means?

I have written thousands of essays. Which do you mean? Both the
layout essays are in English. There are just a few Latin phrases on
the website. Which one did you mean?
 
R

RedGrittyBrick

Roedy said:
I have written thousands of essays. Which do you mean? Both the
layout essays are in English. There are just a few Latin phrases on
the website. Which one did you mean?

Methinks the OP mistook me for you. My SSCCE contained a variable named
"essay" containing Lorem Ipsum.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top