0

[![enter image description here][1]][1]I am new to Java Swing. I have a create a GUI layout will 2 text areas and a button "copy to clipboard". I have a code that will copy the contents of first text area to clipboard, but not sure how to add the content in second text area and the labels corresponding to jtext area.

String get= hActionText.getText();
   StringSelection selec= new StringSelection(get);
   Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
   clipboard.setContents(selec, selec);
Manu
  • 83
  • 1
  • 2
  • 8
  • Possible duplicate of [Get readable text only from clipboard](http://stackoverflow.com/questions/7105778/get-readable-text-only-from-clipboard) – Arnaud Nov 10 '16 at 15:35
  • Please elaborate more. The question states "to copy text area to clipboard", but in the text you ask something else and mention another text area and something you wish to do with it. – Aleksandar Stojadinovic Nov 10 '16 at 15:35
  • You can only add one piece of data at a time to the clipboard. Any new data you add will replace the existing data. – camickr Nov 10 '16 at 15:36
  • @AleksandarStojadinovic I changed the question now. I believe it suits my question now.. – Manu Nov 10 '16 at 15:38
  • @camickr Cant i copy the contents from 2 text areas at the same time? – Manu Nov 10 '16 at 15:39
  • You can put any string you want into the clipboard. If you want the text from two text areas then you need to get the text from each text area and concatenate the text into one string. – camickr Nov 10 '16 at 15:40
  • @camickr If I understand you correctly you want to put content from two fields on the clipboard, and potentially now which part of the content came from which field, and possibly populate them again? – Aleksandar Stojadinovic Nov 10 '16 at 15:43
  • @camickr thank you, can you please help me with concatenation? – Manu Nov 10 '16 at 15:44
  • @AleksandarStojadinovic Exactly, you understood it correctly. – Manu Nov 10 '16 at 15:45

2 Answers2

0

If I understood what are you trying, you are trying to put the values of both of your fields on the clipboard, and than read them and populate the fields again.

The clipboard is way too simple for that, it can only hold one String basically. What I propose is to create a structure you will put on the clipboard, and which structure is better for describing data as a String than JSON :-) . Just create JSON content like this:

[
    {
       "label":"field1",
       "content":"contentFromField1"
    },
    {
       "label":"field2",
       "content":"contentFromField2"
    }
]

And put it on the clipboard. Of course, you always have to check after reading the clipboard is the content actually deserializable.

For creating content like this you can use a Java library like json-simple . A simple example with the content like above:

JSONObject obj1 = new JSONObject();
obj1.put("label", "field1");
obj1.put("content", "contentFromField1);

JSONObject obj2 = new JSONObject();
obj2.put("label", "field2");
obj2.put("content", "contentFromField2);

JSONArray list = new JSONArray();
list.add(obj1);
list.add(obj2);
Aleksandar Stojadinovic
  • 4,851
  • 1
  • 34
  • 56
  • can I ask you a question? I am very new to Java. Out of curiosity, I am trying to using Java Swing and JSON code you have given uses Javascript. Can I still use it? I am not sure.. – Manu Nov 10 '16 at 16:03
  • It's not using Javascript, where did you get that idea? What you are looking at is JSON, JSON is not Javascript. JSON is a data exchange format, you can represent data and structures through it and read and write to it from different languages. You can represent Java objects, Python dictionaries, Javascript objects and so on. In this case we just used it to structure some stuff. – Aleksandar Stojadinovic Nov 10 '16 at 19:23
0

please help me with concatenation?

This is basic java that I'm sure you use all the time:

String textForClipboard = label1.getText() + ":" + label2.getText();

Or you could use a StringBuilder:

StringBuilder sb = new StringBuilder();
sb.append( labe1.getText() );
sb.append( ":" );
sb.append( label2.getText() );

Then when you get the data from the clipboard you need to parse it. You could use the String.split(...) method.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I have added a screenshot above to sort the confusion. Basically, I want to copy the contents of 2 text areas along with the label. So that I can copy it to text file something like coder: "data entered" and Reviewer: "data entered. – Manu Nov 10 '16 at 16:30
  • code you provided only gets the label names and not the contents of text area, if i am not wrong. Correct? – Manu Nov 10 '16 at 16:59
  • @Manu, Try it for yourself. You learn by trying, not asking questions. You also save time by doing it yourself instead of waiting minutes/hours for someone to reply. The code I gave is a basic example showing how to concatenate strings. Try it and change it as required. All you need to do is create the string and use System.out.println(...) to verify if the contents of the string is what you expect. – camickr Nov 10 '16 at 17:21
  • Thank you, let me try it out!! – Manu Nov 10 '16 at 17:27