Java Help Needed

Got a little techonology problem that you need fixed pronto? Post it here and we'll see what we can do.

Moderator: Moderators

Locked
Message
Author
ArwenEarendil
PPT Warrior
PPT Warrior
Posts: 765
Joined: Thu Jun 17, 2004 1:28 pm
Location: Among the crayons on my desk..

Java Help Needed

#1 Post by ArwenEarendil »

I'm redoing a textbased Computer Aided Instruction program I created last year. The thing is, my teacher wants the program with a GUI. I'm not doing it as an applet, but with AWT and Swing. So basically, a standalone program.

I just dived right into graphics, so I'm basically drawing from textbook info and previous knowledge I had on Java, C++ and OOProg. So please excuse the bad code.

The problem is this. The main goal (for me anyways) is to keep the program to ONE window. That means that in the start window (say, the menu) I click a button, the window is supposed to clear and show me a completely different interface. The action passes right into the ActionListener, and works right until after repaint(); After removing previous panels and repainting, I try to add new panels. Except, it doesn't work.

Thoughts? Suggestions? Help? Anything is appreciated.

You'll notice that I tried several methods (via commenting out). I also didn't put comments regarding what each part was supposed to do, so if you don't understand what my purpose was supposed to be, please ask.

Here are the links:

http://www.geocities.com/whitemagicdesi ... troyer.txt
http://www.geocities.com/whitemagicdesi ... oveAll.txt
Last edited by ArwenEarendil on Fri Nov 03, 2006 5:40 pm, edited 1 time in total.
Image
. Set by Medusa ♥
Universal Senja
Honorary Member
Honorary Member
Posts: 731
Joined: Tue Jun 01, 2004 3:26 pm

#2 Post by Universal Senja »

I believe I know what you're trying to do. Out of curiosity, are you using a Container?
ArwenEarendil
PPT Warrior
PPT Warrior
Posts: 765
Joined: Thu Jun 17, 2004 1:28 pm
Location: Among the crayons on my desk..

#3 Post by ArwenEarendil »

Universal Senja wrote:I believe I know what you're trying to do. Out of curiosity, are you using a Container?


Yes. However, I just looked at my previous post (I was in a rush yesterday) and I posted the same link. -_-; That has been corrected now. The main file is TestRemoveAll.txt

In the ActionListener, this is what happens:

Code: Select all

contentPane.removeAll(); //or contentPane.remove(Component component)
repaint(); //works right up to here
contentPane.add(Component component); //doesn't work


In my mind, this works, but it's probably wrong. If I compile the project, I just get a blank window after the object is passed to the ActionListener.
Image
. Set by Medusa ♥
Universal Senja
Honorary Member
Honorary Member
Posts: 731
Joined: Tue Jun 01, 2004 3:26 pm

#4 Post by Universal Senja »

Just letting you know I haven't forgotten about this. I've been pretty busy myself and haven't really found a nice block of time yet. Is there a due date or a day you'll need this done by?
ArwenEarendil
PPT Warrior
PPT Warrior
Posts: 765
Joined: Thu Jun 17, 2004 1:28 pm
Location: Among the crayons on my desk..

#5 Post by ArwenEarendil »

I have until January-February.. which is really a lot of time for the project. My only obstacle right now is this.
Image
. Set by Medusa ♥
Universal Senja
Honorary Member
Honorary Member
Posts: 731
Joined: Tue Jun 01, 2004 3:26 pm

#6 Post by Universal Senja »

Ok, I think I might have it. On line 83 where you have a repaint(), change that to contentPane.validate();
ArwenEarendil
PPT Warrior
PPT Warrior
Posts: 765
Joined: Thu Jun 17, 2004 1:28 pm
Location: Among the crayons on my desk..

#7 Post by ArwenEarendil »

*glomps*

IT WORKS!

By itself, it didn't work.. if froze after a while. But I did a combo of repaint() and contentPane.validate(); and it works! Thank you! ^_^

So this is what the ActionListener looked like in the end:

Code: Select all

public void actionPerformed (ActionEvent e)
  {
    Container contentPane = getContentPane();
   
    if (e.getActionCommand().equals("Clear"))
    {
      contentPane.remove(buttonPanel);
      contentPane.remove(textPanel);
      repaint();
      contentPane.add(textPanel2, BorderLayout.CENTER);
      contentPane.add(buttonPanel2, BorderLayout.SOUTH);
      contentPane.validate();
    }
    else if(e.getActionCommand().equals("Return"))
    {
      contentPane.remove(textPanel2);
      contentPane.remove(buttonPanel2);
      repaint();
      contentPane.add(textPanel, BorderLayout.CENTER);
      contentPane.add(buttonPanel, BorderLayout.SOUTH);
      contentPane.validate();
    }
    else
    {
      System.exit(0);
    }
  }
Image
. Set by Medusa ♥
Universal Senja
Honorary Member
Honorary Member
Posts: 731
Joined: Tue Jun 01, 2004 3:26 pm

#8 Post by Universal Senja »

No problem. Glad to see everything turned out alright. :)
Locked