3 回答

TA貢獻1828條經(jīng)驗 獲得超3個贊
這個情況我遇到過,我的解決方法是:排序之前把jtable內(nèi)容設(shè)置為空,再調(diào)用排序,然后調(diào)用顯示的。希望你能解決你的問題

TA貢獻1818條經(jīng)驗 獲得超7個贊
你這代碼完全netbeans拖出來的,都不知道jtable的基本概念,數(shù)據(jù)直接加到j(luò)table的Model里,不用你管理界面刷新,table有對model的監(jiān)聽,會自己刷

TA貢獻1821條經(jīng)驗 獲得超5個贊
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;
public class Test extends javax.swing.JFrame {
private Vector<Vector> tblDate; // 表數(shù)據(jù)
private Vector<String> tblHeaders; // 表頭
private List<Integer> list; // 數(shù)據(jù)源
public Test() {
tblDate = new Vector<Vector>();
tblHeaders = new Vector<String>();
list = new ArrayList<Integer>();
tblHeaders.add("編號");
tblHeaders.add("數(shù)據(jù)");
initComponents();
setLocationRelativeTo(null);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// 添加
list.add(list.size());
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// 清空
list.clear();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// 刷新
tblDate.clear();
for (int i : list) {
tblDate.add(new Vector(Arrays.asList(i, "測試數(shù)據(jù)" + i)));
}
jTable1.setModel(new javax.swing.table.DefaultTableModel(
this.tblDate, this.tblHeaders
));
jTable1.repaint();
jTable1.updateUI();
}
// 下面都是NetBeans自動生成代碼,不要管
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info
: javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception ex) {
// TODO
}
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Test().setVisible(true);
}
});
}
@SuppressWarnings("unchecked")
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTable1.setModel(new javax.swing.table.DefaultTableModel(
this.tblDate, this.tblHeaders
));
jTable1.setAutoCreateRowSorter(true);
jScrollPane1.setViewportView(jTable1);
jButton2.setText("添加");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("清空");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton1.setText("刷新");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing
.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout
.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout
.PREFERRED_SIZE, 0, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGap(0, 168, Short.MAX_VALUE)
.addComponent(jButton2)
.addGap(18, 18, 18)
.addComponent(jButton3)
.addGap(18, 18, 18)
.addComponent(jButton1)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1,
javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short
.MAX_VALUE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout
.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2)
.addComponent(jButton3))
.addContainerGap())
);
pack();
}
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
}
原因不知道
添加回答
舉報