aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Linking two JPanels with a JButton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Linking two JPanels with a JButton" Watch "Linking two JPanels with a JButton" New topic
Author

Linking two JPanels with a JButton

Sam Swash
Greenhorn

Joined: Jan 30, 2011
Posts: 6
I would like to know a hint or someone tell me the overall structure of implementing the feature that if a user clicks on a JButton they are redirected to another JPanel.

Thanks,
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38910
    
  23
Welcome to the Ranch

Depends what you mean by "new panel". If you wish to bring a new panel to the top, there are certain kinds of layout which are good for that: this one, for example.

I shall move this thread because we usually discuss GUI-related questions elsewhere.
Sam Swash
Greenhorn

Joined: Jan 30, 2011
Posts: 6
What I mean is that within the same FrameView there are two JPanels, and a button on one JPanel is, on click, redirects the user to the other panel.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38910
    
  23
There are methods which request focus, and you can put the panels into an array and iterate the array, requesting focus on different members of the array in turn. That's one way to do it, and there are doubtless many others . . .

But I am a bit confused by the notion of redirecting "to a panel" when both panels are visible to the user anyway. Why don't you simply click a panel?
Sam Swash
Greenhorn

Joined: Jan 30, 2011
Posts: 6
The first panel has a button "Register" and the second panel is the register form, so when the user clicks on "register" button they are directed to the "register" panel, which is the other panel
Sam Swash
Greenhorn

Joined: Jan 30, 2011
Posts: 6
What i'm asking is similar to use of tabs where a button click on one tab can redirect the user to another tab, but i would like to understand about how a button click on one panel can redirect the user to another panel.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Have you checked the link in Campbell's first post?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sam Swash
Greenhorn

Joined: Jan 30, 2011
Posts: 6
yes i have...and what i'm asking is similiar to use of tabs where in this case someone clicks a button on one panel and they are taken to another panel within the same jFrame.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

And that's just what CardLayout can do for you. But I must admit, I expected a link to the Java tutorial in the Javadoc page of CardLayout. So try http://download.oracle.com/javase/tutorial/uiswing/layout/card.html instead.
Sam Swash
Greenhorn

Joined: Jan 30, 2011
Posts: 6
Thanks for the link,

A great Help!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

You're welcome
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38910
    
  23
Rob Spoor wrote: . . . I expected a link to the Java tutorial in the Javadoc page of CardLayout. . . .
So would I. Most Components etc have a "How to use XXX" link in their documentation. I didn't look for it earlier, however.
 
jQuery in Action, 2nd edition
 
subject: Linking two JPanels with a JButton