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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:39:04+00:00 2026-05-27T07:39:04+00:00

I made a swing application in which i have a grid layout on a

  • 0

I made a swing application in which i have a grid layout on a Panel, now i have 8 custom buttons on that panel, and two button on left and right for the navigation.

Now my problem is that when i clicked on that navigation buttons i want to move the button in the sliding fashion, for that purpose i set each button location,

But the Jpanel not refresh itself, so it is not visible, If i add the dialog box and click ok then it look as buttons are moving.

How can i do this, i used the revalidate(), and repaint() but it not works.

function which execute on next button click

java.net.URL imageURL = cldr.getResource("images/" +2 + ".png");
                    ImageIcon aceOfDiamonds = new ImageIcon(imageURL);
                    button = new MyButton("ABC", aceOfDiamonds, color[3]);
                    Component buttons[] = jPanel1.getComponents();
                    ArrayList<MyButton> buttons1=new ArrayList<MyButton>();
                    for(int i=0;i<buttons.length;i++)
                    {
                        buttons1.add((MyButton) buttons[i]);
                    }
                    Point p=buttons[0].getLocation();
                    Point p1=buttons[1].getLocation();
                    int dis=p1.x-p.x;
                    System.out.println("Distance-->"+dis);
                    button.setLocation(buttons[buttons.length-1].getLocation().x+dis,buttons[0].getLocation().y);
                    jPanel1.add(button);
                    buttons1.add(button);
                    for(int i=0;i<dis;i++)
                    {
                        for(int btn=0;btn<buttons1.size();btn++)
                        {
                            int currX=buttons1.get(btn).getLocation().x;
                            currX--;
                            buttons1.get(btn).setLocation(currX, buttons1.get(btn).getLocation().y);
                        }
                        try
                        {
                            Thread.sleep(500);
                        }
                        catch (InterruptedException ex)
                        {
                            Logger.getLogger(TestPanel.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        //JOptionPane.showMessageDialog(null,"fds");
                        jPanel1.validate();jPanel1.repaint();
                       }

![enter image description here][1]

  • 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-27T07:39:04+00:00Added an answer on May 27, 2026 at 7:39 am

    Don’t set locations. Remove all buttons from the container and re-add them in the new order, then call revalidate() and repaint().

    Edit
    If you want to animate sliding the buttons over, then consider placing them in a JScrollPane, one without scrollbars, and then programmatically scroll the buttons. And I agree that you shouldn’t use Thread.sleep(...) on the EDT but rather use a Swing Timer.

    Edit 2
    For example:

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    
    public class SlideButtons extends JPanel {
       private static final int PREF_W = 600;
       private static final int PREF_H = 200;
       private static final int MAX_BUTTONS = 100;
       private static final int SCROLL_TIMER_DELAY = 10;
       public static final int SCROLL_DELTA = 3;
       private JPanel btnPanel = new JPanel(new GridLayout(1, 0, 10, 0));
       private JScrollPane scrollPane = new JScrollPane(btnPanel);
       private JButton scrollLeftBtn = new JButton("<");
       private JButton scrollRightBtn = new JButton(">");
       private BoundedRangeModel horizontalModel = scrollPane.getHorizontalScrollBar().getModel();
       private Timer scrollTimer = new Timer(SCROLL_TIMER_DELAY, new ScrollTimerListener());
       public String btnText = "";
    
       public SlideButtons() {
          scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
          scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
          for (int i = 0; i < MAX_BUTTONS; i++) {
             String text = String.format("Button %03d", (i + 1));
             JButton btn = new JButton(text);
             btnPanel.add(btn);
          }
          ScrollingBtnListener scrollingBtnListener = new ScrollingBtnListener();
          scrollLeftBtn.addChangeListener(scrollingBtnListener);
          scrollRightBtn.addChangeListener(scrollingBtnListener);
    
          JPanel northPanel = new JPanel(new BorderLayout());
          northPanel.add(scrollLeftBtn, BorderLayout.LINE_START);
          northPanel.add(scrollPane, BorderLayout.CENTER);
          northPanel.add(scrollRightBtn, BorderLayout.LINE_END);
    
          setLayout(new BorderLayout());
          add(northPanel, BorderLayout.PAGE_START);
       }
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    
       private class ScrollingBtnListener implements ChangeListener {
          @Override
          public void stateChanged(ChangeEvent e) {
             JButton btn = (JButton)e.getSource();
             ButtonModel model = btn.getModel();
             //actionCommand  = model.getActionCommand();
             btnText = btn.getText();
             if (model.isPressed() && model.isEnabled()) {
                scrollTimer.start();
             } else {
                scrollTimer.stop();
             }
          }
       }
    
       private class ScrollTimerListener implements ActionListener {
          @Override
          public void actionPerformed(ActionEvent e) {
             if (btnText == null) {
                return;
             }
             int max = horizontalModel.getMaximum();
             int min = horizontalModel.getMinimum();
             int value = horizontalModel.getValue();
    
             if (btnText.equals(">")) {
                if (value <= max) {
                   value += SCROLL_DELTA;
                } else {
                   value = max;
                }
             } else if (btnText.equals("<")) {
                if (value >= min) {
                   value -= SCROLL_DELTA;
                } else {
                   value = min;
                }
             }
             horizontalModel.setValue(value);
          }
       }
    
       private static void createAndShowGui() {
          SlideButtons mainPanel = new SlideButtons();
    
          JFrame frame = new JFrame("SlideButtons");
          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();
             }
          });
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a swings application but there is one problem in that which
I have made a Java Swing application. Now I would like to make it
I have made a swing application which uses image files located in a folder
Hi I made a java swing application. Also created jar of that file. Now
I have made a small java swing application that I want to share with
I have made a java swing GUI. Now I want to display a static
I had made one application in java-swing, Now what i am getting problem is,
I made a simple Swing application. But the rendering behaves buggy. Have I done
Recently I made an application in using Swing, AWT and JDBC that manages some
I have Swing Java application manifesting an error on linux, which I need 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.