0

SEE UPDATE AT THE BOTTOM!!

I've tried to figured out how to do this for a couple of days but so far I have had no luck.

Basically what I want to do is have a combobox, which when an option is selected loads an applet, and passes a value to the applet. Here is the code for the ComboBox class, which is supposed to open the other class in a new window. The other class is the main class for an applet. They are both in the same project but in different packages. I know that there aren't any errors with the rest of the code.

 //where I evaluate the selection and then open SteadyStateFusionDemo
 // more selections just showing one code block
      combo.addItemListener(new ItemListener(){
      public void itemStateChanged(ItemEvent ie){
          String str = (String)combo.getSelectedItem();
               if (str.equals("NSTX")) {
                   machine = "A";
                   JFrame frame = new JFrame ("MyPanel2");
                   frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
                   SteadyStateFusionDemo d = new SteadyStateFusionDemo();
                   frame.getContentPane().add (new SteadyStateFusionDemo());
                   d.init();
                   frame.pack();
                   frame.setVisible (true);

And just to cover everything here is the beginning of the init() method of SteadyStateFusionDemo as well as the main method in the class. Too much code to post otherwise. There are several different privates before the init method.

    //method that initializes Applet or SteadyStateFusionDemo class       
    public void init() {

    //main method of the SteadyStateFusionDemo class
     public static void main (String[] args) {
          JFrame frame = new JFrame ("MyPanel");
          frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add (new SteadyStateFusionDemo());
          frame.pack();
          frame.setVisible (true);

What am I doing wrong? Why doesn't my class load?

UPDATED: Changed the code so that a JFrame opens and then the JApplet loads inside. I have successfully done this in a test Java applet but for some odd reason it won't work with this Applet. I even set up the test in a similar way (The code for this is virtually the same, except with different class names, and of course a much, much shorter init() method). Can someone help me figure out why this isn't working? Also, a JFrame will open if I delete the lines referring to SteadyStateFusionDemo, but once I reference it won't work. Why does this happen?

awwerk1
  • 23
  • 4
  • Are you getting any exceptions? – PM 77-1 Jun 05 '13 at 14:31
  • No, it just doesn't initialize at all. It's extremely frustrating because it worked with a test application that I'm doing but not in this case. Also when I run the SteadyStateFusionDemo class by itself it works fine, so I know that the problem is in the connection – awwerk1 Jun 05 '13 at 14:34
  • Do you have any *do nothing* catch blocks that could *mask* some or all exceptions? – PM 77-1 Jun 05 '13 at 14:45

1 Answers1

0

UPDATE:
Based on your feedback, it seems you are trying to achieve the following:
Use the code of an existing Applet (found here) in a Desktop application (i.e. in a JFrame).


Converting an Applet to a Desktop application is an "undertakable" task, the complexity of which depends on how much "Applet-specific" stuff is used by the Applet. It can be as simple as creating a JFrame and adding myFrame.setContentPane(new myApplet().getContentPane()); or as complex as...hell.
This tutorial might be a good place to start.


After taking a look at the Applet at hand, it seems to be fairly easy to convert it. The only complicating factor is the use Applet's methods getCodeBase() and getImage(URL) (somewhere in the code). These two methods result in a NullPointerException if the Applet is not deployed as...an Applet.

So, what you can do is override those two methods in order to return the intended values (without the exception). The code could look like this:

/* Import the necessary Applet entry-point */
import ssfd.SteadyStateFusionDemo;

/* Subclass SSFD to override "problematic" methods */
SteadyStateFusionDemo ssfd = new SteadyStateFusionDemo() {
    @Override
    public URL getCodeBase() {
        /* We don't care about the code-base any more */
        return null;
    }

    @Override
    public Image getImage(URL codeBase, String imgPath) {
        /* Load and return the specified image */
        return Toolkit.getDefaultToolkit().getImage(
                this.getClass().getResource("/" + imgPath));
    }
};
ssfd.init();

/* Create a JFrame and set the Applet as its ContentPane */
JFrame frame = new JFrame();
frame.setContentPane(ssfd);

/* Configure and show the JFrame */
...

The complete code for an example JFrame Class can be found here.


Of course, you need to have all Classes from the original Applet accessible to your new Class (e.g. put the original Applet in your classpath).

gkalpak
  • 47,844
  • 8
  • 105
  • 118
  • It still doesn't work. I've tried running it as a Java Application and Java applet in Eclipse but neither will load the other class. And only the Java applet will load a separate window, but it launches at the same time as the ComboBox. – awwerk1 Jun 05 '13 at 14:44
  • I added some more to the top, where I show how I evaluate the selections. – awwerk1 Jun 05 '13 at 14:47
  • Could it be possible that since one class uses an applet and the other does not that this would cause problems? I am trying to open a Java applet with a regular class. If so, how would I go about fixing this? – awwerk1 Jun 05 '13 at 15:42
  • 1
    Lol - this could be an issue, since the applets are shown in a browser or using `appletviewer.exe`, but "normal" classes are run using `java.exe`. How to fix this depends on what you are trying to achieve. Why are you using applets ? – gkalpak Jun 05 '13 at 16:36
  • They are going to be on a webpage. I have tried making the ComboBox class an applet, but now the other class, SteadyStateFusionDemo does not load. – awwerk1 Jun 05 '13 at 16:55
  • See http://stackoverflow.com/questions/2434483/how-to-open-new-applet-window-from-a-applet for some ideas. But keep in mind this is not the most simple/clean deployment concept. Make sure there is a good reason to complicate things like that. – gkalpak Jun 05 '13 at 17:04
  • I have now made a new window, which runs when select one of the choices but I want the SteadyStateFusionDemo class to run inside it. How would I go about doing that? – awwerk1 Jun 05 '13 at 17:58
  • If by "new window" you mean a JFrame, then you can't have SSFD (which is an applet) run in JFrame. First, yu need to modify SSFD to be normal desktop app (i.e. a JFrame) and then you can call it using this "new window" technique. – gkalpak Jun 05 '13 at 18:36
  • Do you think it could be possible for JApplet to run inside a JFrame? – awwerk1 Jun 05 '13 at 20:04
  • @awwerk1: No, it is impossible to run a JApplet inside a JFrame, since both are top-level containers. It should be very easy, though, to convert a JApplet to a JFrame (**[this tutotial](http://www.dreamincode.net/forums/topic/28410-application-to-japplet-and-reverse/)** might help). – gkalpak Jun 06 '13 at 08:00
  • I was able to do this for a test application but for some odd reason it won't work in this case. I know that I am getting closer to solving issue due to the success of the test app, but I think that something needs to be added or changed to open up the SteadyStateFusionDemo in a different window. What do you think I should do? (I made my edits to the code above, so you might want to look at that). – awwerk1 Jun 06 '13 at 20:26
  • @awwerk1: Sorry, I can't answer that if I don't see the code for SSFD. As I said before, you can't load a JApplet into a JFrame (or at least it is not a good idea). If your JApplet does not use any applet-specific stuff but just old, plain Swing stuff, you could try: `frame.setContentPane(new SteadyStateFusionDemo().getContentContentPane());`. But I really need to see code to know if anything else needs fixing. – gkalpak Jun 06 '13 at 20:55
  • Ok well my Applet is actually pretty complex (I am only building off an existing applet that someone else made). So, yeah I can't use the frame.setContentPane. I think what I might have to do is just link them through the html – awwerk1 Jun 07 '13 at 20:19
  • Hm...what do you mean by "complex". You might also want to look into **[Java Web Start](http://docs.oracle.com/javase/tutorial/deployment/webstart/)**. – gkalpak Jun 08 '13 at 06:12
  • Well this is the applet here http://ippex.pppl.gov/tokamak/tokamak.htm. As you can see it is pretty complex, as a lot of physics equations are involved in running the applet. What I want to do is launch this from a JFrame. I think what you have suggested is probably the way to go. However, I do have to pass a value from the JFrame to the applet. – awwerk1 Jun 10 '13 at 18:33
  • @awwerk1: I managed to get this Applet work as a Desktop application. It wasn't so difficult after all :) Please, see my updated (more like "majorly revamped") answer and let me know if you still face any difficulties. Hey, I almost forgot to mention you should not forget to mark the answer as accepted if it helped you solve your problem...blah, blah, blah... – gkalpak Jun 11 '13 at 15:04
  • Thank you! It works, but the buttons and images aren't loading. Do you know why that could be? The graph aspect of the applet does work however. – awwerk1 Jun 12 '13 at 15:31
  • I can see the buttons and images loading in my version. How do you deploy your app ? What is your directory structure ? – gkalpak Jun 12 '13 at 15:38
  • The ComboBox class, SSFDtoJFrame class and the SteadyStateFusionDemo class are in the same package. The images and buttons are created in a different package, but referenced in the SteadyStateFusionDemo. I kept a lot of the same structure from the original app. – awwerk1 Jun 12 '13 at 15:43
  • Then you have done something wrong :) You don't have to move SteadyStateFusionDemo class to another package. If you have modified the SSFD class, you might have broken something. Unfortunately, I can't guess what is wrong. If you could upload your project somewhere, I would take a look. – gkalpak Jun 12 '13 at 16:36
  • Even when I revert to the original code, the graphs are the only things that show... – awwerk1 Jun 12 '13 at 16:48
  • Like I said, I can't guess. I worked for me, it should work for you too, but I don't know what is wrong. Are JSRing it up ? – gkalpak Jun 12 '13 at 17:20
  • You should **not** use the appletviewer. That's the whole point. To convert it to a Desktop-application, so you **don't** need to run it as an Applet (in a browser or in appletviewer). – gkalpak Jun 13 '13 at 05:14
  • Excuse my mistake. I said that incorrectly. I've run it as an application and an applet, and the same thing happens. I only see the graphs and nothing else. – awwerk1 Jun 13 '13 at 13:08
  • Actually, it was kinda my fault. I updated my answer slightly (changed the two overriding methods) and also updated the **[example JFrame Class code](http://ideone.com/WCX3JI)**. Finally, I also prepared a ready to run .JAR (as a proof of concept), which can be found **[here](https://dl.dropboxusercontent.com/u/12617754/SOdemo/SSFDtoJFrame.jar)** (although it won't remain there forever, so download locally if you want to keep it) ;) – gkalpak Jun 13 '13 at 14:44
  • Did you download the .JAR from **[here](https://dl.dropboxusercontent.com/u/12617754/SOdemo/SSFDtoJFrame.jar)** and it does not work ??? It works for me :/ You just download the .JAR, double-click it and there it is !!! – gkalpak Jun 13 '13 at 15:03
  • Hmm... now it whenever I try to run SteadyStateFusionDemo it crashes (black screen appears but nothing displays). The first method at least showed one panel of the applet. – awwerk1 Jun 13 '13 at 15:10
  • You download the .JAR I linked to above, you double-click it with the mouse and it craches ??? – gkalpak Jun 13 '13 at 15:14
  • The .jar works, but when I run it as an application from eclipse it doesn't work. Am I missing something? – awwerk1 Jun 13 '13 at 15:16
  • Always glad to help :) (I was so close to desperation ;P) – gkalpak Jun 13 '13 at 15:38