I have written an applet program but it does not work. I get a blank page. Is there anything else i have to set/change . I have installed java plug-in and jdk in the same folder(called myjava). The two files(LPCTEST.html and testing4.java) are also in myjava.And ordinary applications are running fine.
APPLET CODE import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; //import java.lang.Object; public class testing4 extends JApplet { private JTextArea display; private JScrollPane scrollPane2; private JButton paste; private JPanel P2; private File fileName; public void init() { paste=new JButton("paste"); paste.setMinimumSize(new Dimension(90,20)); paste.setPreferredSize(new Dimension(90,20)); paste.setMaximumSize(new Dimension(90,20)); copyhandler pa=new copyhandler(); paste.addActionListener(pa); display=new JTextArea(); scrollPane2=new JScrollPane(display); scrollPane2.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); scrollPane2.setMinimumSize(new Dimension(200,58)); scrollPane2.setPreferredSize(new Dimension(200,58)); scrollPane2.setMaximumSize(new Dimension(200,58)); P2=new JPanel(); P2.setMinimumSize(new Dimension(200,80)); P2.setPreferredSize(new Dimension(200,80)); P2.setMaximumSize(new Dimension(200,80)); P2.setLayout(new BoxLayout(P2,BoxLayout.Y_AXIS)); scrollPane2.setAlignmentX (Component.LEFT_ALIGNMENT); P2.add(scrollPane2); paste.setAlignmentX(Component.LEFT_ALIGNMENT); P2.add(paste); Container c=getContentPane(); c.add(P2); } //COPIES TEXT FROM FILE ON MY DISK TO THE TEXTAREA private class copyhandler implements ActionListener { public void actionPerformed(ActionEvent event) { JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY ); int result = fileChooser.showOpenDialog(testing4.this); // user clicked Cancel button on dialog if ( result == JFileChooser.CANCEL_OPTION ) return; fileName = fileChooser.getSelectedFile();
if ( fileName == null || fileName.getName().equals( "" ) ) JOptionPane.showMessageDialog( null,"Invalid File Name", "Invalid File Name", JOptionPane.ERROR_MESSAGE ); else { try { FileReader in = new FileReader(fileName); BufferedReader reading= new BufferedReader(in); String store=""; while (reading.ready()) { StringTokenizer st = new StringTokenizer(reading.readLine()); store=store+st.nextToken()+"\n"; } display.setText(store); } catch ( IOException e ) { JOptionPane.showMessageDialog( null, "Error Opening File", "Error", JOptionPane.ERROR_MESSAGE ); } } } } public static void main (String [] args)
{ testing4 done =new testing4(); //you did not set applet's width and height: //applet is a container too done.setWidth(205); done.setHeight(110); JFrame frame = new JFrame("testing"); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); done.init(); frame.getContentPane().add(done); frame.pack(); frame.setSize(205,110); frame.setVisible(true); } }
hi, may be there are more bugs, but one thing why it doesnt work is that you want to load a file from your harddisk. applets dont have the permission the read or write files to the harddisk until they are signed. go to the java homepage to read more about signed applets.