Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3696528
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:44:59+00:00 2026-05-19T04:44:59+00:00

I would like to have a JLabel changint color to a random one, while

  • 0

I would like to have a JLabel changint color to a random one, while jumping to a random position, and while changing its text.

but the setText and setBounds seem to clash and i don’t know why. if you comment out the setText then the setBounds will work, but they won’t work together.

import java.awt.*;
import java.util.*;
import javax.swing.*;

public class test2 extends JFrame { 

private static JLabel label = new JLabel("0");
private static Random gen = new Random();

public test2() {
    JPanel panel = new JPanel();
    panel.add(label);
    this.add(panel);
}

public static void move() {
    for (int i = 0; i < 10; i++) {
        int n = gen.nextInt(254)+1;
        int nn = gen.nextInt(254)+1;
        int nnn = gen.nextInt(254)+1;
        label.setText(""+i);
        //the setBounds command will not work with the setText command. why?
        label.setBounds(n*2, nn*2, 20, 20);
        label.setForeground(new Color(n, nn, nnn));
        try {
            Thread.sleep(200);
        } catch (Exception e) {}
    }
}

public static void main(String[] args) {
    test2 frame = new test2();
    frame.setVisible(true);
    frame.setSize(600, 600);
    frame.setResizable(true);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
    move();
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T04:45:00+00:00Added an answer on May 19, 2026 at 4:45 am

    setBounds shouldn’t work with a non-null layout, and your problem is happening because changing the text in the JLabel stimulates a re-layout of the JPanel, and since JPanels by default use FlowLayout, the JLabel stays at the top in the middle. So if you really need this functionality, then consider setting the layout of the JPanel to null, or else use a JLayeredPane.

      public test2() {
         JPanel panel = new JPanel(null);
         panel.add(label);
         this.add(panel);
      }
    

    Also you shouldn’t be calling Thread.sleep(…) on the EDT, the Swing thread. Instead consider using a Swing Timer.

    Since I disagreed with the other example, I will post an example of what I was recommending in its place, one that uses a Swing Timer:

    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class Test3 extends JPanel {
        private static final int SIDE = 600;
        public static final int MAX_COUNTER = 20;
        private static final int TIMER_DELAY = 400;
        private static final int MAX_RAND = 220;
        private int counter = 0;
        private JLabel label = new JLabel(String.valueOf(counter));
        private Random gen = new Random();
        private int[] randNumb = new int[3];
    
        public Test3() {
            setPreferredSize(new Dimension(SIDE, SIDE));
            setLayout(null);
            add(label);
    
            new Timer(TIMER_DELAY, new TimerListener()).start();
        }
    
        private void myMove() {
            for (int i = 0; i < randNumb.length; i++) {
                randNumb[i] = gen.nextInt(MAX_RAND);
            }
            label.setText(String.valueOf(counter));
            label.setSize(label.getPreferredSize());
            label.setLocation((SIDE * randNumb[0])/MAX_RAND, (SIDE*randNumb[1])/MAX_RAND);
            label.setForeground(new Color(randNumb[0], randNumb[1], randNumb[2]));
            repaint();
        }
    
        private class TimerListener implements ActionListener {
    
            public void actionPerformed(ActionEvent e) {
                if (counter < MAX_COUNTER) {
                    myMove();
                    counter++;
                } else {
                    ((Timer)e.getSource()).stop();
                }
            }
        }
    
        private static void createAndShowUI() {
            JFrame frame = new JFrame("Test3");
            frame.getContentPane().add(new Test3());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    createAndShowUI();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a panel with several JLabel, I would like to change all their
I have a Jlabel packed with html. I would like to scroll this content
I have a problem that I would like have solved via a SQL query.
I would like to have a reference for the pros and cons of using
I would like to have an iframe take as much vertical space as it
I would like to have a VM to look at how applications appear and
We would like to have user defined formulas in our c++ program. e.g. The
I would like to have a nice template for doing this in development. How
I would like to have all developers on my team to use the same
I would like to have information about the icons which are displayed alongside the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.