Applet issues
Author |
Message |
Shades
|
Posted: Fri May 30, 2008 5:44 pm Post subject: Applet issues |
|
|
My applet itself runs when run through NetBeans, although when incorporated into it's html code, either the applet doesn't load (showing the applet window with an X) or loads (this is when I run the auto-generated NetBeans html code) but doesn't do what it's supposed to: it creates the progress bars and the button and everything, but does not download the files or show progress. So, is this an issue with my applet's structure or is it a case of me overseeing certain necessary commands?
code: | import java.net.*;
import java.io.*;
import java.awt.event.*;
public class dlProgressBar extends javax.swing.JApplet {
static int runCount1=0, runcount2=0, runcount3=0;
static String links[]={"http://mirror.silliam.com/dota/DotA%20Allstars%20v6.52c.w3x", "http://dota.ws/download/DotA%20Allstars%20v6.52c.w3x", "http://www.myklspace.com/dota/DotA%20Allstars%20v6.48b.w3x"};
/** Initializes the applet dlProgressBar */
@Override
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
jButtonDl1.addActionListener(new begindl());
}
public class begindl implements ActionListener{
public void actionPerformed (ActionEvent e){
jButtonDl1.setEnabled(false);
new Thread(new thread1()).start();
new Thread(new thread2()).start();
new Thread(new thread3()).start();
}
}
public class thread1 implements Runnable{
public void run(){
download1(links[0]);
}
}
public class thread2 implements Runnable{
public void run(){
download2(links[1]);
}
}
public class thread3 implements Runnable{
public void run(){
download3(links[2]);
}
}
public void download1(String address, String localFileName) {
runCount1=0;
OutputStream out = null;
URLConnection conn = null;
InputStream in = null;
try {
URL url = new URL(address);
out = new BufferedOutputStream(new FileOutputStream(localFileName));
conn = url.openConnection();
in = conn.getInputStream();
byte[] buffer = new byte[1];
int numRead=0;
jProgressBar1.setMaximum(conn.getContentLength());
//long numWritten=0;
while ((numRead = in.read(buffer)) != -1) {
out.write(buffer, 0, numRead);
//numWritten += numRead;
jProgressBar1.setValue(runCount1);
jProgressBar1.repaint();
runCount1++;
}
System.out.println("File #1 size:"+runCount1);
} catch (Exception exception) {
exception.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException ioe) {
}
}
}
public void download1(String address) {
int lastSlashIndex = address.lastIndexOf('/');
if (lastSlashIndex >= 0 &&
lastSlashIndex < address.length() - 1) {
download1(address, address.substring(lastSlashIndex + 1));
} else {
System.err.println("Could not figure out local file name for " +
address);
}
}
public void download2(String address, String localFileName) {
runcount2=0;
OutputStream out = null;
URLConnection conn = null;
InputStream in = null;
try {
URL url = new URL(address);
out = new BufferedOutputStream(new FileOutputStream("2"+localFileName));
conn = url.openConnection();
in = conn.getInputStream();
byte[] buffer = new byte[1];
int numRead=0;
jProgressBar2.setMaximum(conn.getContentLength());
//long numWritten=0;
while ((numRead = in.read(buffer)) != -1) {
out.write(buffer, 0, numRead);
//numWritten += numRead;
jProgressBar2.setValue(runcount2);
jProgressBar2.repaint();
runcount2++;
}
System.out.println("File #2 size:"+runcount2);
} catch (Exception exception) {
exception.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException ioe) {
}
}
}
public void download2(String address) {
int lastSlashIndex = address.lastIndexOf('/');
if (lastSlashIndex >= 0 &&
lastSlashIndex < address.length() - 1) {
download2(address, address.substring(lastSlashIndex + 1));
} else {
System.err.println("Could not figure out local file name for " +
address);
}
}
public void download3(String address, String localFileName) {
runcount3=0;
OutputStream out = null;
URLConnection conn = null;
InputStream in = null;
try {
URL url = new URL(address);
out = new BufferedOutputStream(new FileOutputStream(localFileName));
conn = url.openConnection();
in = conn.getInputStream();
byte[] buffer = new byte[1];
int numRead=0;
jProgressBar3.setMaximum(conn.getContentLength());
//long numWritten=0;
while ((numRead = in.read(buffer)) != -1) {
out.write(buffer, 0, numRead);
//numWritten += numRead;
jProgressBar3.setValue(runcount3);
jProgressBar3.repaint();
runcount3++;
}
System.out.println("File #3 size:"+ runcount3);
} catch (Exception exception) {
exception.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException ioe) {
}
}
}
public void download3(String address) {
int lastSlashIndex = address.lastIndexOf('/');
if (lastSlashIndex >= 0 &&
lastSlashIndex < address.length() - 1) {
download3(address, address.substring(lastSlashIndex + 1));
} else {
System.err.println("Could not figure out local file name for " +
address);
}
} |
Generated code:
code: |
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jProgressBar1 = new javax.swing.JProgressBar();
jProgressBar2 = new javax.swing.JProgressBar();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jProgressBar3 = new javax.swing.JProgressBar();
jLabel3 = new javax.swing.JLabel();
jButtonDl1 = new javax.swing.JButton();
jLabel1.setText("DotA%20Allstars%20v6.52c.w3x");
jLabel2.setText("DotA%20Allstars%20v6.52c.w3x");
jLabel3.setText("DotA%20Allstars%20v6.48b.w3x");
jButtonDl1.setText("Download");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jProgressBar2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 57, Short.MAX_VALUE)
.addComponent(jButtonDl1))
.addComponent(jLabel3)
.addComponent(jProgressBar3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(1, 1, 1)
.addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButtonDl1)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jProgressBar2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jProgressBar3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JButton jButtonDl1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JProgressBar jProgressBar1;
private javax.swing.JProgressBar jProgressBar2;
private javax.swing.JProgressBar jProgressBar3;
// End of variables declaration
} |
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
|
|