Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

AWT Applet vrs SWING applet

 
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?
 
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
 
Sheriff
Posts: 21805
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • 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.
 
Rancher
Posts: 3075
24
  • 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!
 
Aaaaaand ... we're on the march. Stylin. Get with it tiny ad.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!