472,796 Members | 1,566 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,796 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 12790
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.