gpt4 book ai didi

java - Netbeans JTable 分页

转载 作者:行者123 更新时间:2023-11-30 11:44:38 32 4
gpt4 key购买 nike

我从来不知道 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
}

最佳答案

您可以通过两种方式创建分页

  1. 数据库端,在SQL语句

  2. 在删除垂直和水平ScrollBarJScrollPane 中显示减少的行数,方法JScrollBar.setValue(int) 可以在 JScrollPane

  3. 中移动 JTable

关于java - Netbeans JTable 分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10706962/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com