Hello everyone,
Actually I want to learn more about thread, can we have more than one "run" for thread in a program ?
package ignisftpv20;
import javax.swing.JFileChooser;
import java.io.*;
import java.net.*;
import javax.swing.JOptionPane;
public class FTPClient extends javax.swing.JFrame implements Runnable {
JFileChooser fileChooser = new JFileChooser();
File fileUpload;
ObjectInputStream input;
ObjectOutputStream output;
FileInputStream fis;
FileOutputStream fout;
File file;
public FTPClient() {
initComponents();
new Thread( this ).start();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
btnBrowse = new javax.swing.JButton();
btnSend = new javax.swing.JButton();
btnConnect = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("FTP Client");
btnBrowse.setText("Browse");
btnBrowse.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnBrowseActionPerformed(evt);
}
});
btnSend.setText("Send File");
btnSend.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSendActionPerformed(evt);
}
});
btnConnect.setText("Connect");
btnConnect.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnConnectActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(91, 91, 91)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnSend)
.addComponent(btnBrowse)
.addComponent(btnConnect))
.addContainerGap(107, Short.MAX_VALUE))
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnBrowse, btnConnect, btnSend});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(65, 65, 65)
.addComponent(btnConnect)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btnBrowse)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnSend, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(113, Short.MAX_VALUE))
);
layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btnBrowse, btnConnect, btnSend});
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-281)/2, (screenSize.height-327)/2, 281, 327);
}// </editor-fold>
//browse
private void btnBrowseActionPerformed(java.awt.event.ActionEvent evt) {
int flag = fileChooser.showOpenDialog(FTPClient.this);
if (flag == JFileChooser.APPROVE_OPTION) {
fileUpload = fileChooser.getSelectedFile();
}
}
//connect
private void btnConnectActionPerformed(java.awt.event.ActionEvent evt) {
try{
Socket s = new Socket ("127.0.0.1" , 5555);
input = new ObjectInputStream(s.getInputStream());
output = new ObjectOutputStream(s.getOutputStream());
JOptionPane.showMessageDialog(null, "OK Houston");
}catch(Exception e){e.printStackTrace();}
}
//send file
private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {
new Thread(this).start();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FTPClient().setVisible(true);
}
});
}
public void run(){
sendBytes();
}
public void sendBytes(){
try{
fis = new FileInputStream(fileUpload.getName());
byte[] buffer = new byte[1024];
int bytes = 0;
while((bytes = fis.read(buffer))!=-1)
{
output.write(buffer,0,bytes);
}
}catch(Exception e){e.printStackTrace();}
}
// Variables declaration - do not modify
private javax.swing.JButton btnBrowse;
private javax.swing.JButton btnConnect;
private javax.swing.JButton btnSend;
// End of variables declaration
}
I want to run everything in button Connect, btnConnect
//connect
private void btnConnectActionPerformed
.
.
.
in a new thread.
How to do that, thanks.