Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

How do I anchor JPanel location?

Thread view: 
Todd - 20 Sep 2007 16:41 GMT
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
derek - 20 Sep 2007 17:06 GMT
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.
Todd - 20 Sep 2007 17:44 GMT
> Try placing a scrollpane around the panel that can collapse.
> In other words the scrollpane would contain the panel that can collapse.
[quoted text clipped - 4 lines]
> 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
derek - 20 Sep 2007 18:12 GMT
> Derek,
> I am afraid that I don't understand.  Do you mean that the collapsable
[quoted text clipped - 4 lines]
> to view the expanded panel(s)?
> Todd

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

_______________________________________________
|                         |                    |
|                         |                    |
[quoted text clipped - 5 lines]
|                                              |
|______________________________________________|

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".
Roedy Green - 20 Sep 2007 18:41 GMT
>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
[quoted text clipped - 4 lines]
>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.

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 20 Sep 2007 18:51 GMT
On Thu, 20 Sep 2007 17:41:06 GMT, Roedy Green
<see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted
someone who said :

>See http://mindprod.com/jgloss/layout.html

see also  http://mindprod.com/jgloss/layoutmanager.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Todd - 20 Sep 2007 19:30 GMT
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 \-------------------------
|  ---------------------------------------------  |
|  |  ---------------------------------------  |  |
[quoted text clipped - 9 lines]
|  |  ---------------------------------------  |  |
|  ---------------------------------------------  |
---------------------------------------------------

Problem views:
View 1: Tab 1, Subpanel 1 expanded
-------/ Tab 1 \ / Tab 2 \-------------------------
|  ---------------------------------------------  |
|  |  ---------------------------------------  |  |
[quoted text clipped - 13 lines]
|  |  ---------------------------------------  |  |
|  ---------------------------------------------  |
---------------------------------------------------

View 2: Tab 2; Tab 1/Subpanel 1 still expanded
-------/ Tab 1 \ / Tab 2 \-------------------------
|  ---------------------------------------------  |
|  |  (Parent panel)                           |  |
[quoted text clipped - 15 lines]
|  |                                           |  |
|  ---------------------------------------------  |
---------------------------------------------------

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
Todd - 20 Sep 2007 19:35 GMT
BTW, I have tried the GridBagConstraints.anchor set to NORTH with no
luck either.

Todd
Todd - 20 Sep 2007 20:28 GMT
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
Roedy Green - 20 Sep 2007 20:48 GMT
>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.

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 20 Sep 2007 20:51 GMT
>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.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

RedGrittyBrick - 20 Sep 2007 20:54 GMT
> 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< -------------------------------
Roedy Green - 20 Sep 2007 20:54 GMT
>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.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Todd - 20 Sep 2007 21:21 GMT
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
Todd - 20 Sep 2007 21:24 GMT
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
RedGrittyBrick - 20 Sep 2007 23:13 GMT
> 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.
Roedy Green - 21 Sep 2007 03:08 GMT
>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?
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

RedGrittyBrick - 21 Sep 2007 11:05 GMT
>> BTW, I forgot to mention, I tried several Latin translators in an
>> attempt to understand your "essay."  However, none of them
[quoted text clipped - 4 lines]
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.