i have a Frame which look like this:
public class Load_Frame extends JFrame implements ActionListener{
private JButton uploadButton, downloadButton;
private JTextField uploadField;
private String filename;
private Client client;
public Load_Frame(String username, Socket socket) {
this.client = new Client(username, socket);
uploadField = new JTextField ();
uploadField.setBounds(60,100,450,30);
uploadButton = new JButton ("Upload");
uploadButton.setBounds(410,150,100,30);
uploadButton.addActionListener(this);
downloadButton = new JButton ("Download");
downloadButton.setBounds(390,300,120,30);
downloadButton.addActionListener(this);
this.add(uploadField);
this.add(uploadButton);
this.add(downloadButton);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
//Upload:
if (e.getSource()== uploadButton) {
this.filename = uploadField.getText();
File file = new File(filename);
client.upload(file);
}
//Download
else if (e.getSource()== downloadButton) {
filename = (String) filesList.getSelectedItem();
client.download(filename);
}
}
My problem is: i have been said that the frame and the "process" should be separated in different thread, so that when the process fail the frame don't freeze. So i need my Client to be a new thread.
But then, i still need acess to those "upload" and "download" button. I've read that i can easily do that like it:
public class Client implements Runnable, ActionListener{
...
public void actionPerformed(ActionEvent e){
if(e.getSource() == uploadButton){
File file = new File(filename); //how can i retrieve the filename??
upload(file);
}
}
and i'll just need to add another actionListener in my Frame class like that:
uploadButton.addActionListener(client);
(same for download of course)
My prolem is: how can i get the filename, the text written in the TextField of my Frame?? Should i give this TextField as a parameter for my client ? This will make the code look weird, and by weird i mean not very logical, so i hope there is another way to do so.