Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

Display JApplet as application...

The following code runs fine to display typical microwave oven front.

In place of:

public class TestPanel extends JFrame {

I like to start with:

public class TestPanel extends JApplet {

What changes I need to apply to this code to display it as an application.

Thank you.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
//import javax.swing.Timer;

public class TestPanel extends JFrame {
  
    public TestPanel() {
        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(4,2));
        
        for (int i=1; i<=9; i++) 
            p1.add(new JButton("" +i));
        
        p1.add(new JButton("" + 0));
        p1.add(new JButton("Start"));
        p1.add(new JButton("Stop"));
        
        JPanel p2 = new JPanel(new BorderLayout());
        p2.add(new JTextField("Time to be displayed here"), 
              BorderLayout.NORTH);
        p2.add(p1, BorderLayout.CENTER);
        
        add(p2, BorderLayout.EAST);
        add(new JButton("Food to be placed here"),
                BorderLayout.CENTER);
   }
    
    public static void main(String[] args) {
        TestPanel frame = new TestPanel();
        frame.setTitle("The Front View of a Microwave Oven");
        frame.setSize(400, 250);
        frame.setLocation(40, 60);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }    
}

Open in new window

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>What changes I need to apply to this code to display it as an application.

It's already an application
This extends JFrame and has main() methiod - it is an Apliction
Applet would be extending JApplet and would start execution withe the init() method
Avatar of Mike Eghtebas

ASKER

RE:> Applet would be extending JApplet and would start execution withe the init() method

Change:

public class TestPanel extends JFrame {

To:

public class TestPanel extends JApplet {

And run. It will not rune unless you add init() and the rest. These are changes I like to see.
SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You of course need to add events  etc.
applet.PNG
What changes did you do in the main()?

I have few red lines in there. It is not running yet.
It compiles without any problems and runs in appletviewer as you see

I  renamed you constructor as init() method and removed your main()
and it worked without problems.

Do you use my code?
If so which is diagrnostic on red lines?

Absolutetly No problem was for me

I  renamed you constructor as init() method and removed your main()
and it worked without problems.

and of course changed that it extends JApplet not JFrame
Can you please redefine your question?
So iow the question is the very reverse of the one (mostly) posted!?
re:> Can you please redefine your question?

Applet could be viewed in a web browser or using main() in. I want to view it keeping main (with necessary changes in it) to view this code that starts with:

public class TestPanel extends JApplet {

instead of:

public class TestPanel extends JFrame {



I want to keep main()
yiou can keep main if your goal is to confuse peopele  - just add it to the code like that:
public static void main(String [] args){

 TestPanel frame = new TestPanel();
}

 
for_yan,

I get the same result as you have posted after renaming the constructor to init() and removing main(). In the bottom of the screen identifies it as "Applet Started".

I suppose using main, we can display this applet in JFrame. That is what I am looking for. Sorry for bad bad question.
It is not only bad question - it is a wierd desire
Applet is applet and Appliactaion is application

Applet should be executed in the browser or appletvieweer
Application is dispalyed in a separate winodw.

If you want cat to bark - that just does not make sense.
No question is bad question. LOL

I changed to:

public static void main(String [] args){

 TestPanel frame = new TestPanel();
}

it doesn't work.

Code attached.


import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
//import javax.swing.Timer;

public class TestPanel extends JApplet {
    
    public void init() {
//    public TestPanel() {
        
        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(4,2));
        
        for (int i=1; i<=9; i++) 
            p1.add(new JButton("" +i));
        
        p1.add(new JButton("" + 0));
        p1.add(new JButton("Start"));
        p1.add(new JButton("Stop"));

        JPanel p2 = new JPanel(new BorderLayout());
        p2.add(new JTextField("Time to be displayed here"), 
              BorderLayout.NORTH);
        p2.add(p1, BorderLayout.CENTER);
        
        add(p2, BorderLayout.EAST);
        add(new JButton("Food to be placed here"),
                BorderLayout.CENTER);
   }
    
    public static void main(String[] args) {
        TestPanel frame = new TestPanel();
//        frame.setTitle("The Front View of a Microwave Oven");
//        frame.setSize(400, 250);
//        frame.setLocation(40, 60);
//        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        frame.setVisible(true);
    }    
}

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you start that instead with the following code, after compilation, you'll find you can do either


appletviewer TestPanel.java


or

java TestPanel
/*
<applet code="TestPanel" width="400" height="300"></applet>
*/

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.JApplet;

Open in new window

Why are you asking that if you already did it many times before - tihis wierd thing.

It just confuses people and nothing else
Thank you
:) It's quite normal to wish to run code as either an applet or application in fact

I think a bit of post factum tidying of the question could be a good thing though, so have pointed this out to the mods
re:>Why are you asking that if you already did it many times before

At time I fake my way to a solution. I get to solution without 100% understanding of it (I kind of postponed for a later time).

Considering I have started java only since June 2011, and with lots of desire to do lots of things with it, I have to revisit some basics once in a while just to make sure.

I this was in a school, the path could have been more smooth I guess but with self study I suppose there are areas I need to go back and make sure I understand the concept fully.

You have been a great help. I hope I have not disappointed you very much.

Regards,

Mike  
What miminum saving you gain having this code running in both ways - you lose ten times in the confusion which it creates.

Don't do that - don't invent something when there is no need for inveninting.

You already did it many times - I very much rememeber - you still cannot rememeber.
And no surprise - because  it is a weird cvombination.

Do it in straightforward and logical way and you'll remember much  and don't spend time asking questions, etc.
In that time you would have been able to make 5 variants of this code for each necessity.
re:> What miminum saving you gain having this code running in both ways.

There is a code to be developed using in an applet. And, it is supposed to be viewed  in a JFrame. In the past I had seen something like:

tp.init();
frame.getContentPane().add(tp);

used in main() but I couldn't recall it hence the question.

Mike
I had many cases where I first developed application and then transferred  it to the applet.

It takes usually 10 minutes to change if you need.

(and I never lose any previous version - so in such cases you keep your code as application intact
and have another code as applet).

No sense to confuse yourself with these weird combinations.

We have encounterd with you it already not the first time and some codes before also worked - I remember first time
I was surpriesd by that.
Neverthless you could not reemeber how to do it.

I could not rememeber it immediately either. All these complications only make simple things look difficult and confusing.



re> No sense to confuse yourself with these weird combinations.

I am guilty as charged in many points you bring up. But, in all honesty, this question stated I have a JFrame and want to change it to JApplet and run as application (meaning main() will be used).

re:> No sense to confuse yourself...

I suppose after a while, when I am comfortable with java, confusion will gradually fade out. But until then, I will do my best.

At the end, I appreciate all the help I have received from you and I hope it will continue to be so.

Thank you,

Mike
But there is absolutely no reason to do this:

>I have a JFrame and want to change it to JApplet and run as application (meaning main() will be used).

If you have JFrame run it as application

If you need to run it as Applet - have it as applet and run it as applet - that is what majority of peopel are doing

>I suppose after a while, when I am comfortable with java, confusion will gradually fade out

No, it will not. Those things which you use all the time, which are straightforward will become more comfortable of course.

Those oddities will alwasy be a problem - you'll ned to remember them every time or search for them somehwre in your archive

Even worse, once you'll look at your own code and will think, why the hell I needed to do it in such convoluted
way, so I can't understand mayself how it was done.

Not even talking about someone else who may need to read your code.

The simpler - the better, no doubt about it, even if you use more lines of code.



re:> But there is absolutely no reason to do this:

As I mentioned before, there is a problem requires this.

The whole chapter is about applet but asks to use main(). This is the reason for the question.

It is not that I had the solution and can find it. I mentioned I had seen the solution but I don't recall how it was.

I hope I don't come across defending myself. I never ever want to be in your s**t list.

Mike