0

I have a code that generates tag by pressing button ...like you type some random word then it generates a list of tags, here's the code

JButton tagGen = new JButton("GENERATE");
        tagGen.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String copyTag;
                String title = textField.getText();
                String[] keywords = {"Google", "Yahoo", "Bing", "DuckDuckGo"};

            for(int i=0; i<keywords.length; i++) {
                copyTag = (title.replaceAll("Search Engine|search |is |Is","") 
                    + keywords[i]+", ");
                textTag.setText(copyTag);
            }
        }

So the problem is i'm not getting the full tags like this...

random text Google, random text Yahoo, random text Bing, random text DuckDuckGo,

instead getting the only last tag...

random text DuckDuckGo,

what i'm doing wrong? i'm using setText method but still it's not printing full text

i tried to search the fix of this problem but couldn't find any solution

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    Not sure what you try to accomplish. And not sure why do you tag your question as JavaFX while it seems to be only swing... Put the `textTag.setText(copyTag);` out of the `for` loop, just after. And append the text in the `copyTag` variable instead of reassigning it each time by doing this for example `copyTag ` **+** `= (title.replaceAll("Search Engine|search |is |Is","") + keywords[i]+", ");` – Pagbo Feb 13 '19 at 12:45
  • @Pagbo thanks it fixed the problem but it's taking too much processing time when working with around 500+ tags any idea to reduce the time? it's actually taking around 25-30 second.... – Shubham Chaurasiya Feb 13 '19 at 13:18
  • @kleopatra i already demonstrated what i can, what i'm doing wrong? – Shubham Chaurasiya Feb 13 '19 at 13:19
  • 1
    You should make a performance analysis to see which instruction is too long, but you could begin by appending your tags into a `StringBuilder` instead of a `String`. And above at all, if the data process is expected to be long, **do not execute it in the graphic thread** (no matter if it is the EDT or the JavaFX thread) or you will get freeze. – Pagbo Feb 13 '19 at 13:32
  • 2
    read the referenced help page and provide the example as suggested ... anyway, what does this have to do with fx, the snippet is for JButton aka Swing? – kleopatra Feb 13 '19 at 13:46
  • @Pagbo yeah it's a java swing program **GUI** based i just have long keywords around **500+** and it's taking time in **GUI** but not in CLI, how can i reduce the time? – Shubham Chaurasiya Feb 13 '19 at 14:11
  • @kleopatra okay thanks i just fixed the tag – Shubham Chaurasiya Feb 13 '19 at 14:12

1 Answers1

0

Don't use a JextField for this.

Instead use a JTextArea then in your loop you can use:

//textTag.setText(copyTag);
textArea.append(copyTag);
camickr
  • 321,443
  • 19
  • 166
  • 288