473,405 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Replacing Components in a JPanel

I have a JPanel in a window and I am putting different components in it as
the user goes through a setup wizard. In theory, I have an array of
components (other JPanels), and I want to step through them, displaying
panel_0, then panel_1....

I have no problem removing panel_0 from the JPanel parent (I've even done a
repaint() right after removing to be sure of this), but no matter what I
do, I can't seem to do an "add(panel_1)" to the sampe panel. It's the same
method I'm using to add the first panel, before I show() the window, but
once I remove a component and try to add another in its place, I get a
blank.

Is there a reason I can't add components? Is it because the window is
visible? What do I need to do?

Thanks!

Hal
Jul 17 '05 #1
6 12836
You can try "invalidate()" on the container. Adding of a component doesn't
always trigger the layout manager to relayout the container and by
invalidating the container the layout manager will be triggered to do
relayout. The first time the layout manager will be triggered by the
show().
Stellah
"Hal Vaughan" <ha*@thresholddigital.com> schreef in bericht
news:NQhjb.787330$uu5.137635@sccrnsc04...
I have a JPanel in a window and I am putting different components in it as
the user goes through a setup wizard. In theory, I have an array of
components (other JPanels), and I want to step through them, displaying
panel_0, then panel_1....

I have no problem removing panel_0 from the JPanel parent (I've even done a repaint() right after removing to be sure of this), but no matter what I
do, I can't seem to do an "add(panel_1)" to the sampe panel. It's the same method I'm using to add the first panel, before I show() the window, but
once I remove a component and try to add another in its place, I get a
blank.

Is there a reason I can't add components? Is it because the window is
visible? What do I need to do?

Thanks!

Hal

Jul 17 '05 #2
Hal Vaughan wrote:
I have a JPanel in a window and I am putting different components in it as
the user goes through a setup wizard. In theory, I have an array of
components (other JPanels), and I want to step through them, displaying
panel_0, then panel_1....

I have no problem removing panel_0 from the JPanel parent (I've even done a
repaint() right after removing to be sure of this), but no matter what I
do, I can't seem to do an "add(panel_1)" to the sampe panel. It's the same
method I'm using to add the first panel, before I show() the window, but
once I remove a component and try to add another in its place, I get a
blank.

Is there a reason I can't add components? Is it because the window is
visible? What do I need to do?


Hal,

I don't see any reason a priori your method doesn't work. However, I
would recommend using the CardLayout instead. This is exactly what it
was designed for.

HTH,
Ray

Jul 17 '05 #3
Raymond DeCampo wrote:
Hal Vaughan wrote:
I have a JPanel in a window and I am putting different components in it
as
the user goes through a setup wizard. In theory, I have an array of
components (other JPanels), and I want to step through them, displaying
panel_0, then panel_1....

I have no problem removing panel_0 from the JPanel parent (I've even done
a repaint() right after removing to be sure of this), but no matter what
I
do, I can't seem to do an "add(panel_1)" to the sampe panel. It's the
same method I'm using to add the first panel, before I show() the window,
but once I remove a component and try to add another in its place, I get
a blank.

Is there a reason I can't add components? Is it because the window is
visible? What do I need to do?

Hal,

I don't see any reason a priori your method doesn't work. However, I
would recommend using the CardLayout instead. This is exactly what it
was designed for.


Works perfectly. One of the problems of being self-taught is that I seem to
have missed out on a LOT of the Java API and features. Often I don't even
know where to look, what to look for, or if what I want to do can be done
in a much simpler way.

Thanks!

Hal
HTH,
Ray


Jul 17 '05 #4
Stella wrote:
You can try "invalidate()" on the container. Adding of a component doesn't
always trigger the layout manager to relayout the container and by
invalidating the container the layout manager will be triggered to do
relayout. The first time the layout manager will be triggered by the
show().
Stellah
Tried it. I have no idea why, but it didn't work. Someone below suggested
using a CardLayout (new to me -- which isn't a surprise).

Thanks for the idea. I didn't know about invalidate() and will definately
keep it in mind for similar situations in the future.

Hal

"Hal Vaughan" <ha*@thresholddigital.com> schreef in bericht
news:NQhjb.787330$uu5.137635@sccrnsc04...
I have a JPanel in a window and I am putting different components in it
as
the user goes through a setup wizard. In theory, I have an array of
components (other JPanels), and I want to step through them, displaying
panel_0, then panel_1....

I have no problem removing panel_0 from the JPanel parent (I've even done

a
repaint() right after removing to be sure of this), but no matter what I
do, I can't seem to do an "add(panel_1)" to the sampe panel. It's the

same
method I'm using to add the first panel, before I show() the window, but
once I remove a component and try to add another in its place, I get a
blank.

Is there a reason I can't add components? Is it because the window is
visible? What do I need to do?

Thanks!

Hal


Jul 17 '05 #5
Hal Vaughan wrote:

Works perfectly. One of the problems of being self-taught is that I seem to
have missed out on a LOT of the Java API and features. Often I don't even
know where to look, what to look for, or if what I want to do can be done
in a much simpler way.


I'm glad that worked out for you.

If you are looking for a means of getting more familar with the Java
API, I recommend the Java Tutorial from Sun (http://java.sun.com). All
of the material is available free online, or, if you are a big spender,
is available in book format as well. I think also, if you do not have a
perpetual connection, the tutorial is available for download.

Ray

Jul 17 '05 #6
Raymond DeCampo wrote:
Hal Vaughan wrote:

Works perfectly. One of the problems of being self-taught is that I seem
to
have missed out on a LOT of the Java API and features. Often I don't
even know where to look, what to look for, or if what I want to do can be
done in a much simpler way.


I'm glad that worked out for you.

If you are looking for a means of getting more familar with the Java
API, I recommend the Java Tutorial from Sun (http://java.sun.com). All
of the material is available free online, or, if you are a big spender,
is available in book format as well. I think also, if you do not have a
perpetual connection, the tutorial is available for download.

Ray


Cool! That's the kind of thing I never hear about unless I stumble on it in
a search.

Thanks!

Hal
Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Fereshteh Shojaei | last post by:
Hi, I wonder if somebody could help me. I would like to move the cursor from one JTextField to another one when I press <CR>. Regards, Fereshteh
7
by: sreenulanka | last post by:
please help me in my forum i have labels and textareas .iwant to add textarea components dyanamically when i am clicking the button.please help me import java.awt.*; import javax.swing.*;...
6
by: davedwm | last post by:
I'm trying to display a grid of TextPane's that are sized to fit a JPanel i've drawn into a Dialog class, the number of 'Cells' are entered into JTextFields and when the appropriate button is hit...
5
by: James Barrett | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi all, I am experimenting with JApplet and JPanel. My JApplet contains a JPanel called jpanel1 size 210x210. I created a class myPanel which...
1
by: rpm27 | last post by:
I'm new to Java. I am starting to understand Java GUI dimension (the swing library), and I need someone to clarify something to me: As far as I know, JFrame objects provide a window (with the...
3
by: gaya3 | last post by:
Hi, I'm having a small doubt in swings.pl consider two java classes. First java class: JFrame -> frame1 and when some action is performed it calls the method in 2nd java function with...
8
by: eddiewould | last post by:
Hi, I want to write a custom widget which will act similarly to a JPanel (i.e it can contain other Components), but semantically it's not a kind of JPanel so it shouldn't extend from it. Here is...
1
by: Akino877 | last post by:
Hello, I have a question regarding Java and Swing programming I wonder if I could ask the forum for some help. I have a JPanel that has a couple of radio buttons and an "OK/Next" button on it. ...
2
by: chanshaw | last post by:
Alright I'm trying to place the textarea on top of the image but right now it displays the textarea below the image, how do i go about doing this. import java.awt.Color; import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.