Open new JPanel (JFrame, ...) by clicking JButton

  • Follow


Hello everybody,

I'm writing a program in Java, in which I want to use more than one
window.
Can anyone tell me how I can open a new JPanel (or perhaps I'd better
choose "extends JFrame"?) by clicking a JButton in the main program?
eg. clicking on "Find" will open a "Find-window", in wich the user can
type and do things (more than a regular aboutbox or so...)
I am a newbie in Java and I've been searching for a whole day on
something that maybe the most of you wil find ridiculous, but I really
can't find or realise it...

I hope someone can help me!

Thanks in advance,
Greetings,
Tari

0
Reply snuffie_456 (12) 1/9/2006 11:25:44 PM

On 2006-01-09, snuffie_456@yahoo.com penned:
> Hello everybody,
>
> I'm writing a program in Java, in which I want to use more than one
> window.  Can anyone tell me how I can open a new JPanel (or perhaps
> I'd better choose "extends JFrame"?) by clicking a JButton in the
> main program?  eg. clicking on "Find" will open a "Find-window", in
> wich the user can type and do things (more than a regular aboutbox
> or so...) I am a newbie in Java and I've been searching for a whole
> day on something that maybe the most of you wil find ridiculous, but
> I really can't find or realise it...
>
> I hope someone can help me!
>
> Thanks in advance, Greetings, Tari

You'll need an ActionListener for the JButton.  In that
ActionListener, you will need to create or show a new JFrame or
similar.

Have you gotten to the point of using ActionListeners yet?

-- 
monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
0
Reply Monique 1/9/2006 11:55:38 PM


Hello,

I tried it for opening the searchFrame by clicking on a button in the
main one, and it works! Thanks for the tip!
Only the closing-thing doesn't work: eg. when I click on OK in the
searchFrame itself, the searchFrame stays opened... Any idea?
Just one more question: is it possible that the above tip only works
for JFrames, but not for JPanels or so?

Greetings,
Tari

0
Reply snuffie_456 1/10/2006 9:13:24 AM

Hello,

I've read some information about ActionListeners on the internet..
I tried to use it with the above tip, and needed to include the
creation of a new JFrame (what you suggested me: thanks!).
Only closing a JFrame seems to be a problem?

Greetings,
Tari

0
Reply snuffie_456 1/10/2006 9:17:04 AM

It is quite easy. You want a toplevel container, i.e a JFrame. So what
you do is that you build both your frames. You show the main one. And
when the user clicks on the button you simply do
searchFrame.setVisible(true);
and then on the ok button (or whatever method you want to use to close
the window) you simply do searchFrame.setVisible(false);

This caused me some problem when I was learning java many years ago,
just like you say there does not seem to be any sites dealing with
this. But once you've realized that what you want to do is to have two
windows (or frames) open it seems so simple so you can't really be
bothered to write a tutorial about it. 
Good luck!

regards
daniel

>Hello everybody,
>
>I'm writing a program in Java, in which I want to use more than one
>window.
>Can anyone tell me how I can open a new JPanel (or perhaps I'd better
>choose "extends JFrame"?) by clicking a JButton in the main program?
>eg. clicking on "Find" will open a "Find-window", in wich the user can
>type and do things (more than a regular aboutbox or so...)
>I am a newbie in Java and I've been searching for a whole day on
>something that maybe the most of you wil find ridiculous, but I really
>can't find or realise it...
>
>I hope someone can help me!
>
>Thanks in advance,
>Greetings,
>Tari

0
Reply Daniel 1/10/2006 9:47:16 AM

On 2006-01-10, snuffie_456@yahoo.com penned:
> Hello,
>
> I tried it for opening the searchFrame by clicking on a button in
> the main one, and it works! Thanks for the tip!  Only the
> closing-thing doesn't work: eg. when I click on OK in the
> searchFrame itself, the searchFrame stays opened... Any idea?  Just
> one more question: is it possible that the above tip only works for
> JFrames, but not for JPanels or so?
>

The OK button's ActionListener needs to do something like
searchFrame.setVisible(false) in its actionPerformed().

JPanels have to live inside something; often that something is a
JFrame.

-- 
monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
0
Reply Monique 1/10/2006 7:15:21 PM

5 Replies
719 Views

(page loaded in 0.09 seconds)

Similiar Articles:













7/21/2012 6:29:37 AM


Reply: