Java - 如何使“可运行” J表?
我在创建 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您正在使用 Swing 并希望在一定时间间隔内执行操作,则应使用 Swing 计时器,如 教程。
将循环转换为对 actionPerformed 的调用序列,您将得到:
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:
您需要从 AWT 线程进行更新:
请参阅 http://java.sun.com/javase/6/docs/api/javax/swing/SwingUtilities.html#invokeLater(java.lang.Runnable)
You need to update from the AWT thread:
See http://java.sun.com/javase/6/docs/api/javax/swing/SwingUtilities.html#invokeLater(java.lang.Runnable)
您所描述的行为最可能出现的问题是您没有使用单独的线程来启动 run 方法。如果直接调用 run 方法,则屏幕将不会更新,直到主线程再次可用,这要等到 run 方法完成后才会发生。
验证您没有在代码中的任何地方调用 run() ,因为这应该在线程中处理。
编辑:启动该类的代码应如下所示:
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:
尝试调用 repaint 。最好包裹在 SwingUtilities.invokeLater。
Try putting a call to repaint after your setValueAt() call. Preferably wrapped in SwingUtilities.invokeLater.
如果在线程中运行 SwingUtilities.invokeLater,则必须使用它。
这只会将请求排队到您的绘画例程中,因此您需要确保通过暂停来调用它们。它应该看起来像这样:
可能有一种方法可以让您的睡眠等待,直到 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:
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.
按照你的评论。
正如mmyers所说,调用
start
而不是run
,使用前者是正确的。所以应该是:
而不是
或
As per you comments.
Call
start
notrun
as mmyers points, using the former is the correct.So it should be:
and not
or