我从来不知道JTable
没有分页。因此,我现在仍然坚持使用JTable
,我需要实现分页。我发现了很多Java Swing分页的例子,但它们并没有在netbeans中实现。我将如何在我的项目中实施分页(下面的相关代码)?。
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Joseph
*/
public class SupplierForm extends javax.swing.JFrame {
/**
* Creates new form SupplierForm
*/
public SupplierForm() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
supplierCombo = new javax.swing.JComboBox();
jScrollPane1 = new javax.swing.JScrollPane();
dataTable = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
supplierCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "USER", "Item 3", "Item 4" }));
supplierCombo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
supplierComboActionPerformed(evt);
}
});
dataTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"ID", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(dataTable);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(173, 173, 173)
.addComponent(supplierCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 660, Short.MAX_VALUE)))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(supplierCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(31, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(34, 34, 34))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void supplierComboActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String tableName = (String) supplierCombo.getModel().getSelectedItem();
DefaultTableModel model = (DefaultTableModel) dataTable.getModel();
int c = model.getRowCount();
for (int i=c-1; i>=0; i--){
model.removeRow(i);
dataTable.revalidate();
}
String sql = "select * from "+ tableName+" ";
try {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException ex) {
Logger.getLogger(SupplierForm.class.getName()).log(Level.SEVERE, null, ex);
}
Connection connect = (Connection) DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\\Merlin1.accdb");
Statement statmnt = connect.createStatement();
ResultSet rslt = statmnt.executeQuery(sql);
while(rslt.next()){
String id = rslt.getString("ID");
String name = rslt.getString("USER_NAME");
String surname = rslt.getString("PASSWORD");
//String age = rslt.getString("Age");
model.addRow(new Object[]{id,name,surname});
}
} catch(SQLException e){
JOptionPane.showMessageDialog(this,"No Supplier exists with this name");
}
// dataTable.revalidate();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(SupplierForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(SupplierForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(SupplierForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(SupplierForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SupplierForm().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTable dataTable;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JComboBox supplierCombo;
// End of variables declaration
}
答案 0 :(得分:1)
你可以用两种方式创建分页
Database
<{1}} 上的
在SQL statement
中显示已删除的垂直和水平JScrollPane
的行数减少,方法ScrollBar
可以在JScrollBar.setValue(int)
内移动JTable
答案 1 :(得分:0)
int crt = 1;
private void PREVMouseClicked(java.awt.event.MouseEvent evt) {
DefaultTableModel tm = (DefaultTableModel)jTable.getModel();
tm.setRowCount(0);
try{
ctr --;
int page_num = (ctr-1)*5;
connect = DriverManager.getConnection("jdbc:mysql://localhost/database","root","");
state = connect.createStatement();
if(page_num <=0 ){
PREV.setSize(0,0);
next.setSize(89,15);
res = state.executeQuery("SELECT * FROM `item` ORDER BY `id` ASC LIMIT 0,5");
while(res.next()){
int item = res.getInt("id");
String item1 = res.getString("item1");
String item2 = res.getString("item2");
Object record [] = {item,item1,item2};
tm.addRow(record);
}
ctr = 1;
}else{
next.setSize(89,15);
res = state.executeQuery("SELECT * FROM `item` ORDER BY `id` ASC LIMIT "+page_num+",5");
while(res.next()){
int item = res.getInt("id");
String item1 = res.getString("item1");
String item2 = res.getString("item2");
Object record [] = {item,item1,item2};
tm.addRow(record);
}
}
}catch(SQLException e){
System.out.print(e.getMessage());
}
}
private void nextMouseClicked(java.awt.event.MouseEvent evt) {
ctr++;
int page_num = (ctr-1)*5;
DefaultTableModel tm = (DefaultTableModel)jTable.getModel();
tm.setRowCount(0);
int id = 0;
try{
connect = DriverManager.getConnection("jdbc:mysql://localhost/database","root","");
state = connect.createStatement();
res = state.executeQuery("SELECT * FROM `item` ORDER BY `id` ASC");
while(res.next()){
id++;
}
}catch(SQLException e){
System.out.print(e.getMessage());
}
if(id%5 == 0){
int ans = id - 5;
int ans1 = id / 5;
int last = (ans1 * 5) - 5;
try{
if(page_num == ans && page_num == 5){
next.setSize(0,0);
PREV.setSize(89,15);
res = state.executeQuery("SELECT * FROM `item` ORDER BY `id` ASC LIMIT "+ans+",5");
while(res.next()){
int item = res.getInt("id");
String item1 = res.getString("item1");
String item2 = res.getString("item2");
Object record [] = {item,item1,item2};
tm.addRow(record);
}
}else{
if(ctr >= ans1){
next.setSize(0,0);
PREV.setSize(89,15);
res = state.executeQuery("SELECT * FROM `item` ORDER BY `id` ASC LIMIT "+last+",5");
while(res.next()){
int item = res.getInt("id");
String item1 = res.getString("item1");
String item2 = res.getString("item2");
Object record [] = {item,item1,item2};
tm.addRow(record);
}
}else{
PREV.setSize(89,15);
res = state.executeQuery("SELECT * FROM `item` ORDER BY `id` ASC LIMIT "+page_num+",5");
while(res.next()){
int item = res.getInt("id");
String item1 = res.getString("item1");
String item2 = res.getString("item2");
Object record [] = {item,item1,item2};
tm.addRow(record);
}
}
}
}catch(SQLException e){
System.out.print(e.getMessage());
}
}else{
int ans = (id/5) + 1;
int last = (ans * 5)-5;
try{
if(ctr == ans || ctr > ans){
next.setSize(0,0);
PREV.setSize(89,15);
res = state.executeQuery("SELECT * FROM `item` ORDER BY `id` ASC LIMIT "+last+",5");
while(res.next()){
int item1 = res.getInt("id");
String size_Code = res.getString("size_code");
String size_Name = res.getString("size_name");
Object record [] = { item1, size_Code, size_Name};
tm.addRow(record);
}
}else{
PREV.setSize(89,15);
res = state.executeQuery("SELECT * FROM `item` ORDER BY `id` ASC LIMIT "+page_num+",5");
while(res.next()){
int item = res.getInt("id");
String item1 = res.getString("item1");
String item2 = res.getString("item2");
Object record [] = {item,item1,item2};
tm.addRow(record);
}
}
}catch(SQLException e){
System.out.print(e.getMessage());
}
}
}