Good day all!
Please I need someone to help me out by telling me what or how will I call up an Applet from a separate Java file that has a main method in it...thats to say, I will compile and run the Applet code from the other class with a main method in it.

Thanks alot
Loveday

Edited 6 Years Ago by Loveday: n/a

I will compile and run the Applet code from the other class with a main method

There is an interface: JavaCompiler for compiling a java program from within a java program.
After the code is compiled and the .class file is available, you can create an instance of the class and call its methods the same way a browser would call the methods. For full applet support, you need to provide an AppletContext for the Applet to use.

Can you explain some more what you are trying to do?

There is an interface: JavaCompiler for compiling a java program from within a java program.
After the code is compiled and the .class file is available, you can create an instance of the class and call its methods the same way a browser would call the methods. For full applet support, you need to provide an AppletContext for the Applet to use.

Can you explain some more what you are trying to do?

Thanks for your quick response. Actually, I have two files, an Applet and the other one with a main method, just for the sole purpose of calling up the applet file. How can I implement it in that class with main method?
Thanks
Alam Loveday

How can I implement it

Please explain "implement it"? What is it you want to do?
My previous post laid out the steps you'd take for the way you wrote the original question.

Please explain "implement it"? What is it you want to do?
My previous post laid out the steps you'd take for the way you wrote the original question.

I want the class with a main method to call an Applet file(that is, execute it). What method can I use in the main method class to execute the applet code? The files are attached here. Pls have a look.
Thanks
Alam Loveday

Attachments
//<applet code=SortNumber.class width=600 height=400></applet>

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.JButton;
//import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import java.applet.Applet;
import java.applet.AppletContext;
import java.net.MalformedURLException;
import java.net.URL;


public class SortNumber extends Applet implements ActionListener{

    private static JTextField text[];
    private static JPanel mainPanel,textPanel,panelStart,southPanel;
    private static JTextArea textArea;
    private static JButton buttonStart,buttonOk;;

      public SortNumber() {

        mainPanel = new JPanel();
        textPanel = new JPanel();
        panelStart = new JPanel();
        southPanel = new JPanel();

        buttonStart = new JButton("Start Sorting");
        buttonOk = new JButton("View Numbers");
        buttonStart.addActionListener(this);
        buttonOk.addActionListener(this);

        textArea = new JTextArea(7,40);

        text = new JTextField[10];
        text[0] = new JTextField(3);
        text[1] = new JTextField(3);
        text[2] = new JTextField(3);
        text[3] = new JTextField(3);
        text[4] = new JTextField(3);
        text[5] = new JTextField(3);
        text[6] = new JTextField(3);
        text[7] = new JTextField(3);
        text[8] = new JTextField(3);
        text[9] = new JTextField(3);

        for(int i=0;i<text.length;i++){
            textPanel.add(text[i]);
        }

        buttonStart.setEnabled(false);

        panelStart.add(buttonOk);
        panelStart.add(buttonStart);

        mainPanel.setLayout(new BorderLayout());
        mainPanel.add(textPanel,BorderLayout.NORTH);
        mainPanel.add(panelStart,BorderLayout.CENTER);
        mainPanel.add(southPanel,BorderLayout.SOUTH);

        southPanel.add(new JScrollPane().add(textArea));

        getContentPane().add(mainPanel);
        setSize(500,250);
        //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

	

	public void start(){
	URL codeBase = getCodeBase();
	AppletContext context = getAppletContext();
	
	try{
 
			URL url = new URL(codeBase + "AppletContextExample.html");
			context.showDocument(url);
 
		}catch(MalformedURLException me){
			//do nothing
		}
	}

    
    //public static void main(String[] args) {
//        new SortNumber();
//    }

    public void actionPerformed(ActionEvent e) {
        Object obj = e.getSource();
        if(obj == buttonStart){
            buttonOk.setEnabled(!false);
            buttonStart.setEnabled(false);

            //textArea.setText(sort());
            textArea.append(sort());
        }
        
        if(obj == buttonOk){
            buttonOk.setEnabled(false);
            buttonStart.setEnabled(!false);

            textArea.setText("  [ "+getNumbers()+ " ]"+"\n\n");
            
        }

    }

    private static String getNumbers(){
        String numbers = "";
        for(int i=0;i<text.length;i++){
            numbers = numbers+text[i].getText().trim()+", ";
        }

        return numbers.trim();
    }

    private static String sort(){

        int t0 = Integer.valueOf(text[0].getText().trim());
        int t1 = Integer.valueOf(text[1].getText().trim());
        int t2 = Integer.valueOf(text[2].getText().trim());
        int t3 = Integer.valueOf(text[3].getText().trim());
        int t4 = Integer.valueOf(text[4].getText().trim());
        int t5 = Integer.valueOf(text[5].getText().trim());
        int t6 = Integer.valueOf(text[6].getText().trim());
        int t7 = Integer.valueOf(text[7].getText().trim());
        int t8 = Integer.valueOf(text[8].getText().trim());
        int t9 = Integer.valueOf(text[9].getText().trim());

        int[] xx = {t0,t1,t2,t3,t4,t5,t6,t7,t8,t9};
        
        Arrays.sort(xx);
        return Arrays.toString(xx);

    }

}
public class SortNumber2
{
	public static void main(String[] args) {
        SortNumber s=new SortNumber();
    }
}

To call a method in another class:
1) get/create a reference to an instance of the class:
AnApplet anApplet = new AnApplet(); // create an instance of the class

2)To call a method in that class, use the reference created above:
anApplet.aMethod(); // call a method in the class

to call an Applet file(that is, execute it)

What do you mean by "call" and by "execute"?
When an applet executes in a browser, the browser provides support via an AppletContext class to the applet. If you don't need any "applet" support, then you can ignore that.
If you do, then you you need to provide an AppletContext object to the applet you are executing.

To call a method in another class:
1) get/create a reference to an instance of the class:
AnApplet anApplet = new AnApplet(); // create an instance of the class

2)To call a method in that class, use the reference created above:
anApplet.aMethod(); // call a method in the class


What do you mean by "call" and by "execute"?
When an applet executes in a browser, the browser provides support via an AppletContext class to the applet. If you don't need any "applet" support, then you can ignore that.
If you do, then you you need to provide an AppletContext object to the applet you are executing.

I have done as you directed but am still having some issues. You can have a glance to see with what I have done so far in the attached file. Thanks for your efforts.

Attachments
//<applet code=SortNumber.class width=600 height=400></applet>

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.JButton;
//import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import java.applet.Applet;
import java.applet.AppletContext;
//import java.net.MalformedURLException;
import java.net.URL;


public class SortNumber extends Applet implements ActionListener{

    private static JTextField text[];
    private static JPanel mainPanel,textPanel,panelStart,southPanel;
    private static JTextArea textArea;
    private static JButton buttonStart,buttonOk;
    Applet SortNumber=new Applet();



       public SortNumber(){

        mainPanel = new JPanel();
        textPanel = new JPanel();
        panelStart = new JPanel();
        southPanel = new JPanel();

        buttonStart = new JButton("Start Sorting");
        buttonOk = new JButton("View Numbers");
        buttonStart.addActionListener(this);
        buttonOk.addActionListener(this);

        textArea = new JTextArea(7,40);

        text = new JTextField[10];
        text[0] = new JTextField(3);
        text[1] = new JTextField(3);
        text[2] = new JTextField(3);
        text[3] = new JTextField(3);
        text[4] = new JTextField(3);
        text[5] = new JTextField(3);
        text[6] = new JTextField(3);
        text[7] = new JTextField(3);
        text[8] = new JTextField(3);
        text[9] = new JTextField(3);

        for(int i=0;i<text.length;i++){
            textPanel.add(text[i]);
        }

        buttonStart.setEnabled(false);

        panelStart.add(buttonOk);
        panelStart.add(buttonStart);

        mainPanel.setLayout(new BorderLayout());
        mainPanel.add(textPanel,BorderLayout.NORTH);
        mainPanel.add(panelStart,BorderLayout.CENTER);
        mainPanel.add(southPanel,BorderLayout.SOUTH);

        southPanel.add(new JScrollPane().add(textArea));

        add(mainPanel);
        setSize(500,250);
        //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }



    @Override
	public void start(){
	//URL codeBase = getCodeBase();
        String ss="SortNumber";
//        AppletContext context = getAppletContext();
//        Applet  AppletInstance = context.getApplet(getCodeBase().getPath());
	//String dd=getCodeBase().getPath();
	try {
    URL u = new URL (getDocumentBase(), " SortNumber.java");
    getAppletContext().showDocument (u, "_blank");
} catch (Exception e) {
}
	}


//    public static void main(String[] args) {
//        new SortNumber();
//    }

    public void actionPerformed(ActionEvent e) {
        Object obj = e.getSource();
        if(obj == buttonStart){
            buttonOk.setEnabled(!false);
            buttonStart.setEnabled(false);

            //textArea.setText(sort());
            textArea.append(sort());
        }

        if(obj == buttonOk){
            buttonOk.setEnabled(false);
            buttonStart.setEnabled(!false);

            textArea.setText("  [ "+getNumbers()+ " ]"+"\n\n");

        }

    }

    private static String getNumbers(){
        String numbers = "";
        for(int i=0;i<text.length;i++){
            numbers = numbers+text[i].getText().trim()+", ";
        }

        return numbers.trim();
    }

    private static String sort(){

        int t0 = Integer.valueOf(text[0].getText().trim());
        int t1 = Integer.valueOf(text[1].getText().trim());
        int t2 = Integer.valueOf(text[2].getText().trim());
        int t3 = Integer.valueOf(text[3].getText().trim());
        int t4 = Integer.valueOf(text[4].getText().trim());
        int t5 = Integer.valueOf(text[5].getText().trim());
        int t6 = Integer.valueOf(text[6].getText().trim());
        int t7 = Integer.valueOf(text[7].getText().trim());
        int t8 = Integer.valueOf(text[8].getText().trim());
        int t9 = Integer.valueOf(text[9].getText().trim());

        int[] xx = {t0,t1,t2,t3,t4,t5,t6,t7,t8,t9};

        Arrays.sort(xx);
        return Arrays.toString(xx);

    }

}
import java.applet.Applet;

public class SortNumber2 extends Applet
{
	public static void main(String[] args) {
	SortNumber s = new SortNumber();
		s.start();
    }
}

but am still having some issues

Please explain what you have tried, what the issues are and show full text of any errors.

This article has been dead for over six months. Start a new discussion instead.