LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-08-2006, 02:43 AM   #1
ppanyam
Member
 
Registered: Oct 2004
Location: India
Distribution: Redhat
Posts: 88

Rep: Reputation: 15
Load different JPanel when different Menu Iteks are selected


In a java application, I want to load a different JPanel with different widgets in BorderLayout.Centre of a JFrame.

However, if user selects a differnet Menu Item, the original JPanel has to be removed and in its place another JPanel with different widgets must be displayed and so on. In some cases, the JPanel may display a graphic or a text message only.

Where can I find a suitable article or link for the above exercise?

I am using java version 1.4.2 on Windows and IRIX.

Thanks in advance

ppanyam
 
Old 08-08-2006, 04:20 AM   #2
anupamsr
Member
 
Registered: Sep 2004
Location: India
Distribution: Kubuntu, Archlinux, Suse, Gentoo, Mandrake
Posts: 370

Rep: Reputation: 30
If I am correct, you need to create a parent JPanel and create another JPanel in it. Then you can keep replacing this JPanel. Here is the sample code:
Code:
parentJPanel.removeAll();
parentJPanel.add(newJPanel);
parentJPanel.setVisible(false);
parentJPanel.setVisible(true);
Keep note of last two lines, without which your change will not be visible (repaint() somehow doesnt work).
 
Old 08-08-2006, 07:57 AM   #3
ppanyam
Member
 
Registered: Oct 2004
Location: India
Distribution: Redhat
Posts: 88

Original Poster
Rep: Reputation: 15
Why are we using

Code:
parentJPanel.setVisible(false);
parentJPanel.setVisible(true);
one after the other?
 
Old 08-08-2006, 08:13 AM   #4
anupamsr
Member
 
Registered: Sep 2004
Location: India
Distribution: Kubuntu, Archlinux, Suse, Gentoo, Mandrake
Posts: 370

Rep: Reputation: 30
yeah, because otherwise it wont refresh I found this on some Java forum once because my parentJPanel.repaint() was not giving the expected results. So you force it to repaint by doing the above one after the other.
 
Old 08-08-2006, 08:52 AM   #5
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by anupamsr
yeah, because otherwise it wont refresh I found this on some Java forum once because my parentJPanel.repaint() was not giving the expected results. So you force it to repaint by doing the above one after the other.
thats weird.

the removeAll and add should perform refreshing shouldnt they?
 
Old 08-08-2006, 11:53 PM   #6
anupamsr
Member
 
Registered: Sep 2004
Location: India
Distribution: Kubuntu, Archlinux, Suse, Gentoo, Mandrake
Posts: 370

Rep: Reputation: 30
Yeah, but as I said, it didn't. Then I searched on google and found it posted on some forum that if it doesn't, you can force it this way. So there I go
 
Old 08-09-2006, 02:15 AM   #7
ppanyam
Member
 
Registered: Oct 2004
Location: India
Distribution: Redhat
Posts: 88

Original Poster
Rep: Reputation: 15
Thanks a lot. Let me try it.
 
  


Reply

Tags
java


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
During load got GRUB concolse but no Menu jenshen Linux - Software 2 11-18-2005 10:35 PM
[JAVA] JPanel over JPanel ... not being displayed properly linux_ub Programming 0 08-13-2004 11:21 AM
Perlscript doesn't read selected Pull-down Menu cccc Programming 12 05-27-2004 09:30 AM
(java) mouselistener in JPanel dave bean Programming 6 01-27-2004 12:44 PM
Why cannot i boot win98 with it selected in lilo menu whepin Linux - Newbie 9 12-20-2001 10:01 PM


All times are GMT -5. The time now is 02:32 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration