本文介绍了JTable中的JProgressBar没有更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么以下代码中的进度条没有更新?如何将它们更新?
Why don't the progress bars in the following code update and how would I get them to?
import java.awt.event.{ActionListener, ActionEvent}
import javax.swing.table.{TableCellRenderer, AbstractTableModel}
import javax.swing.{Timer, JTable, JProgressBar, JFrame}
object TestProgressBar {
val frame = new JFrame()
val model = new TestModel()
val table = new JTable(model)
class TestModel extends AbstractTableModel {
def getColumnCount = 4
def getRowCount = 4
def getValueAt(y: Int, x: Int) = "%d,%d".format(x, y)
override def setValueAt(value: Any, row: Int, col: Int) {
if (col == 0) {
model.fireTableCellUpdated(row, col);
}
}
}
class TestCellRenderer extends TableCellRenderer with ActionListener {
val progressBar = new JProgressBar()
val timer = new Timer(100, this)
progressBar.setIndeterminate(true)
timer.start()
override def getTableCellRendererComponent(tbl: JTable, value: AnyRef,
isSelected: Boolean,
hasFocus: Boolean,
row: Int, column: Int) = {
progressBar
}
override def actionPerformed(e: ActionEvent) {
val model = table.getModel
for (row <- 0 to model.getRowCount) {
model.setValueAt(0, row, 0)
}
}
}
def main(args: Array[String]) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
table.getColumnModel.getColumn(0).setCellRenderer(new TestCellRenderer())
frame.add(table)
frame.pack()
frame.setVisible(true)
}
}
推荐答案
正如@Robin所说,渲染器仅在模型更新时引发。您需要定期更改所需行中的值。此示例使用 javax.swing.Timer
。
As @Robin notes, the renderer in only evoked when the model is updated. You need to periodically change the value in the desired row(s). This example uses javax.swing.Timer
.
private class TestCellRenderer implements TableCellRenderer, ActionListener {
JProgressBar bar = new JProgressBar();
Timer timer = new Timer(100, this);
public TestCellRenderer() {
bar.setIndeterminate(true);
timer.start();
}
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row, int column) {
return bar;
}
@Override
public void actionPerformed(ActionEvent e) {
TableModel model = table.getModel();
for (int row = 0; row < model.getRowCount(); row++) {
table.getModel().setValueAt(0, row, 0);
}
}
}
您可能还想实施 setValueAt()
,因为 AbstractTableModel
实现为空:
You may also want to implement setValueAt()
, as the AbstractTableModel
implementation is empty:
@Override
public void setValueAt(Object aValue, int row, int col) {
if (col == 1) {
// update your internal model and notify listeners
this.fireTableCellUpdated(row, col);
}
}
附录:作为参考,这里有一个相应的Java实现补充Scala。
Addendum: For reference, here's a corresponding Java implementation to complement the Scala.
代码:
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.Timer;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
/**
* @see http://stackoverflow.com/a/10491290/230513
*/
public class TestProgressBar extends JPanel {
private JTable table = new JTable(new TestModel());
public TestProgressBar() {
table.getColumnModel().getColumn(0).setCellRenderer(new TestCellRenderer());
table.setPreferredScrollableViewportSize(new Dimension(320, 120));
this.add(new JScrollPane(table));
}
private class TestModel extends AbstractTableModel {
@Override
public int getRowCount() {
return 4;
}
@Override
public int getColumnCount() {
return 4;
}
@Override
public Object getValueAt(int row, int col) {
return String.valueOf(row) + ", " + String.valueOf(col);
}
@Override
public void setValueAt(Object aValue, int row, int col) {
// update internal model and notify listeners
fireTableCellUpdated(row, col);
}
}
private class TestCellRenderer implements TableCellRenderer, ActionListener {
JProgressBar bar = new JProgressBar();
Timer timer = new Timer(100, this);
public TestCellRenderer() {
bar.setIndeterminate(true);
timer.start();
}
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row, int column) {
return bar;
}
@Override
public void actionPerformed(ActionEvent e) {
TableModel model = table.getModel();
for (int row = 0; row < model.getRowCount(); row++) {
table.getModel().setValueAt(0, row, 0);
}
}
}
private void display() {
JFrame f = new JFrame("TestProgressBar");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new TestProgressBar().display();
}
});
}
}
这篇关于JTable中的JProgressBar没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!