Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed jTable to show 10 rows after time delay then load new records after all displayed
    primarykey
    data
    text
    <p><strong>SOLVED and EDITED with Working Version and left in case it can help someone else</strong></p> <p>The Rows of this jTable are being populated from a 2D String Array. The array will be dynamic. I need to only display 10 rows for 3 seconds, then display another 10 rows for 3 seconds, repeat this cycle until end of array then refill array and start cycle again.</p> <p>I can't find any examples of jTables using time delay to update table instead of buttons, data change etc.<br> <strong>EDIT</strong> - Fixed the rows to display with Timer ActionListener</p> <p>The rows to display will be selected from a jSpinner also but how can I implement the set Rows instead of showing all rows? Vertical and Horizontal scrolling is disabled in the jScrollPane.</p> <p><strong>EDIT</strong> - I am still stuck on how to "refill" and display the new records after displaying all records in my array 1 first time through..<strong>SOLVED with another Timer/ActionListener and new DefaultTableModel</strong></p> <pre><code>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 &lt;http://stackoverflow.com/questions/6175139/jtable-row-limitation&gt; 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); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload