Hi

I am working on an incredible project, it is an GUI. I hav used a JLabel to show 10 words but it actually always shows the last one only. I think that i need a timer to make it change but i have already a timer and i cannot put another one.
my code is this:
int i = 0;
while (i < 10) {
word = game.randomize(game.getBigDic());
arrayOfTen.add(word);
System.out.println(arrayOfTen.get(i));
label3.setText(arrayOfTen.get(i));
i++;
}

The arraylist has the 10 words but the label can't seem to show the 10 words. How can i make each word show each after 1 second?

The following piece of code:-

label3.setText(arrayOfTen.get(i));

Replaces whatever previous value you have set for the label with the new one you have specified here.

Use this in case you want to also retain the previous content of the label:-

label3.setText(label3.getText() + " " + arrayOfTen.get(i));

And please use code tags when you are posting code. All you have to do is put your code inside [ code=java ] and [ /code ], its is already mentioned in the announcement at the top of the Java forum.

The line of code worked perfectly but i wonder if i could not have all the words of the array appearing at the same time. Setting the text to null doesn't work.

but i wonder if i could not have all the words of the array appearing at the same time

What is it that you want exactly, your question is just too generic, I do not know what to make of it.

What is it that you want exactly, your question is just too generic, I do not know what to make of it.

he wants to have all the words in the array to be set as the text, but instead of adding all the words, he just sets the last one, and now can't figure out why only the last one is showing :)

label3.setText(arrayOfTen.get(i));

he resets the value every time he goes through the loop.

all he has to do is:

String labelText = "";

int i = 0;
while (i < 10) {
word = game.randomize(game.getBigDic());
arrayOfTen.add(word);
System.out.println(arrayOfTen.get(i));
labelText = labelText + " " + arrayOfTen.get(i);
//label3.setText(arrayOfTen.get(i));
// add to the text, not reset
i++;
}
label3.setText(labelText);

Sorry Stultuske but my first post already does what you are recommending in your post.
But the following line in his second post contradicts what he says in his first post:-

i wonder if i could not have all the words of the array appearing at the same time

So here he does not want the entire content of the array to be displayed at the same time.

then I guess he wants them to appear one after another ...
can be done by a Timer, or a second for-loop, or by using sleep.

String text = "";
for ( int j = 0; j < 10; j++){
   text = label3.getText();
   if (text.equals(""))
    label3.setText(arrayOfTen(j));
   else
    label3.setText(text + " " + arrayOfTen(j));
  sleep(7500);
}

or something similar. shouldn't be the biggest problem, I guess

He better explain what he is looking for as that incredible project can have de speakable/ugly/terrible GUI. Why to pass 10 elements of array to same label?

Hi
thanks for your help, i only want to say that i am a she and not a he and that yes, i am programming a text game in java and i want to display in a label 10 words but not at the same time. i wanted to use a timer but i had already a timer in the program and when i try to put another one, it gives me an error.

My apologies for gender confusion, but it was not clear from the name if the person is male or female.

Can you please kindly explain more in details what you trying to achieve? You know "text game" and "i want to display in a label 10 words but not at the same time" are very general terms. So if you can say what functionality you expect out of your game, what is the reason behind flashing 10 words in label or what you trying to achieve, we may be able to give you better advice then just general direction

My name is Jennifer and i love computers. I am into programming but i struggle a bit with it. I am 22 years old, i have a nice boyfriend, i am Haitian living for the moment in the States to complete my degree. i love reading, sleeping, dancing and eating. i hope that i will meet new people. kisses

Sheesh, I guess we should dwell in the Community Introductions section more ..... :P

The game is a little bit simple. When it begins, the user will see 10 words appear, one after another, then in a limited time, he has to rewrite the words he has seen earlier. I wanted to use the label to show quickly the 10 words and then the game will begin, but it seems that the label is only getting the last word of the arraylist. i already have a timer for the countdown timer and i cannot use another one for the label. the for loop doesn't work.

In theory you can create as many timer as you wish, it is the way how you handle them what is important.
Would you mind share the code? You can upload whole project as zip file if you do not mind sharing...

as far as I've read your code (no, I did not download the zip file)
you do show all the words in the JLabel, but you reset the text immediately, so it is not visible to the naked eye.

well... I would like to apologise as well for thinking you to be a "he" but on the other hand... unless you believe your gender has anything to do with your programming skills, I don't really need to know, do I?

if you still get only the last word of the array in your JLabel, can you paste your current code where you set the text for the JLabel here?

If I'm not mistaken, she is just looking for a way to "pause" after each word is displayed, but she doesn't know it. She thinks what Stul said above - that it's just not working - but it probably is working and just is disappearing too fast to be seen.

Now, someone else may have a better suggestion, but I seem to remember something like this to pause. If she adds it in her loop she might get what she wants.

Thread.getThread().sleep(3000); //Pause for 3 s

> Thread.getThread().sleep(3000); //Pause for 3 s Thread.currentThread().sleep(3000);

That would give you a warning since sleep is a static method and from what I have seen applies to the current thread only, So its got to be:- Thread.sleep(3000);

I have tried Thread.sleep(7000); but it required try catch, the game was very slow with it and it did not anything. it always showed the last word of the arraylist. I was wondering if there was not another way to do without th label. it is killing me.

I have tried Thread.sleep(7000); but it required try catch, the game was very slow with it and it did not anything. it always showed the last word of the arraylist. I was wondering if there was not another way to do without th label. it is killing me.

it only shows the last word because you overwrite it every time, which has been pointed out several times before. don't overwrite it, add it to the String that's already in the JLabel.
just look up what the sleep() method of a thread does, it might give you an idea.

You're better off using the Timer anyway to avoid repaint issues and stalling the AWT event thread.
Here's a simple example of the label with a timer:

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;

public class TimerExample extends JFrame {

    final String[] words = {"The", "cat", "in", "the", "hat"};
    final JLabel label;
    Timer wordTimer;
    int wordIndex = 0;

    public TimerExample() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(200, 200);
        label = new JLabel("", JLabel.CENTER);
        getContentPane().add(label);

        wordTimer = new Timer(1000, new WordTimerListener());
        setVisible(true);
        wordTimer.start();
    }

    class WordTimerListener implements ActionListener {
        int wordIndex = 0;

        public void actionPerformed(ActionEvent e) {
            if (wordIndex < words.length) {
                label.setText(words[wordIndex++]);
            } else {
                wordTimer.stop();
            }
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TimerExample();
            }
        });
    }
}

i finally had the chance to work on my project and thank you so much, it worked perfectly.

This question has already been answered. Start a new discussion instead.