• Post Reply Bookmark Topic Watch Topic
  • New Topic

AWT Applet vrs SWING applet  RSS feed

 
Ronald Vermeij
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am learning "Swing by example" by translating an good working simple AWT applet into a Swing Applet. To keep it very simple (and understand the basic first) I only add a button to an applet_surface and a button_actionlistener to that button.

Now i'm running to a brick wall... seems like I can not find a solution why my Swing applet is not working :-(

AWT Applet code


Swing applet code


Both examples compiles without errors, but when loading the applets into a browser via an html page...:
- The AWT applet shows the [quit] button on the applet_surface
- The Swing version shows nothing :-(

Question
What have I forgotten in the Swing version?
Do I need use a JFrame first to which I add the JButton before it gets visible on screen?
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Swing, you'd generally override paintComponent, not paint: http://download.oracle.com/javase/tutorial/uiswing/painting/closer.html

As an aside, it's good practice to mark all methods that are supposed to override other methods via an "@Override" annotation: http://download.oracle.com/javase/tutorial/java/javaOO/annotations.html
 
Rob Spoor
Sheriff
Posts: 21052
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever you override any of the painting methods, always call the super class implementation as the first thing. In this case: super.paint(g). Or, if you follow Lester's advice, super.paintComponent(g) inside paintComponent.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the API for JComponent#paint(...) and you'll understand that your overriding paint(...) results in three other painting methods not being called.
 
Rob Camick
Ranch Hand
Posts: 2788
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am learning "Swing by example" by translating an good working simple AWT applet into a Swing Applet.


Wrong approach. It is NOT a one to one conversion. You should read the section from the Swing tutorial on How to Make Applets to learn the proper approach.

As everybody has stated you should NOT override the paint() method. However, a JApplet does not have a paintComponent() method. If you want to add custom painting then there is a section on custom painting in the tutorial to read as well.

 
Ronald Vermeij
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess it is back to "Ground Zero" /"Square One" regarding to Swing Applet building:-)
Thanks Everyone!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!