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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:18:18+00:00 2026-06-07T04:18:18+00:00

I tried create simple class which can slide a JPanel like this: +———-+ +——+—+

  • 0

I tried create simple class which can slide a JPanel like this:

+----------+    +------+---+    +----------+
|          |    |      |   |    |          |
| JPanel1  | => | JPane| JP| => | JPanel2  |
|          |    |      |   |    |          |
+----------+    +------+---+    +----------+

I created javax.swing.Timer and added in class

timer = new Timer(50, this);
timer.start();

static final int frames = 5;
int counter = 0;

actionPerformed(ActionEvent e) {
    if (counter >= frames) {
        timer.stop();
        counter = 0;
    } else {
        counter++;
        jPanel2.setBounds(800 - 800 * counter / frames, 0, 800, 600);
    }
}

This is work, but very slowly. I have only 2-3 fps and don’t know how to speed up this method. May you help me?

  • 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-07T04:18:20+00:00Added an answer on June 7, 2026 at 4:18 am

    Perhaps change the Timer’s interval from 50 to some smaller number.

    i.e., from this:

    timer = new Timer(50, this);
    

    to this:

    timer = new Timer(10, this);
    

    Note: you should avoid use of magic numbers here.

    Heck, something like…

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    public class SlideEg extends JPanel {
       private SlideContainer slideContainer = new SlideContainer();
    
       public SlideEg() {
          setLayout(new BorderLayout());
          add(slideContainer, BorderLayout.CENTER);
    
          JLabel helloLabel = new JLabel("Hello", SwingConstants.CENTER);
          slideContainer.add(helloLabel);
    
          Timer myTimer = new Timer(5000, new ActionListener() {
    
             @Override
             public void actionPerformed(ActionEvent e) {
                JLabel goodbyeLabel = new JLabel("Goodbye", SwingConstants.CENTER);
                goodbyeLabel.setOpaque(true);
                goodbyeLabel.setBackground(Color.pink);
                slideContainer.add(goodbyeLabel);
             }
          });
          myTimer.setRepeats(false);
          myTimer.start();
       }
    
       private static void createAndShowGui() {
          SlideEg mainPanel = new SlideEg();
    
          JFrame frame = new JFrame("SlideEg");
          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();
             }
          });
       }
    }
    
    class SlideContainer extends JLayeredPane {
       private static final int PREF_W = 400;
       private static final int PREF_H = PREF_W;
       private static final int SLIDE_DELAY = 20;
       protected static final int DELTA_X = 2;
       Component oldComponent;
    
       public SlideContainer() {
          setLayout(null);
       }
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    
       @Override
       public Component add(Component comp) {
          Component[] comps = getComponents();
          if (comps.length > 0) {
             oldComponent = comps[0];
          }
          if (oldComponent == comp) {
             return super.add(comp);
          }
          if (oldComponent != null) {
             putLayer((JComponent) oldComponent, JLayeredPane.DEFAULT_LAYER);
          }
          Component returnResult = super.add(comp);
          putLayer((JComponent) comp, JLayeredPane.DRAG_LAYER);
          comp.setSize(getPreferredSize());
          comp.setVisible(true);
          comp.setLocation(getPreferredSize().width, 0);
          slideFromRight(comp, oldComponent);
          return returnResult;
       }
    
       private void slideFromRight(final Component comp,
             final Component oldComponent2) {
          new Timer(SLIDE_DELAY, new ActionListener() {
    
             @Override
             public void actionPerformed(ActionEvent aEvt) {
                int x = comp.getX();
                if (x <= 0) {
                   comp.setLocation(0, 0);
                   putLayer((JComponent) comp, JLayeredPane.DEFAULT_LAYER);
                   if (oldComponent2 != null) {
                      remove(oldComponent2);
                   }
                   ((Timer) aEvt.getSource()).stop();
                } else {
                   x -= DELTA_X;
                   comp.setLocation(x, 0);
                }
                repaint();
             }
          }).start();
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create simple dummy tag which can work with Struts2. I have
This is a simple class ive created in C++ for a musical application im
I have tried to create a simple controller/model (based on the samples code of
A simple/newbie question. I create a branch from the master branch (say testing), tried
I created a simple test with SendKeys, and tried both SendWait and Send. Private
I have created a small and simple android app. I tried installing it on
I created a sample webservice which contain default 'HelloWorld' function as 'webmethod' when tried
I have tried create this table, but nothing I have tried works from FKs.
I'd like to use SQLiteCursor class to fetch data (not using Cursor class). This
How can I add touch capabilities to a simple bitmap ? I've tried to

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.