I have a working application, but I need to convert it to an applet.
My main method isn't located in my frame class so I can't just extend JApplet
and change my main method to init()
.
Is there an easy way to "wrap" an applet
around an application.
Asked
Active
Viewed 279 times
0

Agustin Meriles
- 4,866
- 3
- 29
- 44

Zyvo
- 11
- 1
- 4
-
pushing an applet around an application – Mar 21 '13 at 14:26
-
Just initialize that (frame) class and make it visible in a new class which extends applet. – Sudhanshu Umalkar Mar 21 '13 at 14:26
-
ok but if i make my main class extend japplet it creates and applet with nothing in it and then my usual frame... – Zyvo Mar 21 '13 at 14:27
-
i did that but it didn't work – Mar 21 '13 at 14:27
-
If you need an applet, you need a class that extends JApplet or Applet, no workaround here I guess. You can make the frame visible in its constructor. – Sudhanshu Umalkar Mar 21 '13 at 14:36
-
Yes I know I need a class that extends it, but can I have a class that just adds panels to applets? – Zyvo Mar 21 '13 at 14:37
-
You should be able to do that if you have access to an instance of the applet. – Sudhanshu Umalkar Mar 21 '13 at 14:38
-
1Several hybrid examples are cited [here](http://stackoverflow.com/a/12449949/230513); also consider [tag:javawebstart]. – trashgod Mar 21 '13 at 16:23
1 Answers
1
I would separate out the guts of your UI creation, then call it either from main() or init(). See the below example:
public class Test extends Applet {
private JPanel mainPanel;
// run as application
public static void main(String[] args) {
Test test = new Test();
test.createUI();
JFrame frame = new JFrame();
frame.add(test.mainPanel);
frame.pack();
frame.setVisible(true);
}
// run as applet
public void init() {
createUI();
add(mainPanel);
}
// create your UI here
private void createUI() {
mainPanel = new JPanel();
mainPanel.add(new JButton("Test"));
}
}

martinez314
- 12,162
- 5
- 36
- 63
-
So this somewhat worked, but now when I run the program it doesn't set the correct size, which really doesn't matter because when on a webpage it'll be the size I set in the HTML code. The only problem is that when I go to export it through eclipse, it can't find a main class. It finds a main class when I want to run it, but not when I try to export it. – Zyvo Mar 22 '13 at 13:09
-
@RiFFRaFF If you run it from Eclipse, it should automatically create a Run Configuration. You should find this Run Configuration when you try to Export it, which is what points it to the main class. Can't think of a reason why this wouldn't work. – martinez314 Mar 22 '13 at 17:57