Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Java

Reply
 
LinkBack Thread Tools Display Modes
Old 04-07-2003, 06:42 PM   #1 (permalink)
SkittlesAreYum
Registered User
 
Join Date: Apr 2003
Posts: 8
SkittlesAreYum is on a distinguished road
Unhappy Difficulties with removeAll() on a JFrame

What I want to do is reuse a frame again and again, but each time it will have different components, so I need to remove all the old ones. So, I'm trying to use the removeAll() method to take all the components off, and then add new ones later. For example:

--all neccessary imported packages--
JFrame myFrame = new JFrame();
JLabel label1 = new JLabel("hello");

take all components off, and add new ones
myFrame.removeAll();
JLabel label2 = new JLabel("hello again");

Well, the removeAll() method works like a charm. In fact, it works so damn well that I cannot add anything more to the frame. label2, for example, won't show up. My teacher suggested using myFrame.validate() after I removeAll, but that doesn't help. What is the problem here? Can anyone help?

Thanks in advance.
SkittlesAreYum is offline   Reply With Quote
Old 04-07-2003, 07:05 PM   #2 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
.repaint()
.refresh()

can't quite remember...

for the record, try resizing the frame after you've done .removeAll() and then added some things.. if they show up after the resizing id suggest its .repaint() you need.

( resizing as in grab corners with mouse and drag ).
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 04-07-2003, 07:11 PM   #3 (permalink)
SkittlesAreYum
Registered User
 
Join Date: Apr 2003
Posts: 8
SkittlesAreYum is on a distinguished road
It's repaint(), but that doesn't work either. I'm still left with a blank frame. And physically resizing the frame doesn't help either way, either. Dang it.
SkittlesAreYum is offline   Reply With Quote
Old 04-07-2003, 07:20 PM   #4 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
then id suggest they aren't on the frame..

what is the code u do for adding them? because what u've posted up there obviously doesn't add either label to those frames....
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 04-07-2003, 07:24 PM   #5 (permalink)
SkittlesAreYum
Registered User
 
Join Date: Apr 2003
Posts: 8
SkittlesAreYum is on a distinguished road
Okay, here's the full code of an example I'm trying to get to work, just as a test program. Just paste the whole thing into TextPad or whatever, it should run.



import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class X
{
private static JFrame xFrame;
public static void main(String[] args)
{
xFrame = new JFrame();
xFrame.setSize(100,100);
JButton xButton = new JButton("X");
xButton.addActionListener(new XListener());
xFrame.getContentPane().add(xButton);
xFrame.setVisible(true);
}

private static class XListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
xFrame.removeAll();
xFrame.validate();
xFrame.hide();
xFrame.show();

xFrame.getContentPane().add(new JLabel("Java is stupid"));
}
}
}
SkittlesAreYum is offline   Reply With Quote
Old 04-07-2003, 07:29 PM   #6 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
xFrame.getContentPane().add(new JLabel("Java is stupid"));

after that you tried:

xFrame.repaint();

and it didn't work?

.. it should...
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 04-07-2003, 07:30 PM   #7 (permalink)
SkittlesAreYum
Registered User
 
Join Date: Apr 2003
Posts: 8
SkittlesAreYum is on a distinguished road
Well, no I hadn't tried that. But I just did, and still a no-go. Why am I so good at messing up Java? I always stump my teacher with crap like this.

Thanks for your constant help, abc.
SkittlesAreYum is offline   Reply With Quote
Old 04-07-2003, 07:38 PM   #8 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
yes, okay, .removeAll() seemingly removes .. everything, so i think you need to add a content pane again... how, i don't exactly know, but thats your problem
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 04-07-2003, 07:41 PM   #9 (permalink)
SkittlesAreYum
Registered User
 
Join Date: Apr 2003
Posts: 8
SkittlesAreYum is on a distinguished road
I've tried remaking the Container (getContentPane()) for the frame, and it doesn't work either. However, check out this my teacher made:

public class Xnew
{
private static JFrame xFrame;
private static JPanel xPanel;
public static void main(String[] args)
{
xFrame = new JFrame();
xPanel = new JPanel();

xFrame.setSize(100,100);
JButton xButton = new JButton("X");
xButton.setEnabled(true);
xButton.addActionListener(new XListener());
xPanel.add(xButton);
xFrame.getContentPane().add(xPanel);
xFrame.setVisible(true);
}

private static class XListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
xPanel.removeAll();
xPanel.add(new JLabel("Java is stupid"));
xPanel.updateUI();
}
}
}

Making the label on a JPanel allows it to display after everything else is removed. To top it all off, the JPanel is NOT removed by removeAll().

Should we just give up now and say removeAll() is bugged?
SkittlesAreYum is offline   Reply With Quote
Old 04-07-2003, 07:53 PM   #10 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
no no, its not bugged, the jpanel isn't removed because .removeAll is called on the panel itself, not the frame..

i think my prior suggestion is still true; removeAll gets rid of everything, you need to add a new contentPane to it in order to get it to display the items..


... it seems.
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 04-07-2003, 07:57 PM   #11 (permalink)
SkittlesAreYum
Registered User
 
Join Date: Apr 2003
Posts: 8
SkittlesAreYum is on a distinguished road
I modified the first (X) program so a Container is declared right above where the label is added. Then I modified the line where the label is added so it appears as below.

Container contentPane = xFrame.getContentPane();

contentPane.add(new JLabel("Java is stupid"));

And it still won't work. Grrr. Thanks for your help, I'm going to bed right now, I'll check this tommorow.
SkittlesAreYum is offline   Reply With Quote
Old 04-14-2003, 08:46 AM   #12 (permalink)
jeff
Registered User
 
Join Date: Apr 2003
Posts: 2
jeff is on a distinguished road
Hi,

Notice that when you first add your button, you're adding it to the JFrame's content pane. The content pane is where you're supposed to add components to a JFrame.

see:
http://java.sun.com/docs/books/tutor.../toplevel.html

Later in your application, you're calling removeAll directly on the JFrame. This removes the content pane and other panes mentioned in the above URL. You don't really want to be doing this. Instead, call getContentPane().removeAll(). This will restore the JFrame back to how it was before you added any components.

Then, add your new components to the content pane and call:

invalidate
revalidate
repaint

on it.

So, the body of your actionListener could look like this:

xFrame.getContentPane().removeAll();
xFrame.getContentPane().add(new JLabel("Java is NOT stupid"));
xFrame.getContentPane().invalidate();
xFrame.getContentPane().validate();
xFrame.getContentPane().repaint();

I've tried this with your code and it fixes the problem.
jeff is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 05:30 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting