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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:03:37+00:00 2026-05-16T05:03:37+00:00

i am working on an animation that when i pressed a button the each

  • 0

i am working on an animation that when i pressed a button the each row of the corresponding table is highlighted one by one from top to bottom. However, when the animation started, the table cannot repaint correctly, the ui of the table is corrupted somehow. here is my code below.

/*

* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* TableFrame.java
*
* Created on 2010/8/5, 下午 04:07:10
*/

package test;
import javax.swing.;
import javax.swing.table.
;
/**
*
* @author Administrator
*/
public class TableFrame extends javax.swing.JFrame {
StringThread1 t = null;

public JTable table;
public JScrollPane getScrollPane(){
return this.jScrollPane1;
}
/** Creates new form TableFrame */
public TableFrame() {
initComponents();
table = new JTable(new javax.swing.table.DefaultTableModel(100, 2));
this.jScrollPane1.setViewportView(table);
for(int i = 0; i < 100; i++){
for(int j = 0; j < 2; j++){
table.setValueAt(new Integer(i), i, j);
}
}
t = new StringThread1(this);
}

/** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jScrollPane1 = new javax.swing.JScrollPane();
    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jScrollPane1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

    jButton1.setText("jButton1");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
                .addGroup(layout.createSequentialGroup()
                    .addGap(32, 32, 32)
                    .addComponent(jButton1)))
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jButton1)
            .addGap(18, 18, 18)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(48, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>                        

boolean play = false;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    this.t.start();
        play = !play;
}                                        

class StringThread1 extends Thread
{
    private TableFrame str;

    StringThread1 (TableFrame s)
    {
        str=s;
    }

    public void run() {
        int y = 0;
        while(true){
            try{
                System.out.println("y " + String.valueOf(y));
                this.sleep(500);
                this.str.table.setRowSelectionInterval(y, y);
                int rp = y * this.str.table.getRowHeight();
                int halfHeight = this.str.getScrollPane().getHeight() / 2 - this.str.table.getRowHeight();
                if(rp + this.str.table.getRowHeight() * 2 >= this.str.getScrollPane().getVerticalScrollBar().getValue() + this.str.getScrollPane().getHeight()){
                    this.str.getScrollPane().getVerticalScrollBar().setValue((y) * this.str.table.getRowHeight());
                }

                else{
                    this.str.getScrollPane().getVerticalScrollBar().setValue(0);
                }


                if(y == 99){
                    y = 0;
                }y+= 1;
            } catch(InterruptedException e){}
        }
    }
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new TableFrame().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration                   

}

  • 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-16T05:03:38+00:00Added an answer on May 16, 2026 at 5:03 am

    There’s a golden rule – don’t manipulate displayed GUI items in threads outside the Event Displatch Thread. So, you need to use SwingUtilities.invokeLater() to do these calls:

    this.str.table.setRowSelectionInterval(y, y);
    

    and

    this.str.getScrollPane().getVerticalScrollBar().setValue((y) * this.str.table.getRowHeight());
    

    and

    this.str.getScrollPane().getVerticalScrollBar().setValue(0);
    

    You can wrap each one, or wrap them all in a single call.

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

Sidebar

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.