Java - 如何使“可运行” J表?

发布于 2024-08-22 03:35:31 字数 853 浏览 4 评论 0原文

我在创建 JTable 时遇到问题,它每秒都会显示一些文本。 我制作 MainView,放置 JTable 并拥有类“TableHandler(JTable table) Implements Runnable”,该类应该按间隔向 JTable 添加一些文本... 这是 run 方法:

public void run() {
    for (int i=0; i<5; i++) {
        table.setValueAt("text", i, i);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
}

问题是,MainView 在 Jtable 填充数据之前不会“显示”,因此用户看不到填充:-(

编辑:更多代码

public MyView(SingleFrameApplication app) {
    super(app);

    initComponents();
    // log of GUI stuff here
   TableHandler th = new TableHandler(myTable);
   th.createTable(); // just loads data
   Timer t = new Timer(100,new ActionListener() {
   public void actionPerformed(ActionEvent e) {
       th.run();
      }
   });
   t.start();

I have problem creating JTable, which will show every second some text.
I make MainView, place JTable and have Class "TableHandler(JTable table) implements Runnable" which is supposed to add some text in intervals to JTable ...
Here is run method :

public void run() {
    for (int i=0; i<5; i++) {
        table.setValueAt("text", i, i);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
}

The problem is, that MainView wont "show" until Jtable is filled with data, so user doesn't see filling :-(

EDIT : more code

public MyView(SingleFrameApplication app) {
    super(app);

    initComponents();
    // log of GUI stuff here
   TableHandler th = new TableHandler(myTable);
   th.createTable(); // just loads data
   Timer t = new Timer(100,new ActionListener() {
   public void actionPerformed(ActionEvent e) {
       th.run();
      }
   });
   t.start();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

过潦 2024-08-29 03:35:31

如果您正在使用 Swing 并希望在一定时间间隔内执行操作,则应使用 Swing 计时器,如 教程

将循环转换为对 actionPerformed 的调用序列,您将得到:

    new Timer ( 1000, new ActionListener () {
        int i = 0; // moved out of loop 

        @Override
        public void actionPerformed ( ActionEvent event ) {
            if ( i < 5 ) {
                i++;

                table.setValueAt ( "text", i, i );

            } else {
                // stop the timer firing events when the loop end 
                // condition is reached
                ( ( Timer ) event.getSource() ).setRepeats ( false );
            }
        }
    } ).start();

If you are using Swing and want to perform an action on a time interval, you should use a swing timer, as described in the tutorial.

Translating your loop into a sequence of calls to actionPerformed, you get:

    new Timer ( 1000, new ActionListener () {
        int i = 0; // moved out of loop 

        @Override
        public void actionPerformed ( ActionEvent event ) {
            if ( i < 5 ) {
                i++;

                table.setValueAt ( "text", i, i );

            } else {
                // stop the timer firing events when the loop end 
                // condition is reached
                ( ( Timer ) event.getSource() ).setRepeats ( false );
            }
        }
    } ).start();
半枫 2024-08-29 03:35:31

您需要从 AWT 线程进行更新:

public void run() {
    for (int i=0; i<5; i++) {
        final int x = i;
        SwingUtilities.invokeLater( new Runnable() {
          public void run() {
            table.setValueAt("text", x, x);
           }});
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
}

请参阅 http://java.sun.com/javase/6/docs/api/javax/swing/SwingUtilities.html#invokeLater(java.lang.Runnable)

You need to update from the AWT thread:

public void run() {
    for (int i=0; i<5; i++) {
        final int x = i;
        SwingUtilities.invokeLater( new Runnable() {
          public void run() {
            table.setValueAt("text", x, x);
           }});
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
}

See http://java.sun.com/javase/6/docs/api/javax/swing/SwingUtilities.html#invokeLater(java.lang.Runnable)

自我难过 2024-08-29 03:35:31

您所描述的行为最可能出现的问题是您没有使用单独的线程来启动 run 方法。如果直接调用 run 方法,则屏幕将不会更新,直到主线程再次可用,这要等到 run 方法完成后才会发生。

验证您没有在代码中的任何地方调用 run() ,因为这应该在线程中处理。

编辑:启动该类的代码应如下所示:

new Thread(new TableHandler(table)).start();

The mostly likely problem to the behavior you are describing is that you aren't using a separate thread to start the run method. If the run method is called directly then the screen will not update until the main thread is available again, which will not occur until after the run method completes.

Verify that you are not calling run() anywhere in your code as that should be handled in the thread.

EDIT: The code to start the class should look something like this:

new Thread(new TableHandler(table)).start();
春庭雪 2024-08-29 03:35:31

尝试调用 repaint 。最好包裹在 SwingUtilities.invokeLater

Try putting a call to repaint after your setValueAt() call. Preferably wrapped in SwingUtilities.invokeLater.

ぇ气 2024-08-29 03:35:31

如果在线程中运行 SwingUtilities.invokeLater,则必须使用它。

这只会将请求排队到您的绘画例程中,因此您需要确保通过暂停来调用它们。它应该看起来像这样:

in your main--or somewhere equivilent:
    new myThread
    myThread.start()

myThread.run()
   new myRoutine();
   while(activelyUpdating)
       invokeLater(myRoutine);
       sleep(something reasonable, like 1000 for updates every second);


myRoutine
    do all your drawing crap
    return (do not loop here, that's done in the thread.  Just return)

可能有一种方法可以让您的睡眠等待,直到 invokeLater 队列为空,但请确保您也睡眠,因为否则 AWT 线程(线程“myRoutine”将运行)将永远不会得到一个有机会运行,invokeLater 线程将排队。

You must use SwingUtilities.invokeLater if you are running it in a thread.

This will just queue up requests to your painting routine, so then you need to be sure they are called by putting a pause. It should look something like this:

in your main--or somewhere equivilent:
    new myThread
    myThread.start()

myThread.run()
   new myRoutine();
   while(activelyUpdating)
       invokeLater(myRoutine);
       sleep(something reasonable, like 1000 for updates every second);


myRoutine
    do all your drawing crap
    return (do not loop here, that's done in the thread.  Just return)

There is probably a way to have your sleep wait until the invokeLater queue is empty, but be sure you sleep as well because otherwise the AWT thread (the thread "myRoutine" will run on) will never get a chance to run and invokeLater threads will just queue up.

究竟谁懂我的在乎 2024-08-29 03:35:31

按照你的评论。

正如mmyers所说,调用start而不是run,使用前者是正确的。

所以应该是:

Thread t = new Thread( this.table ); // or new Thread( this )

t.start(); // call start on the read 

而不是

this.table.run();

t.run();

As per you comments.

Call start not run as mmyers points, using the former is the correct.

So it should be:

Thread t = new Thread( this.table ); // or new Thread( this )

t.start(); // call start on the read 

and not

this.table.run();

or

t.run();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文