JTable row limitation
I have to limit the number of rows in a JTable
. If I have 100 records I need to display 10 on the initial loading of JTable. I wish to put a button like "next"
, and after each click it shows another set of 10 records.
I have to limit the number of rows in a JTable. If i have 100 records i need to display 10 on the initial loading of JTable.
Use preferred size (+ an appropriate layout and layout constraint) to fix the size.
I wish to put a button like "next", and after each click it showing another set of 10 records.
Remove the scroll bar on the RHS of the scroll pane. Then use buttons instead for the effect of 'next/previous'.
Like this
FixedRowsTable.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
class FixedRowsTable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
String[] columns = {"1","2","3","4","5","6","7"};
Integer[][] data = new Integer[1000][columns.length];
for (int xx=0; xx<data.length; xx++) {
for (int yy=0; yy<data[0].length; yy++) {
data[xx][yy] = new Integer((xx+1)*(yy+1));
}
}
final int rows = 11;
JPanel gui = new JPanel(new BorderLayout(3,3));
final JTable table = new JTable(
new DefaultTableModel(data, columns));
final JScrollPane scrollPane = new JScrollPane(
table,
JScrollPane.VERTICAL_SCROLLBAR_NEVER,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
Dimension d = table.getPreferredSize();
scrollPane.setPreferredSize(
new Dimension(d.width,table.getRowHeight()*rows));
JPanel navigation = new JPanel(
new FlowLayout(FlowLayout.CENTER));
JButton next = new JButton(">");
next.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent ae) {
int height = table.getRowHeight()*(rows-1);
JScrollBar bar = scrollPane.getVerticalScrollBar();
bar.setValue( bar.getValue()+height );
}
} );
JButton previous = new JButton("<");
previous.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent ae) {
int height = table.getRowHeight()*(rows-1);
JScrollBar bar = scrollPane.getVerticalScrollBar();
bar.setValue( bar.getValue()-height );
}
} );
navigation.add(previous);
navigation.add(next);
gui.add(scrollPane, BorderLayout.CENTER);
gui.add(navigation, BorderLayout.SOUTH);
JOptionPane.showMessageDialog(null, gui);
}
});
}
}
If you use an AbstractTableModel you can display millions of records. The idea is that your model will be loading whatever records are needed for the view, on demand.
Here you have such a Model. It's not my best code, but will do :-) ...
public class SomeTableModel extends AbstractTableModel {
public SomeTableModel(ResultSet rs) {
this.rs = rs;
try {
pos = this.rs.getRow();
System.out.println(String.valueOf(pos));
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
public int getRowCount() {
int cnt = 0;
int apos = 0;
try {
apos = rs.getRow();
rs.last();
cnt = rs.getRow();
if (apos > 0)
rs.absolute(apos);
} catch (SQLException ex) {
System.out.println("getRowCount: " + ex);
}
return cnt;
}
public int getColumnCount() {
return 3;
}
public Object getValueAt(int rowIndex, int columnIndex) {
// make it jump back to pos !!
Object val = null;
Integer intVal;
try {
if (rowIndex == 0) {
pos = rs.getRow();
total = getRowCount();
}
rs.absolute(rowIndex + 1);
switch (columnIndex) {
case 0: intVal = rs.getInt(1); val = intVal; break;
case 1: val = rs.getString(2); break;
case 2: val = rs.getString(3); break;
default: val = "error";
}
rs.absolute(pos);
} catch (SQLException sqle) {
JOptionPane.showMessageDialog(null, "Trouble in model");
}
return val;
}
private ResultSet rs;
private int pos, total;
}
If you are loading the data from a database table, I think the best way to go is to limit the data coming from the database. Then apply a simple algorithm for the next and previous buttons.