Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By DarrylBurke
  • 1 Post By Fubarable

Thread: Self-defined JPanel object seems to terminate

  1. #1
    Amanoo is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Self-defined JPanel object seems to terminate

    Hello.
    I am working on a java program, and I have a problem with the GUI. I have several classes which extend the standard JPanel class and have some extra functions. One of which is what I called the TilePanel. This class loads a certain image and draws it, which is then imported into other JFrames. This class has a function which changes the image that was loaded to another. By this way I can replace the standard image that it holds with another (for example, the standard image that is loaded during its construction can be replaced by that of a colored circle. The TilePanel is loaded into a BlockPanel (also an extension of a JPanel that I made), which in turn is loaded into a so-called GamePanel.
    My problem is that when I place the method to change the image inside of a certain TilePanel (called from GamePanel using BlockPanel[i].TilePanel[j].setColor(String color); in which i and j are both values between 0 and 8, 0 and 8 themselves included) in the constructor of GamePanel, the TilePanel does indeed change its picture, as it is supposed. However, once the construction of the GamePanel is finished, I cannot change the pictures that is loaded in the TilePanel anymore. I think that after the TilePanel is constructed, it terminates, therefore calling any methods defined inside of its class will not achieve anything.

    How do I keep this from happening, and allow this JPanel-object to be manipulated, and its methods to be called, after its construction?
    I have a hunch I have to create some sort of Thread somewhere, but I am kind of lost on where and exactly how to do this...

    Click here to download my code from mediafire

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Self-defined JPanel object seems to terminate

    I don't know about all, but most will avoid downloading code. If you have pertinent code to show and you want an increased chance of getting someone to look at it, consider posting it here.

    Oh, and also, classes don't "terminate". You've likely got a bug somewhere and may wish to use println statements or a debugger to find it.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Self-defined JPanel object seems to terminate

    What bet it's another case of shadowing variables.

    @Amanoo: I won't download from a link either. To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem. Not all your code.


    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Amanoo is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: Self-defined JPanel object seems to terminate

    I think I managed to get to my problem enough to more or less know what it was. I can probably discuss this matter without having to show any code, but one problem still remains. Nothing terminates, but I am adding certain JFrames into other JFrames. The JFrames that were added can be changed, but this is not visible until some of the JFrames are refreshed. As of yet, I have been unable to determine what is the best way to do this. Invalidating and revalidating JFrames seems to work only once. The problem is that my JFrames can theoretically be changed an infinite amount of times, so I have to find a way to properly refresh them. I am at a loss on this matter.
    Everything works as I thought internally, it is really the GUI representation where it fails.

    EDIT:
    It seems that it does refresh properly if I resize my window after I change something.
    Note that I have my GamePanel class implements ComponentListener, and my componentResized(ComponentEvent e); looks like this
    Java Code:
    	public void componentResized(ComponentEvent e) {
    		thisLayout.setHgap(this.getWidth() / 75);
    		thisLayout.setVgap(this.getHeight() / 75);
    	}
    (GamePanel is the JPanel which adds 9 BlockPanels using a 3x3 GridLayout, and a BlockPanel in turn adds 9 TilePanels in a very similar way).

    I think I could try a workaround which changes the sizes within the JPanels a little and then turns them back. This should make java refresh them properly, but I would prefer a more proper way to do things. This really is more of a patch.

    EDIT2: this doesn't seem to work either, I really seem to need to change the size of GamePanel...
    Last edited by Amanoo; 03-20-2012 at 07:55 PM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Self-defined JPanel object seems to terminate

    Quote Originally Posted by Amanoo View Post
    ...I can probably discuss this matter without having to show any code, ...
    But will likely decrease your chances of our being able to help you.


    Nothing terminates, but I am adding certain JFrames into other JFrames. ...
    You're adding JFrames into other JFrames??? How? Why? This is not supposed to be done, and in fact can't be done. Are you sure that this is what you are doing? Code would be nice.


    The JFrames that were added can be changed, but this is not visible until some of the JFrames are refreshed. As of yet, I have been unable to determine what is the best way to do this. Invalidating and revalidating JFrames seems to work only once. The problem is that my JFrames can theoretically be changed an infinite amount of times, so I have to find a way to properly refresh them. I am at a loss on this matter.
    And without code, so are we. Note that spitting out and closing JFrames is usually a poor way to design a GUI. With more information we can often help you to a better GUI design structure.

    Everything works as I thought internally, it is really the GUI representation where it fails.
    No idea what you're saying here. And regardless, without testable code, I'd have no way of proving it even if I did understand it.


    EDIT:
    It seems that it does refresh properly if I resize my window after I change something.
    Sounds like you're adding components to a GUI *after* setting a window visible, something the tutorials tell you not to do. If this happens while the code is running (that is, if you're adding components to visible GUI's while the program runs), and this happens, then it sounds like you're not revalidating and repainting the container that holds the components that were added and/or removed.

    Note that I have my GamePanel class implements ComponentListener, and my componentResized(ComponentEvent e); looks like this
    Java Code:
    	public void componentResized(ComponentEvent e) {
    		thisLayout.setHgap(this.getWidth() / 75);
    		thisLayout.setVgap(this.getHeight() / 75);
    	}
    (GamePanel is the JPanel which adds 9 BlockPanels using a 3x3 GridLayout, and a BlockPanel in turn adds 9 TilePanels in a very similar way).

    I think I could try a workaround which changes the sizes within the JPanels a little and then turns them back. This should make java refresh them properly, but I would prefer a more proper way to do things. This really is more of a patch.

    EDIT2: this doesn't seem to work either, I really seem to need to change the size of GamePanel...
    Absolutely no way to make heads or tails out of this without code.

    Good luck.
    DarrylBurke likes this.

Similar Threads

  1. Replies: 2
    Last Post: 06-16-2011, 09:39 AM
  2. how can terminate the string
    By abhay23k in forum Threads and Synchronization
    Replies: 4
    Last Post: 08-10-2010, 08:25 PM
  3. event that terminate function
    By itaipee in forum AWT / Swing
    Replies: 6
    Last Post: 12-08-2009, 03:15 PM
  4. Using string to terminate loop
    By mrblippy in forum New To Java
    Replies: 3
    Last Post: 04-23-2009, 06:16 AM
  5. 3 errors and then terminate program
    By hezfast2 in forum New To Java
    Replies: 2
    Last Post: 05-20-2008, 01:57 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
  •