10 Replies - 294 Views - Last Post: 04 October 2012 - 08:38 PM Rate Topic: -----

#1 Tomas1122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-October 12

Japplet

Posted 04 October 2012 - 07:29 PM

Hey anyone can help me turn JFrame into Applet so i can view it on a webpage. Heres my code thanks.


import java.util.Random;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;



/**
 *
 * 
 */

public class Names extends JFrame {
   

    
	String[][] names;
	
        //Names for the table header.
        String[] header = {"Name", "ID", "Majors", "GPA",};
        //First names
        String[] fnames = {"Mike","David","Jonathan","Peter","Paul","Nathan","Nicole","Eddie","Estelle","Emily","Joey","Aaron"};  
        //Last Names
        String[] lnames = {"Eddy","Muggli","Defourneaux"};  
        //Majors
        String[] majors = {"Math","Actuarial Science","Computer Science","Information Technology"};
        
        
        
	Names() {
		super("Names");
		Random ran = new Random();
		names = new String[50][4];
                //randomize it 50 times
		for(int i = 0; i < 50; ++i) {
                    int first = ran.nextInt(fnames.length);
                    int last = ran.nextInt(lnames.length);
                    int major = ran.nextInt(majors.length);
                    
                    //first coloum will be random names
                    names[i][0] = fnames[first] + " " + lnames[last];
                    //second coloum will be random ID
                    names[i][1] = ((int)(Math.random()*8999 + 1000)) + "";//"" + ran.nextInt(8999);
                    //third coloum will be random majors
                    names[i][2] = majors[(int)(Math.random()*majors.length)];
                    //forth coloum will be random GPA
                    names[i][3] = "" + ran.nextInt(4);
}

		JTable t = new JTable(names, header);
                //scrollpane for the table
		add(new JScrollPane(t));
                
               
	}
	
	public static void main(String[] args) {
		JFrame f = new Names();
		f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		f.setSize(300,200);
		f.setVisible(true);
                
     
        
	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: Japplet

#2 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1940
  • View blog
  • Posts: 4,029
  • Joined: 11-December 07

Re: Japplet

Posted 04 October 2012 - 07:32 PM

The approach I use is to code my gui to run in a JFrame JPanel. I can then put it into either a JFrame or a JApplet as I wish. There are other solutions available but they mostly revolve around coding everything as an applet and rigging a JFrame to call the appropriate methods.

This post has been edited by cfoley: 04 October 2012 - 07:42 PM

Was This Post Helpful? 2
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Japplet

Posted 04 October 2012 - 07:33 PM

You won't make a PHd thesis out of that one :)

public class Names extends JApplet {
	String[][] names;
	
        //Names for the table header.
        String[] header = {"Name", "ID", "Majors", "GPA",};
        //First names
        String[] fnames = {"Mike","David","Jonathan","Peter","Paul","Nathan","Nicole","Eddie","Estelle","Emily","Joey","Aaron"};  
        //Last Names
        String[] lnames = {"Eddy","Muggli","Defourneaux"};  
        //Majors
        String[] majors = {"Math","Actuarial Science","Computer Science","Information Technology"};
        
        
        public void init(){
		Random ran = new Random();
		names = new String[50][4];
                //randomize it 50 times
		for(int i = 0; i < 50; ++i) {
                    int first = ran.nextInt(fnames.length);
                    int last = ran.nextInt(lnames.length);
                    int major = ran.nextInt(majors.length);
                    
                    //first coloum will be random names
                    names[i][0] = fnames[first] + " " + lnames[last];
                    //second coloum will be random ID
                    names[i][1] = ((int)(Math.random()*8999 + 1000)) + "";//"" + ran.nextInt(8999);
                    //third coloum will be random majors
                    names[i][2] = majors[(int)(Math.random()*majors.length)];
                    //forth coloum will be random GPA
                    names[i][3] = "" + ran.nextInt(4);
}

		JTable t = new JTable(names, header);
                //scrollpane for the table
		add(new JScrollPane(t));
                
               
	}
	
}


This post has been edited by pbl: 04 October 2012 - 07:37 PM

Was This Post Helpful? 1
  • +
  • -

#4 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1940
  • View blog
  • Posts: 4,029
  • Joined: 11-December 07

Re: Japplet

Posted 04 October 2012 - 07:43 PM

Right you are. Edited my embarrassing error. Thanks!
Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Japplet

Posted 04 October 2012 - 07:46 PM

With no OO approach and every thing in main() not much to do
This is just to display a JTable nothing to do with logic and relation or object interaction
Was This Post Helpful? 0
  • +
  • -

#6 Tomas1122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-October 12

Re: Japplet

Posted 04 October 2012 - 07:55 PM

Thanks a lot pbl and cfoley. :). 1 more question. If i want to view the table made in java on webpage on my computer console and i put this code in html file...

 <applet code ="Names.class" width="500" height ="500">
</applet>
      


why am i getting errors still. i know i have the html page set up right.

This post has been edited by Tomas1122: 04 October 2012 - 07:56 PM

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Japplet

Posted 04 October 2012 - 08:00 PM

Which error ? We are supposed to guess it ?
Was This Post Helpful? 0
  • +
  • -

#8 Tomas1122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-October 12

Re: Japplet

Posted 04 October 2012 - 08:04 PM

View Postpbl, on 04 October 2012 - 08:00 PM, said:

Which error ? We are supposed to guess it ?


sorry. says ClassNotFoundException
Name.class
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Japplet

Posted 04 October 2012 - 08:09 PM

Because your Names.class is not located in the same directory as your .html
Was This Post Helpful? 0
  • +
  • -

#10 Tomas1122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-October 12

Re: Japplet

Posted 04 October 2012 - 08:25 PM

View Postpbl, on 04 October 2012 - 08:09 PM, said:

Because your Names.class is not located in the same directory as your .html


Still showing error. Im using netbeans html to do this if that makes a diff.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Japplet

Posted 04 October 2012 - 08:38 PM

What is the difference between Netbeans html and regular/normal/standard html ?

nNetbeans engineering came with a new standard ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1