Hey I'm trying to put a program I wrote into an applet and then host it on a website. However when I upload it I can get my first bit of the program to run but after that it refuses to repaint and just clears the screen entirely. I'm not sure why but I'm also running blind trying to teach myself all this so I guess that isn't a surprise. Any help would be appreciated. I took out a lot of the code to try and find where the error is so this is what's left. I was originally trying to do this with a JButton instead of just a Button but that didn't work at all so I switched.
Basically what happens is my program runs and opens up three text fields for input and then a button to press when finished. Pressing the button (on my machine) continues the rest of the program but as I said on the website it just clears the screen. I found out that it was actually my resizing refreshing the screen on my machine but since my website doesn't do that it apparently doesn't update. I shouldn't have to resize a window to just refresh the applet. What am I doing wrong?
Welcome to JavaRanch. Several points, in no particular order:
Don't mix Swing components (those starting with "J") with AWT components (those without a "J"). If the applet extends java.applet.Applet, then you should use AWT components, if it extends javax.swing.JApplet, then you should use Swing components.
If you're using Swing, then you should override paintComponent, not paint. Painting in AWT and Swing explains this in detail.
Unless you're doing custom painting (and it sounds as if you don't), there's no need to have a paint or paintComponent method at all. Let Swing do the work instead.
Any time you're adding/removing components, you should call the invalidate() method to notify Swing that the GUI needs to be layed out again; this will in turn lead to the GUI being repainted, so you don't need to call that.
If I have a GUI that changes substantially (like where a complete panel is removed or added), I prefer to use a java.awt.CardLayout. That way I can simply switch between cards as needed instead of having to add and remove components repeatedly.
ok now I have another problem. It puts in the test JLabel I have but the scrollbars won't come in now will the two other JPanels i'm trying to put in with it. Are scrollbars not allowed in an applet or something?
Joined: Mar 22, 2005
Not sure what's going on (the code doesn't compile), but you're adding "scroller" twice - nothing good can come of that. You're also adding "bpanel.getPanel()", even though you've already added "scroller" - which embeds bpanel; that's bound to cause problems as well.
Also note that the calls to "applet.setSize" won't work in a browser,. where the applet size is fixed to the parameters in the APPLET tag.