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 8575855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:47:24+00:00 2026-06-11T19:47:24+00:00

In the last secong and third line if I say label.setText(x = ); The

  • 0

In the last secong and third line if I say

label.setText("x = ");

The label is moving perfectly but when I change it to

label.setText("x = "+ x);

it does not move. To be specific I want to see the width location of JLabel when it’s moving by variable x!
Beside this I said label.setBounds(x,(getHeight()/2),300,300); which set the Y bound of the label to half of the frame size but it’s not at the middle of frame? Any Idea?

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

import javax.swing.JFrame;

public class myTiemr {

    public static void main(String args[])
    {
        TimeFrame frame = new TimeFrame();
    }
}

class TimeFrame extends JFrame
{
    private static final long serialVersionUID = 1L;
    private int x = 0;
    JLabel label = new JLabel("Here is my label");
    public TimeFrame()
    {
        int d = 10;
        setTitle("My Frame");
        setSize(500,500);
        this.setLocationRelativeTo(null);
        add(label);
        Timer time = new Timer(d,new TimerListener());
        time.start();       
        setVisible(true);
    }
    class TimerListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if(x>getWidth()){
                x=-100;
            }
            x+=1;
            label.setText("x = "+ x);
            //label.setText("x = ");
            label.setBounds(x,(getHeight()/2),300,300);
        }
    }

}
  • 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-06-11T19:47:25+00:00Added an answer on June 11, 2026 at 7:47 pm

    The reason that label.setText("x = "+ x) causes the text to remain still but yet the line label.setText("x = ") causes the label to move across the frame is that revalidate() is called on the JLabel. This will cause the correct behavior applying the current layout manager’s rules (namely BorderLayout in this case).

    When the text does not change, revalidate() is never called.

    As @Sébastien Le Callonnec suggested setting no layout on the frame will cause

    label.setText("x = "+ x)
    

    to move as desired.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the example given below, the last line is not uploaded. I get an
Last Thursday we just released a new version of our website. The big change
I'm trying to set up argparse on my python program but it's not working.
I have seen references to similar issues, but not this exact one. I have
I have a textfile.txt like this: First Line Second Line Third Line Fourth Line
I want to get the second last item given to a shell program. Currently
I'm trying to find a way to break a string at the second last
Last week I created an e-shop with opencart. Now I'm trying to customize the
Last night I tried to put together something that I have had working since
Last night before going to bed, I browsed through the Scalar Data section of

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.