Results 1 to 9 of 9
  1. #1
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    98
    Rep Power
    0

    Default SOLVED : When I create new class object, old image persists?

    I have a class Program which creates a new Test class object on a button press.
    Java Code:
    public void actionPerformed(ActionEvent arg0) {
    				if(takingTest){
    					fl = new FileLoader();
    					fl.loadStrings();
    					fl.turnStringsIntoPictures();
    				}
    				hideAllPanels();
    				fl.getProblemList().clear();
    				fl.turnPicturesIntoProblems();
    				p2 = (LinkedList<Problem>) fl.getProblemList().clone();
    				Random r = new Random();	
    				for(int i = 0; i < getQuestionCount(); i++){
    					problems.add(p2.remove(r.nextInt(p2.size())));
    				}
    				takingTest = true;
    				Test test = new Test(b, f, headerPanel.getHeight(), font30, problems);
    				frame.add(test);				
    			}
    I then have the Test class that it is a subclass of Program.
    Java Code:
    public Test(Color b, Color f, int hSize, Font font, LinkedList<Problem> problems){
    		this.b = b;
    		this.f = f;
    		this.hSize = hSize;
    		this.font = font;
    		this.problems = problems;
    		
    		if(testTaken){
    			remove(testPanel);
    			remove(resultsPanel);
    			questionsAnswered = 0;
    			questionsCorrect = 0;
    			revalidate();
    			repaint();
    		}
    		if(!testTaken){
    			setResultsPanel();
    		}
    		resultsPanel.setVisible(false);
    		startTest();
    		
    	}
    	
    	private void startTest(){
    		Random r = new Random();
    		setTestPanel(problems.remove(r.nextInt(problems.size())));
    	}
     [snip]
    The first time through taking the test, all goes well. The images load fine, and nothing seems wrong. The problem is when I go back and try and take the test again. Even though I tried removing the testPanel and resultsPanel, I still am shown the results panel whenever I try to create a new Test object. But oddly, if I click on the resultsPanel that shouldn't be there, where the answer images show up, I get the images showing up again fine (though the count is off as it consumes one to click them to show again). If that makes any sense to anyone, I could really appreciate some help as to what I am doing that is causing the old object information to show when I create a new object.
    Last edited by rru96; 03-26-2014 at 02:17 AM.

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: When I create new class object, old image persists?

    I can only guess without seeing the Program class and/or the rest of the Test class. My guess is that testTaken is static.
    Get in the habit of using standard Java naming conventions!

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,743
    Rep Power
    5

    Default Re: When I create new class object, old image persists?

    No it sounds like a repainting problem to me; probably in the addition and removal of panels, a call to revalidate() needs to be added so the layout managers are triggered to update themselves.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    98
    Rep Power
    0

    Default Re: When I create new class object, old image persists?

    Thanks for the replies. The "testTaken" variable is not static. My thoughts were that it was a repainting issue as gimbal stated. I did do a call to revalidate() and repaint() on lines 13 and 14 of the test class to no avail. I tried also adding calls to testPanel.revalidate() and testPanel.repaint(). Still no luck..

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,743
    Rep Power
    5

    Default Re: When I create new class object, old image persists?

    You need to call revalidate on the container which holds the panels which you are removing/adding. Is that the testPanel you are mentioning here? Also try adding a call to repaint() after the revalidate(), I've had cases where that was necessary.

    BTW: you are using layout managers right? You are not setting null-layout managers?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    98
    Rep Power
    0

    Default Re: When I create new class object, old image persists?

    Oh no.... Once I figured out how to use GridBagLayout.... I have pretty much gone to it 99.9% of the time.

  7. #7
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    98
    Rep Power
    0

    Default Re: When I create new class object, old image persists?

    testPanel and resultsPanel are both in the Test class..... which extends the Program class.

  8. #8
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    98
    Rep Power
    0

    Default Re: When I create new class object, old image persists?

    I have a method called hideAllPanels() that resides in the program class. This method is called whenever any of my menu options are clicked on. In order to get out of the results panel, you must click on one of the menu options, thereby calling the hideAllPanels() method. I tried calling revalidate() and repaint() in that method if takingTest boolean is tripped. Nothing. I even tried calling super.revalidate() and frame.revalidate()... still nothing.

  9. #9
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    98
    Rep Power
    0

    Default Re: When I create new class object, old image persists?

    Instead of a call to revalidate() or rapaint(), evidently just a call to test.removeAll() right before I assign a new Test object worked. Thank you guys

Similar Threads

  1. Replies: 2
    Last Post: 03-12-2012, 11:30 AM
  2. create object from a class
    By HearT.Hunt3r in forum Advanced Java
    Replies: 1
    Last Post: 10-28-2011, 02:46 AM
  3. Replies: 3
    Last Post: 05-02-2011, 04:37 AM
  4. Replies: 2
    Last Post: 06-22-2010, 04:29 AM
  5. Replies: 1
    Last Post: 04-09-2008, 02:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •