2

Here is my simple application which I built just to show my issue (original app is a huge, but issue is the same).

The problem is I can not show the JApplet (and can't show anything inside it too). Working in Netbeans, Java SE. Can you tell me what I've missed?

package testjava;

import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;

/**
 *
 * @author Epsilon
 */
public class TestJava extends JApplet implements Runnable {

    protected Thread ivThread;

    protected JPanel p1, p2;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        JApplet applet = new TestJava();
        applet.init();

        applet.start();

    }

    @Override
    public void init() {
        super.init();

        setSize(880, 710+28);

        setLayout(new BorderLayout());
        setVisible(true); 

        p1 = new JPanel();
        add(p1, BorderLayout.CENTER);

        p2 = new JPanel();
        add(p2, BorderLayout.SOUTH);

        p1.requestFocus();

    }

    @Override
    public void start()
    {
        if(ivThread == null)
        {
            ivThread = new Thread(this, "TestJava");
            ivThread.start();
        }
    }

    @Override
    public void run() {
System.out.println("Hello from Thread!");
        Component b1 = new JButton("Click Me");
        p1.add(b1);
    }

}
Epsiloncool
  • 1,435
  • 16
  • 39
  • Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). – Andrew Thompson Oct 11 '14 at 22:30
  • 1) The `main` of this hybrid does not put anything on-screen BTW. 2) `setSize(880, 710+28);` The size of an applet should be set in HTML or JNLP, it should not set its own size. – Andrew Thompson Oct 11 '14 at 22:32
  • @AndrewThompson Thanks for your comments. I need this to be shown in HTML, but at the developing stage I need this to be shown in Netbeans debug window also. – Epsiloncool Oct 11 '14 at 22:39
  • @AndrewThompson I can't use any other technology (HTML5 Canvas, Flash etc) because they have too much limitations (for example I can't catch mouse and I can't use special keyboard shortcuts like Ctrl+R. Etc. – Epsiloncool Oct 11 '14 at 22:42
  • Following on from point 1) in my 2nd comment. Change `applet.start();` to `applet.start(); JOptionPane.showssageDialog(null, applet);`.. – Andrew Thompson Oct 11 '14 at 22:52
  • @AndrewThompson what is JOptionPane? It shows me errors. – Epsiloncool Oct 11 '14 at 22:56
  • *"It shows me errors."* Holy crap. I'd have thought by 390 rep. you'd know better than to try and have us *guess* what the errors are. Always copy/paste error and exception output! *"what is JOptionPane?"* What is [JavaDocs](http://docs.oracle.com/javase/8/docs/api/)? – Andrew Thompson Oct 11 '14 at 23:04
  • As an aside, applets are a lot harder to develop than (GUI based) desktop apps. If you don't even know about the Java Docs, it indicates you don't have anywhere near enough experience to achieve a working (deployed) applet. – Andrew Thompson Oct 11 '14 at 23:09
  • @AndrewThompson I just wanted to tell you that there is possible error in the code you gave. You proposed to change a line of code to another line. JOptionPane.showssageDialog(null, applet); - here is error in method name. I guess StackOverflow editor eaten a lot of the code you put, right? – Epsiloncool Oct 11 '14 at 23:32
  • @AndrewThompson I am still learning Java, that huge app I spoken about is not developed by me. I tried to find an answer in Java Docs and in Google first, but no success. Thats why I came here. – Epsiloncool Oct 11 '14 at 23:33
  • @AndrewThompson I don't need to have GUI application. This app is an applet showing itself in HTML page and emulating one of the retro-platform. SO there is only 2 GUI elements, no need more. Most actions doing in Canvas of one of these JPanel's. Here is it btw: http://viva-games.ru/game/exolon Push one of these 4 yellow buttons. Thanks. – Epsiloncool Oct 11 '14 at 23:38
  • `JOptionPane.showssageDialog(null, applet);` Oh, my bad. It should be **`JOptionPane.showMessageDialog(null, applet);`** – Andrew Thompson Oct 11 '14 at 23:39
  • @AndrewThompson It works. How I can see applet's main window instead? Do you mean applet has not a window itself and I should create anything (for example JFrame) to show anything there? – Epsiloncool Oct 11 '14 at 23:43
  • 1
    *"I should create anything (for example JFrame) to show anything there?"* Yes, but an option pane is easier. Something I should point out. Noting you mention Netbeans, this (making an hybrid, with a `main`) is not how an IDE would normally run an applet. If we set up an applet based project, the IDE would typically launch the applet in the applet viewer with no security manager. ..I don't try to provide support for IDEs though. – Andrew Thompson Oct 12 '14 at 00:33
  • 2
    See also [*What's Java Hybrid—Applet + Application?*](http://stackoverflow.com/q/12449889/230513). – trashgod Oct 12 '14 at 04:07

1 Answers1

1

I've used JApplet + JFrame solution (enabling JFrame only in debug mode) and it works fine in both ways.

Epsiloncool
  • 1,435
  • 16
  • 39