• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Missing JButton in JFrame ? Why?

 
Ranch Hand
Posts: 67
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rohani Jagdale wrote:Use getContentPane() method . . .

Because the add method has been changed getContentPane is usually redundant.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic