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);
}
}
Japplet
Page 1 of 110 Replies - 194 Views - Last Post: 04 October 2012 - 08:38 PM
#1
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.
Replies To: Japplet
#2
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
#3
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
#4
Re: Japplet
Posted 04 October 2012 - 07:43 PM
Right you are. Edited my embarrassing error. Thanks!
#5
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
This is just to display a JTable nothing to do with logic and relation or object interaction
#6
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...
why am i getting errors still. i know i have the html page set up right.
<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
#7
Re: Japplet
Posted 04 October 2012 - 08:00 PM
Which error ? We are supposed to guess it ?
#8
Re: Japplet
Posted 04 October 2012 - 08:04 PM
#9
Re: Japplet
Posted 04 October 2012 - 08:09 PM
Because your Names.class is not located in the same directory as your .html
#10
Re: Japplet
Posted 04 October 2012 - 08:25 PM
#11
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 ?
nNetbeans engineering came with a new standard ?
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|