aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JSlider not displaying Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JSlider not displaying" Watch "JSlider not displaying" New topic
Author

JSlider not displaying

Michelle Kyamo
Greenhorn

Joined: Aug 25, 2009
Posts: 18
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){
}

}
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

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).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
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

Joined: Aug 25, 2009
Posts: 18
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

Joined: Aug 25, 2009
Posts: 18
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?

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Oct 27, 2005
Posts: 19696
    
  20

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

Joined: Aug 25, 2009
Posts: 18
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.
 
Consider Paul's rocket mass heater.
 
subject: JSlider not displaying