0

I am trying to learn Java Swing. I am trying to copy input data from UI to a clipboard and write it to a text file.

I have the below code to copy it to a clipboard and to create a text file. But I am facing difficulty in copying the data from clipboard to text file.

Question: How do I pass argument to "Print Writer"? I was trying to use "Queue" as I have copied to it earlier. Am I wrong?

private void hCopysaveButActionPerformed(java.awt.event.ActionEvent evt) {                                             
   //JSpinner dateSpinner=hDateSpinner.get
   Date date=new Date();
   SimpleDateFormat form = new SimpleDateFormat ("E MM/dd/yyyy 'at' hh:mm:ss a zzz");
   String rock= krock.getText();
   String get= "Date&Time: "+form.format(date)+"\n"+"\n"+krocklabl.getText()+krock;
   StringSelection Queue= new StringSelection(get);
   Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
   clipboard.setContents(Queue, Queue);
   
   
   try{
   Date datetime=new Date();
   SimpleDateFormat form = new SimpleDateFormat ("E_MM-dd-yyyy_hh-mm-ss_a_zzz");
   FileWriter writer = new FileWriter("C:\\Driver\\"+form.format(date)+ ".txt");
       try (PrintWriter pw = new PrintWriter(writer)) {
           pw.println(+Queue);
           pw.close();
       }
  
   }    catch (IOException ex) {
            Logger.getLogger(Handoff.class.getName()).log(Level.SEVERE, null, ex);
        }
    }                                            

Modified Code:

 try{
       String data = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor); 
       Date datetime=new Date();
       SimpleDateFormat form = new SimpleDateFormat ("E_MM-dd-yyyy_hh-mm-ss_a_zzz");
       FileWriter writer = new FileWriter("C:\\Driver\\"+form.format(date)+ ".txt");
           try (PrintWriter pw = new PrintWriter(writer)) {
               pw.println(data);
               pw.close();
           }
      
       }    catch (IOException ex) {
                Logger.getLogger(Handoff.class.getName()).log(Level.SEVERE, null, ex);
            }
        }                                  

File gets created have input data like 

Date&Time: Wed 11/30/2016 at 08:16:16 PM ISTOvert # :User input not valid until its registered

But it should look like below.

Date&Time: Wed 11/30/2016 at 08:16:16 PM IST

Overt # : User input not valid until its registered

Community
  • 1
  • 1
sady
  • 301
  • 1
  • 7
  • 18
  • 1
    Possible duplicate of [Get readable text only from clipboard](http://stackoverflow.com/questions/7105778/get-readable-text-only-from-clipboard) – AxelH Nov 30 '16 at 06:59
  • 3
    And then, simply [use this](http://stackoverflow.com/questions/1053467/how-do-i-save-a-string-to-a-text-file-using-java) to save it. You should try one thing at the time ;) first try to get the value of the clipboard, print it in the console. Only then, you can try to save it to a file. – AxelH Nov 30 '16 at 06:59
  • @AxelH Thank you. I made a couple of changes to my code. Now I am able to create a text file with inputs. But one concern is input text what is copied to the text file is not formatted its all in one line. I would like to add next line ("\n"). – sady Nov 30 '16 at 09:05
  • @AxelH Code added in try block `String data = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor); ` and passed argument as `pw.println(data);` – sady Nov 30 '16 at 09:05
  • So, how do I append "\n" in the code so that input copied to file is user-friendly? – sady Nov 30 '16 at 09:06
  • 1
    Update the code in your question with what you have, and off course, show an example of that input – AxelH Nov 30 '16 at 09:16
  • @AxelH I have updated the question with modified code and output. Basically, I would need Date&time in one line and Overt in the next line. – sady Nov 30 '16 at 14:55
  • This is your clipboard that is wrong ... I tried you code (by copying the code in the clipboard) and I have the file with the correct format, `\n` and everything like expected. Just print the data into the console ... this should be the same. So check what is copy in the beginning – AxelH Nov 30 '16 at 14:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/129498/discussion-between-sady-and-axelh). – sady Dec 01 '16 at 06:34
  • @AxelH First part of my code (Before try block) will copy it clipboard as expected with \n. I tested it by clicking on a button to copy the input and I paste it on text pad. But, when I try to use the same to create a text file with input, that is when I get the input all in one line. Do you mean to say you have tested second part of the code (try block) and it worked for you, is that correct? – sady Dec 01 '16 at 06:48
  • @AxelH I managed to pass `string get` to `pw.println`. But File that gets created is not getting formatted with `\n` input comes all in one line. However, the first part of my initial code that copy's input from UI is working fine with `\n`. Where am I going wrong here? – sady Dec 04 '16 at 07:43
  • @AxelH I used `System.lineSeparator()` instead of `\n` in the string and that solved the issue. Thanks for your help – sady Dec 05 '16 at 05:19
  • I should have thought about it... you're welcome – AxelH Dec 05 '16 at 06:20

0 Answers0