1

I'll start by saying that I'm a bit new to java, so if it's a stupid mistake, that's the reason.

I was messing with applets in java, and I've run into a problem when setting the size of the window, and using JOptionPane, whether it's just showing a message, or getting input through it, it displays three or more panes, rather than simply one. I'm on Ubuntu 12.04(x64), and here is my test code that still encounters the problem:

import javax.swing.JOptionPane;
import java.awt.Graphics;
import java.applet.Applet;

public class Main extends Applet{

    public void paint(Graphics g)
    {
        //setup screen size
        setSize((int)500,(int)500);
        //print test message
        JOptionPane.showMessageDialog(null, "There Should Be One Of Me!");
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
John
  • 25
  • 4
  • That's expected: there are as many option panes as number of calls to the paint method :-) the latter is not controllable by application code, neither in AWT nor in Swing. Never-ever change any component state in methods called during the paint cycle. – kleopatra Jul 08 '12 at 09:54
  • Ok, thats the answer I was looking for, if you could put that in an answer, that would be great... – John Jul 08 '12 at 17:59
  • *"I was messing with applets in java, and I've run into a problem when setting the size of the window"* That will not work reliably in the web page. Abandon it and try something useful. And use a `JApplet`. Either that or make it a `JFrame` and launch the frame from a link using [JWS](http://stackoverflow.com/tags/java-web-start/info). – Andrew Thompson Jul 09 '12 at 12:29
  • ok, I will definately do that – John Jul 12 '12 at 19:54

2 Answers2

2

That's expected: there are as many option panes as number of calls to the paint method :-) the latter is not controllable by application code, neither in AWT nor in Swing.

As a general rule, never-ever change any component state in methods called during the paint cycle

kleopatra
  • 51,061
  • 28
  • 99
  • 211
0
  1. remove setSize(.. and JOptionPane.sh... from public void paint(Graphics g)

  2. for Applet I miss there method init()

  3. (J)Applet tutorial shows a few examples

  4. maybe better could be to use Swing JFrame instead of (J)Applet

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • For reference, there's a hybrid applet/application [here](http://stackoverflow.com/a/11372932/230513). – trashgod Jul 07 '12 at 18:38
  • not sure what's wrong, maybe temperature in the Central Europe ???, hmmm but I love > 32 degrees by Celsius to much – mKorbel Jul 08 '12 at 11:42
  • aaaach I seeeeeeeee too much links to the external sites :-) – mKorbel Jul 08 '12 at 12:03
  • no, too many (all except the first) unrelated bullets. Still trying to nudge you into staying focused on the problem at hand instead of mentioning whatever comes to your mind :-) – kleopatra Jul 08 '12 at 18:11