This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

JSlider not displaying

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a really weird problem. If I run this applet as I copy below, the applet shows blank white screen. Then if I resize the window displaying the applet, I see a flicker of the Slider, or if I click on the Slider then it shows the slider correctly.

If I change the applet code by deleting the (blank) paint method, and leave everything else the same, the slider displays correctly with no issue.

The applet I am actually trying to make needs to have the paint method, so I can't just delete it. What can I do?


Code:

import java.applet.*;
import java.awt.*;
import javax.swing.*;

public class HelloWorldSWT extends Applet {

JSlider slider;
JPanel panel;

public void init(){
slider = new JSlider(JSlider.HORIZONTAL,1,10,5);
add(slider);
}

public void stop(){
}

public void paint(Graphics g){
}

}
 
Sheriff
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you overridden the paint method, and then made it completely empty? Because this way the applet cannot paint anything on the screen. So yes, just remove the method - your applet sub class still inherits the paint method from its parent class (Applet).
 
Rancher
Posts: 3191
29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start with the section from the Swing tutorial on How to Make Applets. A Swing applet is different from an AWT applet. For one think you should be extending JApplet, not Applet.
 
Michelle Kyamo
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Prime wrote:Why have you overridden the paint method, and then made it completely empty? Because this way the applet cannot paint anything on the screen. So yes, just remove the method - your applet sub class still inherits the paint method from its parent class (Applet).



It is only empty because I tried to pare the applet down to the minimum lines of code needed to demonstrate the problem. Deleting the method isn't a useful solution because in my real applet there would be a bunch of code in the paint method. All my drawString, drawImage, etc has to go in the paint method, right?

I'm also confused as to why you say the applet can't paint anything on the screen that isn't in the paint method, if there is a paint method. I have a different (working) applet where I add several buttons and text input fields in the init method using panels, then I also have a paint method with some drawString, etc. The buttons and text fields still work with a paint method present, it seems to be just the slider, and that confuses me.
 
Michelle Kyamo
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Camick wrote:Start with the section from the Swing tutorial on How to Make Applets. A Swing applet is different from an AWT applet. For one think you should be extending JApplet, not Applet.



Thanks, I will read that tomorrow. Does that mean it's a bad idea to be using Swing and non-Swing things in the same applet?

 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Michelle Kyamo wrote:Thanks, I will read that tomorrow. Does that mean it's a bad idea to be using Swing and non-Swing things in the same applet?


Yes, in general it's a bad idea, unless you have a darn good reason for doing so, and really know what you are doing and why.
 
Rob Spoor
Sheriff
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Michelle Kyamo wrote:

Rob Prime wrote:Why have you overridden the paint method, and then made it completely empty? Because this way the applet cannot paint anything on the screen. So yes, just remove the method - your applet sub class still inherits the paint method from its parent class (Applet).



It is only empty because I tried to pare the applet down to the minimum lines of code needed to demonstrate the problem. Deleting the method isn't a useful solution because in my real applet there would be a bunch of code in the paint method. All my drawString, drawImage, etc has to go in the paint method, right?

I'm also confused as to why you say the applet can't paint anything on the screen that isn't in the paint method, if there is a paint method. I have a different (working) applet where I add several buttons and text input fields in the init method using panels, then I also have a paint method with some drawString, etc. The buttons and text fields still work with a paint method present, it seems to be just the slider, and that confuses me.


If you override paint (or paintComponent, paintChildren, etc), you must always call super.paint(g) (or super.paintComponent(g) etc). All your custom painting should come after that call. Without it, the paint method will not do the out-of-the-box painting, which includes actually painting the control itself.
 
Michelle Kyamo
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

pete stein wrote:

Michelle Kyamo wrote:Thanks, I will read that tomorrow. Does that mean it's a bad idea to be using Swing and non-Swing things in the same applet?


Yes, in general it's a bad idea, unless you have a darn good reason for doing so, and really know what you are doing and why.



OK, I have deleted the paint method altogether and am trying to redo it with everything in Swing this time. Working so far. Thanks everyone for the help.
 
    Bookmark Topic Watch Topic
  • New Topic