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

Java Help Needed

Thu Nov 02, 2006 5:20 pm

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.

Fri Nov 03, 2006 1:43 am

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

Fri Nov 03, 2006 5:42 pm

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:
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.

Mon Nov 06, 2006 1:46 am

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?

Mon Nov 06, 2006 5:16 pm

I have until January-February.. which is really a lot of time for the project. My only obstacle right now is this.

Thu Nov 09, 2006 4:49 am

Ok, I think I might have it. On line 83 where you have a repaint(), change that to contentPane.validate();

Fri Nov 10, 2006 6:03 pm

*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:
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);
    }
  }

Sat Nov 11, 2006 5:13 am

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