The Artima Developer Community
Sponsored Link

Java Answers Forum
How to get GUI panels out of another class?

6 replies on 1 page. Most recent reply: Apr 12, 2005 7:33 AM by Will

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Will

Posts: 20
Nickname: lofty
Registered: Mar, 2005

How to get GUI panels out of another class? Posted: Apr 8, 2005 10:58 AM
Reply to this message Reply
Advertisement
I'm working on a little app that has several Product classes, and a sort of "manager" class. Each product class has a range of GUI panels, eg "Add New Product Panel", and "Edit Product Panel".

What I want to do is call these panels from the "manager" class - so for example, if the user clicks the "edit product" button, the edit product panel is opened up in the main content pane.

I'm having a bit of a battle with it - I want to keep the panels in their corresponding classes, but it seems that doing it that way, my only option is to make each panel a method, so I can call that method from the manager class. That leasds to a host of problems which I won't go into, but I'd appreciate any ideas on a better way to do it.

Thanks


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: How to get GUI panels out of another class? Posted: Apr 11, 2005 12:13 AM
Reply to this message Reply
You're on the right path.

In fact you must find a way to OFFER the panels, either by making them public (bad idea) or to offer a method to get them.

Another nice way:

You can add all panels to a panel with a card layout when creating the main window.
Then you only have to switch the card.

Will

Posts: 20
Nickname: lofty
Registered: Mar, 2005

Re: How to get GUI panels out of another class? Posted: Apr 11, 2005 12:21 AM
Reply to this message Reply
Thanks for the help - I've kind of got it working with the method way, but I think I might try your other idea - I guess the question is which is more elegant / professional?

One other question - as I said I have it kind of working so far, but I've noticed a small problem. Each of the panels has a borderlayout, and the center panel of each panel is made up of a gridbag layout. When I run the app, I can switch between panels about 3 times before the center panel seems to revert to a flow layout. I'm using repaint() and validate() every time I switch panels, but for some reaon after about 3 switches it stops working.

I'm sure this is a problem with my code, but I've noticed a couple of similar problems when using GridBagLayout before - any ideas would be appreciated :)

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: How to get GUI panels out of another class? Posted: Apr 11, 2005 7:15 AM
Reply to this message Reply
That's new ...

Do you remove the unuse panels? I can only assume that not doing it can cause a problem like this one.

However, using a CardLayout you won't have this problem.

I'm using card layouts with up to 8 cards and it just works great.

Will

Posts: 20
Nickname: lofty
Registered: Mar, 2005

Re: How to get GUI panels out of another class? Posted: Apr 11, 2005 7:35 AM
Reply to this message Reply
Yes, I'm removing the panels, but I'm using the removeAll() method - I've heard people say that you should really remove all the individual panels instead of using this blanket method, but I can't do that without knowing what the existing panel is.

So it looks like Card Layout might be the one. Thanks again.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: How to get GUI panels out of another class? Posted: Apr 12, 2005 5:52 AM
Reply to this message Reply
Allways use a JPanel variable to store the shown panel, if you do it by hand.

So, the showPanel method would look like:
public void showPanel (JPanel p) {
    if (currentPanel != null)
        target.remove(currentPanel);
    target. add(p, parameters);
    currentPanel = p;
}


This way you don't have to create a Panel for holding another Panel.

Will

Posts: 20
Nickname: lofty
Registered: Mar, 2005

Re: How to get GUI panels out of another class? Posted: Apr 12, 2005 7:33 AM
Reply to this message Reply
Yes, I've got similar code now, though I didn't have it when I first posted. I think the problem must be my components or something - I'll get there eventually :)

Flat View: This topic has 6 replies on 1 page
Topic: My JSlider does not work Previous Topic   Next Topic Topic: I need help with this problem...please

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use