• Post Reply Bookmark Topic Watch Topic
  • New Topic

Missing JButton in JFrame ? Why?  RSS feed

 
vinoth vino
Ranch Hand
Posts: 67
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hello Everyone, when i execute the program i got only background image only.Where is my JButton?
JButton.png
[Thumbnail for JButton.png]
Output
 
Paul Clapham
Sheriff
Posts: 22520
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your JButton is in the old content pane, the one which is now not visible because you set the frame's content pane to be that picture instead.

Just add the JLabel and the JButton and you should see them both -- either above and below, or left and right, I don't remember what the default is for a FlowLayout. Don't mess with setting the content pane.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, add all components before, not after, you invoke setVisible(true).

You do realise that you have two redundant cllas to setSize(...) ... or don't you? Or was that a bad workaround to cause the GUI to revalidate itself?

You could also benefit from the Oracle tutorial on Concurrency in Swing. What you need to know right now is that all Swing constructors and methods are to be invoked on the EDT, with the exception of those specifically documented as thread safe (and for that, refer to the documentation for Java 7 or later, as earlier versions wrongly documented certain methods as thread safe but in fact they never were).
 
vinoth vino
Ranch Hand
Posts: 67
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:Also, add all components before, not after, you invoke setVisible(true).

You do realise that you have two redundant cllas to setSize(...) ... or don't you? Or was that a bad workaround to cause the GUI to revalidate itself?

You could also benefit from the Oracle tutorial on Concurrency in Swing. What you need to know right now is that all Swing constructors and methods are to be invoked on the EDT, with the exception of those specifically documented as thread safe (and for that, refer to the documentation for Java 7 or later, as earlier versions wrongly documented certain methods as thread safe but in fact they never were).

In some site they use "setSize" two times for refreshing
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vinoth vino wrote:In some site they use "setSize" two times for refreshing

I would stay away from such sites. Anyone who does that doesn't understand Swing programming.

The correct approach, as already stated, is to assemble the GUI before you make it visible; in case you need to add/remove components while the GUI is shown, it needs to be revalidate()d.
 
Rohani Jagdale
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use getContentPane() method and then add() method to add components. and move setVisible(true) at last. after adding all components.

 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rohani Jagdale wrote:Use getContentPane() method . . .
Because the add method has been changed getContentPane is usually redundant.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!