Yo dudes. I need so suggestions on how to best implement something please. My program:
I have a swing program that reads a file with multiple records of different types. I read these records into java objects, make JTables of them with their headings and content (so every table only has the heading and 1 record), add these tables to a panel and add the panel to a scrollpane. There are lots of records, about 70000, so i cant load everything into memory at once.
My plan is to paginate the display by only displaying say 100 records at a time, and if you scroll past a certain point, lets say table nr 50, it will load the next 100 tables and add them to the scrollpane.
This brings me to my questions
1) Im not sure how to notify myself that ive scrolled past a certain point. So given that i have 100 JTables attached to a JPanel attached to a scrollpane. How can i add a listener to notify when the user has scrolled past say Jtable 50 for arguments sake.
2) The other issue im having is related to how i put my tables into the panel. Im using a swingworker as below:
private class TableRun extends SwingWorker<Void, JTable> {
private ArrayDeque<FileRecord> fileRecords;
private final GridBagConstraints gc;
private final JPanel parentPanel;
int counter = 1;
TableRun(ArrayDeque<FileRecord> fileRecords, GridBagConstraints gc, JPanel parentPanel) {
this.fileRecords = fileRecords;
this.gc = gc;
this.parentPanel = parentPanel;
}
@Override
protected Void doInBackground() {
Iterator<FileRecord> iterator = fileRecords.iterator();
while (iterator.hasNext()) {
publish(getTabel(iterator.next()));
}
return null;
}
@Override
protected void process(final List<JTable> tables) {
Iterator<JTable> iterator = tables.iterator();
while(iterator.hasNext()) {
JTable table = iterator.next();
gc.fill = 1;
parentPanel.add(table.getTableHeader(), gc);
gc.gridy++;
parentPanel.add(table,gc);
gc.gridy++;
//validate takes a long time so i try to not do it too often.
if(counter == 50 || (counter % 5000) ==0 || (counter == fileRecords.size())) {
validate();
}
System.out.println("Sequence Nr : " + table.getModel().getValueAt(0,1) + " - Counter :" + counter++);
}
}
}
I feed my record objects to the swingworker. It then converts the record into the JTable with all the necessary formatting, then it adds them to the panel. It sometimes calls validate in order to display the tables, else it doesnt display them. (not sure if theres a better way to do this?)
So my question is related to the scrolling. If i initially only feed 100 of the 70000 records to my swinworker it obviously creates the scrollpane thinking theres only 100 records. So the scrollbar isnt very small (ie its clear that you cant scroll very far). What i actually want is that even though im only showing 100 records, i want the size of the scrollpane to be that of 70000 records so that you can see the size of the records based on the scrollbar. If you drag the scrollbar to some point at the bottom it them needs to pull and display those records. How can i create the size of my scrollpane bigger than the actual components in it, but based on their size?
hope my question makes sense
Thanks