1

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.

Ablia
  • 317
  • 1
  • 3
  • 14
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Aug 12 '18 at 15:43
  • 1) I did try to make it short, but well, it's a frame, there's a lot in it...plus, last time that i cut my code to let just the problematic part, people commended on the "missing parts". 2) Sure. However, this is just a "student work" with a time limit, and the UI is not important at all (my teacher was like "oh, you made an UI?"). Plus, this is my first time with Java and i already have to work with multithread, so i won't try to work with layout managers for now. – Ablia Aug 12 '18 at 16:00
  • 1) You're focusing too much on 'short' / 'minimal' and not enough on the rest of the phrases. An MCVE / SSCCE should be such that we can copy/paste, compile/run & see the problem with no changes. 2) Don't waste the time making a GUI if the instructor does not specify one. As you alluded, it is not impressive. Much better to focus on the core of the problem. – Andrew Thompson Aug 13 '18 at 01:07

1 Answers1

1

You can create two thread one for download and one for upload like below

public void actionPerformed(ActionEvent e){
   if(e.getSource()==uploadButton){
      new Thread(){
         public void run(){
          this.filename = uploadField.getText();
          File file = new File(filename);
          client.upload(file);
         }
      }.start();
   }
  else if(e.getSource() == downloadButton){
    new Thread(){
      public void run(){
        this.filename = downloadField.getText();
        File file = new File(filename);
        client.download(file);
      }
    }.start();
  }
}
lion_pankaj
  • 160
  • 1
  • 12