#1
  1. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2005
    Location
    Internet
    Posts
    7,625
    Rep Power
    6089

    Client won't show Container until a JTextArea in it gets text.


    So I'm making a client. There's a thread to handle connection with a server, a thread to handle processing packets.

    There's also the GUI element. Very simple.

    JAVA Code:
    public void instantiateStatusScreen() {
    	statusScreen = new StatusScreen();
     
    	statusScreen.connectionStatus = new JTextArea("", 10, 50);
    	...
     
    	statusScreen.continueButton = new JButton("Continue");
    	...
     
    	statusScreen.add(statusScreen.connectionStatus);
    	statusScreen.add(statusScreen.continueButton);
     
    	replaceShownWith(statusScreen);
    }


    Straightforward. StatusScreen is very very simple also.

    java Code:
    public class StatusScreen extends Container {
    	JTextArea connectionStatus;
    	JButton continueButton;
    }


    replaceShownWith is straightforward. I've had other things (such as removing an element instead of setting to invisible) as well.

    JAVA Code:
    public void replaceShownWith(Container c) {
    	for (int i = 0; i < getContentPane().getComponents().length; i++) {
    		getContentPane().getComponents()[i].setVisible(false);
    	}
    	c.setVisible(true);
    	getContentPane().add(c);
    	c.repaint();
    }


    Now my problem is that the only way I can make this actually visible is if I call this method from another class.

    JAVA Code:
    public void addStatusScreenText(String s) {
    	statusScreen.connectionStatus.append(s);
    }


    The only possible reason I can think of is that my other threads are hogging resources needed for the GUI, and the reason it paints when that method is called is because adding text to a textarea somehow gives the GUI class the resources it needs to show itself.

    Now, the GUI 'box' actually shows. When the program is launched, you get the default gray box, as if nothing has been added yet but the size has already been set. So obviously the constructor works. Of course, when I call other methods from the constructor, it doesn't work. I know that the methods are executed because System.out.println calls work. It says it works. But nothing appears on the screen. Not until I tell the textarea to append some text. Only then does it display. Of course, the whole thing displays, not just the textarea. Doing other stuff, like changing the button to disabled or enabled doesn't do it. Only appending text seems to work.

    Why?
    Chat Server Project & Tutorial | WiFi-remote-control sailboat (building) | Joke Thread
    “Rational thinkers deplore the excesses of democracy; it abuses the individual and elevates the mob. The death of Socrates was its finest fruit.”
    Use XXX in a comment to flag something that is bogus but works. Use FIXME to flag something that is bogus and broken. Use TODO to leave yourself reminders. Calling a program finished before all these points are checked off is lazy.
    -Partial Credit: Sun

    If I ask you to redescribe your problem, it's because when you describe issues in detail, you often get a *click* and you suddenly know the solutions.
    Ches Koblents
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2005
    Location
    Internet
    Posts
    7,625
    Rep Power
    6089
    I've disabled all other threads from running. Still the same. Can't be because of other threads taking up resources, then.

    I think it might be because I'm using a Container instead of a Panel. Don't think these problems exist when I use Panels.
    Chat Server Project & Tutorial | WiFi-remote-control sailboat (building) | Joke Thread
    “Rational thinkers deplore the excesses of democracy; it abuses the individual and elevates the mob. The death of Socrates was its finest fruit.”
    Use XXX in a comment to flag something that is bogus but works. Use FIXME to flag something that is bogus and broken. Use TODO to leave yourself reminders. Calling a program finished before all these points are checked off is lazy.
    -Partial Credit: Sun

    If I ask you to redescribe your problem, it's because when you describe issues in detail, you often get a *click* and you suddenly know the solutions.
    Ches Koblents
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Posts
    20
    Rep Power
    0
    gimp--

    I think you're muddling in the gray area of heavy-weight (java.awt.Container) vs lightweight components (most if-not-all Swing components) and that's leading to confusion in the event-dispatching thread as to what it needs to draw when and where...

    If you stick with purely swing components, JPanels, etc as opposed to Containers you shouldn't run into this problem..

    Here's more on this when I ran into this problem:
    http://java.sun.com/products/jfc/tsc/articles/mixing/
    http://www.ibm.com/developerworks/java/library/j-heavy.html

    Comments on this post

    • gimp agrees
    Last edited by Mr.V.; June 18th, 2007 at 09:00 PM. Reason: (forgot the sun website on the issue...duh =)
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2005
    Location
    Internet
    Posts
    7,625
    Rep Power
    6089
    Thank you.

    Any chance of a swing component that lets me put everything exactly where I want without using any sort of Layout? Those things really piss me off, because there's no way I'm going to manually code all the layout-using crap, and a code generator I tried didn't work straight.
    Chat Server Project & Tutorial | WiFi-remote-control sailboat (building) | Joke Thread
    “Rational thinkers deplore the excesses of democracy; it abuses the individual and elevates the mob. The death of Socrates was its finest fruit.”
    Use XXX in a comment to flag something that is bogus but works. Use FIXME to flag something that is bogus and broken. Use TODO to leave yourself reminders. Calling a program finished before all these points are checked off is lazy.
    -Partial Credit: Sun

    If I ask you to redescribe your problem, it's because when you describe issues in detail, you often get a *click* and you suddenly know the solutions.
    Ches Koblents
  8. #5
  9. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    Chicago, IL
    Posts
    10,131
    Rep Power
    5058
    Originally Posted by gimp
    Any chance of a swing component that lets me put everything exactly where I want without using any sort of Layout?
    Of course; set the layout to null.

    ~
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v̀ÈÊ\"¤¾Àʲ¬Æ\"v¤Î¤\"²¤¨¸¬Æ".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2005
    Location
    Internet
    Posts
    7,625
    Rep Power
    6089
    Really? Really? You're... really? Goddamn, why do I not see these things? Are you really allowed to do that? Wow. Thank you! You just saved my ***. I think.
    Chat Server Project & Tutorial | WiFi-remote-control sailboat (building) | Joke Thread
    “Rational thinkers deplore the excesses of democracy; it abuses the individual and elevates the mob. The death of Socrates was its finest fruit.”
    Use XXX in a comment to flag something that is bogus but works. Use FIXME to flag something that is bogus and broken. Use TODO to leave yourself reminders. Calling a program finished before all these points are checked off is lazy.
    -Partial Credit: Sun

    If I ask you to redescribe your problem, it's because when you describe issues in detail, you often get a *click* and you suddenly know the solutions.
    Ches Koblents
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Posts
    20
    Rep Power
    0
    Gimp--

    Yawmark is correct...
    Here's the part in the Java tutorial regarding null layouts (aka absolute positioning)

    http://java.sun.com/docs/books/tutor...yout/none.html

    I personally understand your frustration with layout managers. But doing without one ended up being harder and more work.

    I've found great luck with GridBagLayout once I got the hang of it in getting things to look exactly how I want them to if a simple flowlayout doesn't suffice. I don't even mess with the other stuff because of how flexible GridBagLayout can be.

    Be warned though, GBL takes a bit of practice getting things working and can be a bit of a pain if you later decide that you want to add a new item to a program interface...
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2005
    Location
    Internet
    Posts
    7,625
    Rep Power
    6089
    Yeah, unfortunately, it didn't work. Everything extends JPanels now instead of Containers but the problem persists.
    Chat Server Project & Tutorial | WiFi-remote-control sailboat (building) | Joke Thread
    “Rational thinkers deplore the excesses of democracy; it abuses the individual and elevates the mob. The death of Socrates was its finest fruit.”
    Use XXX in a comment to flag something that is bogus but works. Use FIXME to flag something that is bogus and broken. Use TODO to leave yourself reminders. Calling a program finished before all these points are checked off is lazy.
    -Partial Credit: Sun

    If I ask you to redescribe your problem, it's because when you describe issues in detail, you often get a *click* and you suddenly know the solutions.
    Ches Koblents
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Posts
    20
    Rep Power
    0
    You haven't posted your whole code, what top level container are you using to store? Are you extending JFrame?

    I'm guessing that your problem is that you're adding the containers after you've set the JFrame visible (or whatever top level container you're using).

    Once you've set the JFrame visible, if you add or remove components to its contentPane, you've got to re-layout all the components. With Swing components you achieve this with a call to revalidate();

    Try replacing c.repaint() in:
    Code:
    public void replaceShownWith(Container c) {
    with
    Code:
     ((JPanel)getContentPane()).revalidate();
    and see if that does the trick...

    (p.s. make sure you still aren't mixing heavyweight and lightweight. e.g. ensure public void replaceShownWith(Container c) is actually JPanel c
  18. #10
  19. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2005
    Location
    Internet
    Posts
    7,625
    Rep Power
    6089
    That did the trick!

    If you'll believe it, I had validate() instead of revalidate().
    Chat Server Project & Tutorial | WiFi-remote-control sailboat (building) | Joke Thread
    “Rational thinkers deplore the excesses of democracy; it abuses the individual and elevates the mob. The death of Socrates was its finest fruit.”
    Use XXX in a comment to flag something that is bogus but works. Use FIXME to flag something that is bogus and broken. Use TODO to leave yourself reminders. Calling a program finished before all these points are checked off is lazy.
    -Partial Credit: Sun

    If I ask you to redescribe your problem, it's because when you describe issues in detail, you often get a *click* and you suddenly know the solutions.
    Ches Koblents
  20. #11
  21. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2005
    Location
    Internet
    Posts
    7,625
    Rep Power
    6089
    Oh, and the reason I want to not have a layout and position everything my self is fairly simple.
    This client will have a ton of code, but almost all of it will have nothing to do with the GUI. I have 5 panels I want to display when the time is right. Four of those will have, what, 2-4 elements? No need for a layout there. The last will have element positions dynamically calculated and layouts aren't too allowing of those. Moreover, some of the elements for the last one will be things like Rectangles, as opposed to good old Components.
    Chat Server Project & Tutorial | WiFi-remote-control sailboat (building) | Joke Thread
    “Rational thinkers deplore the excesses of democracy; it abuses the individual and elevates the mob. The death of Socrates was its finest fruit.”
    Use XXX in a comment to flag something that is bogus but works. Use FIXME to flag something that is bogus and broken. Use TODO to leave yourself reminders. Calling a program finished before all these points are checked off is lazy.
    -Partial Credit: Sun

    If I ask you to redescribe your problem, it's because when you describe issues in detail, you often get a *click* and you suddenly know the solutions.
    Ches Koblents
  22. #12
  23. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    Chicago, IL
    Posts
    10,131
    Rep Power
    5058
    No need for a layout there.
    Of course there is.

    The last will have element positions dynamically calculated and layouts aren't too allowing of those.
    Calculating positions dynamically is exactly what layout managers are for!

    The Java™ Tutorial - Lesson: Laying Out Components Within a Container

    ~
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v̀ÈÊ\"¤¾Àʲ¬Æ\"v¤Î¤\"²¤¨¸¬Æ".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}

IMN logo majestic logo threadwatch logo seochat tools logo