Newbie Newbae
Newbie Newbae

Reputation: 65

show data in TextArea java

I'm trying to show all data that I got in the textarea

    public static void randNumQueue(){
    Queue<String> myQueue = new LinkedList<>();
    if(average12==30){
        long range = (long)randNum2 - (long)randNum1 + 1;
        long fraction = (long)(range * random.nextDouble());
        number = (int)(fraction + randNum1);
        System.out.println(number);
        Set mySet = new HashSet();
        for (int j = 0; j<number; j++)
        {
            int pick;
            do{
                pick = random.nextInt(number)+1;
            }while(!mySet.add(pick));
            myQueue.add(Integer.toString(pick));
        }
        System.out.println("the queue size: "+myQueue.size());
        sizeQ = myQueue.size();
        Iterator it = myQueue.iterator();
        while(it.hasNext()){
                String iteratorValue = (String)it.next();
                Qnum = Integer.parseInt(iteratorValue);
                System.out.println("queue next value: "+Qnum);
            }
    }
}
@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource().equals(b1)){
        randomAverage();
        testAmount = testAmount + 1;
        label5.setText(""+testAmount);
        area3.setText(""+sizeQ);
        area1.setText(""+Qnum);
    }

}

area1 is area text which I'm trying to show all data which I got from IteratorValue

from the above function area1 just showed the last value which I got from iteratorValue

anyone know how to show all the value in area1(textarea)?

Upvotes: 0

Views: 2949

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347332

Use setText when you want to replace the contents of the text area, use JTextArea#append when you want to add text to the end of the text area

Updated

It's a "little" difficult to assess what it is you are trying to do, but something like...

while(it.hasNext()){
    String iteratorValue = (String)it.next();
    area1.append(iteratorValue + "\n");
    Qnum = Integer.parseInt(iteratorValue);
    System.out.println("queue next value: "+Qnum);
}

In your action performed method, remove area1.setText(""+Qnum);. This will append each result to the text area. If you want to clear the text, use something like area1.setText(null)

Upvotes: 2

Related Questions