JTable如何根据时间自动更新?因此,每当当前时间发生变化时,表格都会自动更新。有些人可以提供一些代码片段,说明这在Java中是如何工作的吗?在时间更改后更新表。请提前谢谢你。
package times;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class TableTimeChange {
DefaultTableModel model2 = new DefaultTableModel();
String rowdata[] = new String[8];
Date[] date123 = new Date[8];
private SimpleDateFormat format = new SimpleDateFormat("HH:mm:dd:ss");
private JTable table = new JTable(getTableModel());
public TableTimeChange() {
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JFrame frame = new JFrame();
frame.add(new JScrollPane(table));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
Timer timer = new Timer(1000, new TimerListener());
timer.start();
frame.setVisible(true);
}
private TableModel getTableModel() {
String[] cols = {"Time"};
String array1[] = { "12-12-2001 10:18:30 PM", "12-15-2001 10:18:54 PM"};
String array2[] = {"01-19-1989 01:18:27 AM", "01-19-2019 01:28:36 PM"};
String array3[] = {"12-05-1989 05:18:57 PM", "08-25-1989 09:18:27 PM"};
String array4[] = { "10-15-1985 09:18:17 AM", "10-25-1985 06:48:27 AM"};
String array5[] = {"06-10-2001 03:09:19 AM", "05-16-2011 03:29:10 AM"};
String array6[] = {"01-12-2000 09:00:08 PM", "11-22-2010 07:00:56 PM"};
String array7[] = { "07-19-1969 12:18:47 PM", "02-05-1969 01:18:17 AM"};
String array8[] = {"01-09-2010 10:09:50 AM", "12-09-2010 05:09:10 PM"};
rowdata[0] = array1[0];
rowdata[1] = array2[0];
rowdata[2] = array3[0];
rowdata[3] = array4[0];
rowdata[4] = array5[0];
rowdata[5] = array6[0];
rowdata[6] = array7[0];
rowdata[7] = array8[0];
for(int t = 0; t<rowdata.length; t++)
{
DateFormat sdf = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss a");
DateFormat sdf3 = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss a");
try {
date123[t] = sdf.parse(rowdata[t]);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for(int i = 0; i<8; i++)
{
String[][] data = {{getFormatDate(date123[i])},{ getFormatDate(date123[i])}};
DefaultTableModel model = new DefaultTableModel(data, cols);
model2 = model;
}
return model2;
}
private String getFormatDate(Date date) {
return format.format(date);
}
private class TimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
DefaultTableModel model = (DefaultTableModel)table.getModel();
for (int i = model.getRowCount() -1; i >= 0; i--) {
model.setValueAt(getFormatDate(date123[i]), i, 0);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TableTimeChange();
}
});
}
}
答案 0 :(得分:3)
只需使用Swing Timer,就像我在评论中提到的那样。您需要向ActionListener
提供Timer
,其中actionPerformed
将每隔这么多毫秒被调用(无论您提供什么作为构造函数中的第一个参数)。在那里,您将使用新的Date
或格式化日期更新表模型(正如我在下面的示例中所做的那样),并根据该日期更改表中的任何数据。
免责声明:我是这样的。为什么我在格式中使用dd
?那应该不存在。它应该是&#34; HH:mm:ss&#34;。我懒得改变它。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class TableTimeChange {
private SimpleDateFormat format = new SimpleDateFormat("HH:mm:dd:ss");
private JTable table = new JTable(getTableModel());
public TableTimeChange() {
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JFrame frame = new JFrame();
frame.add(new JScrollPane(table));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
Timer timer = new Timer(1000, new TimerListener());
timer.start();
frame.setVisible(true);
}
private TableModel getTableModel() {
String[] cols = {"Time", "Is Seconds Even"};
Object[][] data = { { getNewRow() }, { getNewRow() } };
DefaultTableModel model = new DefaultTableModel(data, cols);
return model;
}
private String getFormatDate(Date date) {
return format.format(date);
}
private Object[] getNewRow() {
Calendar cal = Calendar.getInstance();
int seconds = cal.get(Calendar.SECOND);
boolean isSecondsEven = (seconds % 2 == 0);
return new Object[] { getFormatDate(cal.getTime()), isSecondsEven };
}
private class TimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
DefaultTableModel model = (DefaultTableModel)table.getModel();
for (int i = model.getRowCount() -1; i >= 0; i--) {
Object[] row = getNewRow();
model.setValueAt(row[0], i, 0);
model.setValueAt(row[1], i, 1);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TableTimeChange();
}
});
}
}
答案 1 :(得分:0)
您可以使用Timer
import java.util.Timer;
...
Timer timer = new Timer();
timer.schedule(new TimerTask()
{
@Override
public void run()
{
// Your code here
}
}, time);