使用工作版本解决和编辑并留下以防万一它可以帮助其他人
此 jTable 的行是从 2D 字符串数组中填充的。该数组将是动态的。我只需要显示 10 行 3 秒,然后再显示 10 行 3 秒,重复此循环直到数组结束,然后重新填充数组并再次开始循环。
我找不到任何使用时间延迟来更新表而不是按钮、数据更改等的 jTables 示例。
编辑- 修复了使用 Timer ActionListener 显示的行
要显示的行也将从 jSpinner 中选择,但是如何实现设置行而不是显示所有行?jScrollPane 中禁用了垂直和水平滚动。
编辑- 我仍然坚持如何“重新填充”并在第一次显示我的数组 1 中的所有记录后显示新记录..用另一个 Timer/ActionListener 和新的 DefaultTableModel 解决
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class DataTable2 {
String [][] data; //2D Array (MultiDimensional) for Records.
String [] titles; //Array for ColumnTitles
JTable table;
DefaultTableModel model;
JScrollPane pane;
JFrame frame;
JPanel panel;
public DataTable2(int rows, int radio){
//Fill Arrays.
titles = setTitles2(radio);
data = fill2();
//Timer Variables.
final int SPEED = 4000;//3.5 sec delay for displaying number of rows on screen.
int pause = 1000;
//Row Variable
final int R=rows;//For Timer ActionListener.
//Components
frame = new JFrame("Live Table");
panel = new JPanel();
model = new DefaultTableModel(data, titles);
table = new JTable(model){
@Override
public boolean isCellEditable(int rowIndex, int colIndex){
return false;
}};
pane = new JScrollPane(table, ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
//Table Formatting...More Done but not shown in example to keep shorter.
table.setRowHeight(100);
table.setFont(new Font("Arial", Font.BOLD,24));
table.getTableHeader().setFont(new Font("Arial", Font.BOLD, 24));
//Modified Code derived from FixedRowsTable <http://stackoverflow.com/questions/6175139/jtable-row-limitation> by @Andrew Thompson
//To show set number of rows of the table at a time. eg. 10 rows, timer, 10 rows, timer etc......
Dimension d = table.getPreferredSize();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
pane.setPreferredSize(new Dimension(screenSize.width-50, table.getRowHeight()*rows));
//Set as constants to be accessed by both ActionListeners.
final int HEIGHT = table.getRowHeight()*(R);
final JScrollBar BAR = pane.getVerticalScrollBar();
//Listener for Timer.
ActionListener perform = new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae) {
BAR.setValue(BAR.getValue()+HEIGHT);
}
};
//Listener for Timer 2.
ActionListener update = new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
BAR.setValue(0);//Reset Row Height.
DefaultTableModel model = new DefaultTableModel(data, titles);
table.setModel(model);
}
};
//Timer.
Timer timer = new Timer(SPEED, perform);
timer.setRepeats(true);
timer.start();
//Timer 2.
double val = table.getRowCount();///R;
int time = 0;
//Formula to compensate for SwingTimer accepting only Integer Values.
if(val%R!=0){
time=SPEED;
}
time+= (table.getRowCount()/R)*SPEED;
//Timer 2
Timer timer2 = new Timer(time, update);
timer2.setRepeats(true);
timer2.start();
//Debug
System.out.println(time);
System.out.println(val);
System.out.println(val%R);
//Set Panel to Full Screen
panel.setPreferredSize(new Dimension (screenSize.width,screenSize.height));
//Add Components.
frame.add(panel);
panel.add(pane);
//Frame Close Operation.
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//Table Constraints.
table.getTableHeader().setReorderingAllowed(false);
table.getTableHeader().setResizingAllowed(true);
table.setShowVerticalLines(false);
table.setShowHorizontalLines(true);
//Apply Model to Table
table.setModel(model);
//Add table to scroll pane.
pane.setViewportView(table);
//Finish Frame
frame.pack();
frame.setLocationByPlatform(false);
frame.setVisible(true);
}
/**
* Method to fill table with String values for testing outside of network.
* @return String array of Records.
*/
private String[][] fill2(){
String[][] temp = {{"Record1", "Record2", "Record3", "Record4", "Record5", "Record6"},
{"Record7", "Record8", "Record9", "Record10", "Record11", "Record12"},
{"Record13", "Record14", "Record15", "Record16", "Record17", "Record18"},
{"Record19", "Record20", "Record21", "Record22", "Record23", "Record24"},
{"Record25", "Record26", "Record27", "Record28", "Record29", "Record30"},
{"Record31", "Record32", "Record33", "Record34", "Record35", "Record36"},
{"Record37", "Record38", "Record39", "Record40", "Record41", "Record42"},
{"Record43", "Record44", "Record45", "Record46", "Record47", "Record48"},
{"Record49", "Record50", "Record51", "Record52", "Record53", "Record54"},
{"Record55", "Record56", "Record57", "Record58", "Record59", "Record60"},
{"Record61", "Record62", "Record63", "Record64", "Record65", "Record66"},
{"Record67", "Record68", "Record69", "Record70", "Record71", "Record72"},
{"Record73", "Record74", "Record75", "Record76", "Record77", "Record78"},
};
return temp;
}
/**
* Method to fill String array of column titles.
* @param rad jRadioButton value from buttonGroup sent from main settings frame.
* @return Column Titles
*/
private String[] setTitles2(int rad){
String[] t;
if(rad==2){
t = new String[]{"TITLE1", "TITLE2"};
}else if(rad==5||rad==10){
t = new String[]{"TITLE1", "TITLE2", "TITLE3", "TITLE4", "TITLE5", "TITLE6", "TITLE7", "TITLE8"};
}else if(rad==6){
t = new String[]{"TITLE1", "TITLE2"};
}else if(rad==7){
t = new String[]{"TITLE1"};
}else if(rad==8||rad==9){
t = new String[]{"TITLE1", "TITLE2", "TITLE3", "TITLE4", "TITLE5"};
}else{
t=new String []{"TITLE1", "TITLE2", "TITLE3", "TITLE4", "TITLE5", "TITLE6"};
}
return t;
}
//Main sample just to show table.
public static void main(String[] args) {
//Sample Value for Example.
int x = 10; //Any Value from 1-10 will be used. 5 or 10 will be used most reguarly.
int y = 1; //Any Value from 1-9 will be used. 1 will be used most reguarly.
//Call Table.
DataTable2 table = new DataTable2(x,y);
}
}