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

The Archive Base Latest Questions

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

So I am creating a basic application that I want to have a JLabel

  • 0

So I am creating a basic application that I want to have a JLabel at the bottom of the screen that starts at the left bottom corner and moves, animation style, to the right bottom corner in a set time, and a static image in the center. To do this, I created a JFrame with a JPanel using BorderLayout. There is a JLabel with an ImageIcon added to BorderLayout.CENTER and a JPanel at BorderLayout.SOUTH. My code, while hastily written and far from pretty, is:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension; 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.BorderFactory;

public class GameWindow extends JPanel{

private static JLabel mainWindow, arrowLabel, arrowBox;
protected static JFrame frame;
protected static JPanel arrows;

public static int x = 600;

public GameWindow(){
    mainWindow = new JLabel("Center");
    arrowLabel = new JLabel("Moving");
    arrows = new JPanel();
    arrows.setSize(600, 100);
    arrows.setLayout(null);
    arrowBox = new JLabel("");
    arrowBox.setBounds(0, 0, 150, 100);
    arrowBox.setPreferredSize(new Dimension(150, 100));
    arrowBox.setBorder(BorderFactory.createLineBorder(Color.black));
    arrows.add(arrowBox);
    this.setSize(600,600);
    this.setLayout(new BorderLayout());
    this.add(mainWindow, BorderLayout.CENTER);
    this.add(arrows, BorderLayout.SOUTH);
}

public static void main(String[] args)
{
    GameWindow g = new GameWindow();
    frame = new JFrame("Sword Sword Revolution");
    frame.add(g);
    frame.setSize(600,600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    Timer t = new Timer(1000, new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {
            arrows.add(arrowLabel);
            arrowLabel.setBounds(x, 100, 100, 100);
            x-=50;
            arrows.repaint();
            frame.repaint();
        }

    });
    t.start();
}

}

The ImageIcon in the center JLabel appears fine, and the empty JLabel with a border appears at the bottom, but I cannot get the second JLabel with the arrow image to show up on screen. Eventually I will change to scheduleAtFixedRate to continuously move the JLabel, but right now I can’t even get the image to appear on screen.

I also understand that I will most likely not be able to use FlowLayout for this, as I understand it does not allow you to set the location of your components. I tried using null layout, but with null layout the empty JLabel with a border does not appear. I can barely make out the top of the border at the bottom edge of the frame, but even with setLocation I cannot get it to appear where I want it to.

Obviously, my thought process is flawed, so any help would be appreciated.

  • 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:54:03+00:00Added an answer on June 11, 2026 at 7:54 pm

    Your use of threading is all wrong for Swing applications. You should not be trying to add or remove components in a background thread but instead should use a Swing Timer to do this on the Swing event thread.

    Also, what do you mean by:

    I want to have a scrolling JLabel at the bottom of the screen

    Please clarify the effect you’re trying to achieve.

    Also regarding,

    I also understand that I will most likely not be able to use FlowLayout for this, as I understand it does not allow you to set the location of your components. I tried using null layout, but with null layout the empty JLabel with a border does not appear. I can barely make out the top of the border at the bottom edge of the frame, but even with setLocation I cannot get it to appear where I want it to.

    No, don’t use null layout for this situation. There are much better layout managers that can help you build your application in a cleaner more platform-independent manner.

    Edit 3
    Regarding:

    To clarify, at the bottom of the screen I want a JLabel at the far right corner, then in the swing timer, the JLabel will gradually move to the left until it leaves the screen. If I could get setLocation to work, the basic premise would be to have a variable x set to 600, and then every second decrement x by say 50 and then redraw the JLabel at the new location on the screen. Basic animation.

    I would create a JPanel for the bottom of the screen for the purposes of either holding your JLabel or displaying the image without a JLabel by overriding its paintComponent(...) method. If you use it as a container, then yes, its layout should be null, but the rest of the GUI should not be using null layout. The Swing Timer would simply change the JLabel’s location and then call repaint() on its JPanel/container. If you go the latter route, you would draw the image in the JPanel’s paintComponent(...) method using g.drawImage(myImage, x, y), and your timer would change x and/or y and call repaint() on the drawing JPanel.

    Also, you likely do not want to keep adding a JLabel in your timer but rather simply moving the JLabel that’s already displayed in the GUI.

    Also, to avoid focus issues, don’t use a KeyListener to capture keystroke input but rather use Key Bindings. Google will direct you to a great tutorial on this construct.

    Edit 4
    For example:

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.EnumMap;
    
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class AnimateExample extends JPanel {
       public static final String DUKE_IMG_PATH = 
             "https://duke.kenai.com/iconSized/duke.gif";
       private static final int PREF_W = 800;
       private static final int PREF_H = 800;
       private static final int TIMER_DELAY = 20;
       private static final String KEY_DOWN = "key down";
       private static final String KEY_RELEASE = "key release";
       public static final int TRANSLATE_SCALE = 3;
       private static final String BACKGROUND_STRING = "Use Arrow Keys to Move Image";
       private static final Font BG_STRING_FONT = new Font(Font.SANS_SERIF,
             Font.BOLD, 32);
       private EnumMap<Direction, Boolean> dirMap = 
             new EnumMap<AnimateExample.Direction, Boolean>(Direction.class);
       private BufferedImage image = null;
       private int imgX = 0;
       private int imgY = 0;
       private int bgStringX; 
       private int bgStringY; 
    
       public AnimateExample() {
          for (Direction dir : Direction.values()) {
             dirMap.put(dir, Boolean.FALSE);
          }
          try {
             URL imgUrl = new URL(DUKE_IMG_PATH);
             image = ImageIO.read(imgUrl);
          } catch (MalformedURLException e) {
             e.printStackTrace();
          } catch (IOException e) {
             e.printStackTrace();
          }
    
          new Timer(TIMER_DELAY, new TimerListener()).start();
    
          // here we set up our key bindings
          int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
          InputMap inputMap = getInputMap(condition);
          ActionMap actionMap = getActionMap();
          for (final Direction dir : Direction.values()) {
    
             // for the key down key stroke
             KeyStroke keyStroke = KeyStroke.getKeyStroke(dir.getKeyCode(), 0,
                   false);
             inputMap.put(keyStroke, dir.name() + KEY_DOWN);
             actionMap.put(dir.name() + KEY_DOWN, new AbstractAction() {
    
                @Override
                public void actionPerformed(ActionEvent arg0) {
                   dirMap.put(dir, true);
                }
             });
    
             // for the key release key stroke
             keyStroke = KeyStroke.getKeyStroke(dir.getKeyCode(), 0, true);
             inputMap.put(keyStroke, dir.name() + KEY_RELEASE);
             actionMap.put(dir.name() + KEY_RELEASE, new AbstractAction() {
    
                @Override
                public void actionPerformed(ActionEvent arg0) {
                   dirMap.put(dir, false);
                }
             });
          }
    
          FontMetrics fontMetrics = getFontMetrics(BG_STRING_FONT);
          int w = fontMetrics.stringWidth(BACKGROUND_STRING);
          int h = fontMetrics.getHeight();
    
          bgStringX = (PREF_W - w) / 2;
          bgStringY = (PREF_H - h) / 2;
       }
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    
       @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics2D g2 = (Graphics2D) g;
          g.setFont(BG_STRING_FONT);
          g.setColor(Color.LIGHT_GRAY);
          g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
          g.drawString(BACKGROUND_STRING, bgStringX, bgStringY);
    
          if (image != null) {
             g.drawImage(image, imgX, imgY, this);
          }
       }
    
       private class TimerListener implements ActionListener {
          public void actionPerformed(java.awt.event.ActionEvent e) {
             for (Direction dir : Direction.values()) {
                if (dirMap.get(dir)) {
                   imgX += dir.getX() * TRANSLATE_SCALE;
                   imgY += dir.getY() * TRANSLATE_SCALE;
                }
             }
             repaint();
          };
       }
    
       enum Direction {
          Up(KeyEvent.VK_UP, 0, -1), Down(KeyEvent.VK_DOWN, 0, 1), Left(
                KeyEvent.VK_LEFT, -1, 0), Right(KeyEvent.VK_RIGHT, 1, 0);
    
          private int keyCode;
          private int x;
          private int y;
    
          private Direction(int keyCode, int x, int y) {
             this.keyCode = keyCode;
             this.x = x;
             this.y = y;
          }
    
          public int getKeyCode() {
             return keyCode;
          }
    
          public int getX() {
             return x;
          }
    
          public int getY() {
             return y;
          }
    
       }
    
       private static void createAndShowGui() {
          AnimateExample mainPanel = new AnimateExample();
    
          JFrame frame = new JFrame("Animate Example");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    

    Which will create this GUI:

    enter image description here

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

Sidebar

Related Questions

The basic setup is classic - you're creating a Windows Forms application that connects
Using jQuery I'm creating a basic 'tooltip' animation so that the tooltip will appear
I am creating window application basic. As i have created datagrid with 2 columns
I have developed a CakePHP application that does basic CRUD on a series of
My current class is planning on creating a basic networked game, but we have
I'm creating an application that rehost the workflow designer and that can load an
I'm a desktop application developer. I'm creating a basic website from which to sell
I have a c# WinForm application that I need to generate TabPages at runtime.
I have a new Winforms application that is replacing an existing Access order processing
I'm developing an application that uses Qt 4.7 with the goal of creating a

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.