Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextField Causing Whole JPanel To Disappear  RSS feed

 
Tom Finn
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are my 3 classes:





Although the JFrame loads, none of the information does. If I remove the JTextFields then it's fine all of a sudden. I've been trying to find the problem for hours and am at my wits end. Would really appreciate some help, thanks!
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens if you resize the JFrame slightly?

Why do you think that happens?
 
Tom Finn
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahhh,

It appears. That's very strange, I'm sure I already tried doing that. I'm assuming this is a repaint problem? Due to the nature of the JtextField? Although I ran a seperate testcase without my custom JFrame and it worked fine so I guess I'm unsure. Could you point me in the right direction?

(EDIT: After a few minutes of digging I see that i needed to revalidate the frame. Amazing. Can't believe I spent 4 hours doing this alone, one person points me in the right direction and I can solve it almost instantly. Thank you so much!)
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Finn wrote:After a few minutes of digging I see that i needed to revalidate the frame.

That's not a solution, it's a workaround. What's wrong with your code is that you're unnecessarily setting the frame visible before adding the components.

All you need to do here is move the setVisible(true) to the end of the constructor, after all content has been added.

In case you come up against a (rather rare) requirement of adding/removing components to/from a Container that's already visible, then you do need to call revalidate() and usually also repaint(). If you're not sure whether repainting would be necessary, it's safer to call it anyways.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!