Java 更新数据时刷新JTable
下面是我的代码:-Java 更新数据时刷新JTable,java,swing,jtable,Java,Swing,Jtable,下面是我的代码:- public class MainScreen extends javax.swing.JFrame { private TableRowSorter<TableModel> sorter; public MainScreen() { initComponents(); this.setSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize());
public class MainScreen extends javax.swing.JFrame {
private TableRowSorter<TableModel> sorter;
public MainScreen() {
initComponents();
this.setSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize());
sorter = new TableRowSorter<>(tblCustomer.getModel());
tblCustomer.setRowSorter(sorter);
List<BasicDetailsDTO> findAll = UtilDAO.getDaoBasicDetails().findAll();
System.out.println("I'm here "+findAll.size());
((DefaultTableModel) tblCustomer.getModel()).setDataVector(getDataVector(findAll), getVectorHeader());
tblCustomer.setAutoCreateRowSorter(true);
tblCustomer.getColumnModel().getColumn(0).setMinWidth(0);
tblCustomer.getColumnModel().getColumn(0).setMaxWidth(0);
}
public static Vector getDataVector(List<BasicDetailsDTO> listData) {
Vector dataVector = new Vector();
for (BasicDetailsDTO instance : listData) {
Vector row = new Vector();
row.add(instance.getId());
row.add(instance.getParticulars());
row.add(instance.getBookedBy());
row.add(instance.getContactPerson());
row.add(instance.getMobileNo());
row.add(instance.getEmail_id());
dataVector.add(row);
}
return dataVector;
}
public static Vector getVectorHeader() {
Vector header = new Vector();
header.add("ID");
header.add("Particulars");
header.add("BOOKED BY");
header.add("CONTACT PERSON");
header.add("MOBILE NO");
header.add("EMAIL ID");
return header;
}
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
displayPanel(new HomePage(), "Details Of Customer", 1200, 800);
}
private void tblCustomerKeyPressed(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
}
private void tblCustomerMousePressed(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
}
private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (tblCustomer.getSelectedRow() == -1) {
displayError("Please Select the Record");
return;
}
int option = displayConfirmDialog("Do you Really want to delete Record ?");
if (option == JOptionPane.YES_OPTION) {
String recordId = tblCustomer.getValueAt(tblCustomer.getSelectedRow(), 0).toString();
BasicDetailsDTO instance = UtilDAO.getDaoBasicDetails().findById(Integer.parseInt(recordId));
instance.setDeleted(Boolean.TRUE);
UtilDAO.getDaoBasicDetails().remove(instance);
List<BasicDetailsDTO> findAll = UtilDAO.getDaoBasicDetails().findAll();
getDataVector(findAll);
displayMessage(" Record Deleted ");
}
}
private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (tblCustomer.getSelectedRow() == -1) {
displayError("Please select record.");
return;
}
String recordID = tblCustomer.getValueAt(tblCustomer.getSelectedRow(), 0).toString();
BasicDetailsDTO instance = UtilDAO.getDaoBasicDetails().findById(Integer.parseInt(recordID));
displayPanel(new HomePage(instance, 1), "Customer " + instance.getBillingName(), 1200, 1000);
}
private void tblCustomerMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
if (evt.getClickCount() == 2) {
String recordID = tblCustomer.getValueAt(tblCustomer.getSelectedRow(), 0).toString();
BasicDetailsDTO instance = UtilDAO.getDaoBasicDetails().findById(Integer.parseInt(recordID));
displayPanel(new HomePage(instance, 1), "Customer " + instance.getBillingName(), 1000, 1000);
}
}
private void btnViewHotelListActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
displayPanel(new ViewHotelDetails(), "List Of Hotels", 800, 700);
}
private void btnViewAgencyListActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
displayPanel(new ViewAgencyDetails(), "List Of Hotels", 800, 700);
}
private void txtSearchKeyReleased(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
if (evt.getKeyCode() != KeyEvent.VK_ENTER && evt.getKeyCode() != KeyEvent.VK_DOWN) {
if (txtSearch.getText().trim().length() > 0) {
RowFilter<TableModel, Object> filter = new RowFilter<TableModel, Object>() {
@Override
public boolean include(javax.swing.RowFilter.Entry<? extends TableModel, ? extends Object> entry) {
String search = txtSearch.getText().trim().toLowerCase();
// System.out.println(entry.getStringValue(1));
return (entry.getValue(1).toString().toLowerCase().indexOf(search) != -1 || entry.getValue(2).toString().toLowerCase().indexOf(search) != -1 || entry.getValue(3).toString().toLowerCase().indexOf(search) != -1);
}
};
sorter.setRowFilter(filter);
//sorter.setRowFilter(null);
tblCustomer.setRowSorter(sorter);
// System.out.println("New Row is " + filter);
} else {
sorter.setRowFilter(null);
tblCustomer.setRowSorter(sorter);
}
} else {
if (tblCustomer.getRowCount() > 0) {
tblCustomer.requestFocus();
tblCustomer.setRowSelectionInterval(0, 0);
} else {
txtSearch.requestFocus();
}
}
}
private void btnInvoiceActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
InputStream in = MainScreen.class.getResourceAsStream("Passenger_Name.docx");
IXDocReport report = XDocReportRegistry.getRegistry().loadReport(in, TemplateEngineKind.Velocity);
IContext context = report.createContext();
if (tblCustomer.getSelectedRow() == -1) {
displayError("Please select record.");
return;
}
String recordID = tblCustomer.getValueAt(tblCustomer.getSelectedRow(), 0).toString();
BasicDetailsDTO instance = UtilDAO.getDaoBasicDetails().findById(Integer.parseInt(recordID));
context.put("Customer", instance);
OutputStream out = new FileOutputStream(new File("Passenger Name_Out.docx"));
report.process(context, out);
Desktop desktop = Desktop.getDesktop();
File f = new File("Passenger Name_Out.docx");
desktop.open(f);
} catch (IOException | XDocReportException ex) {
Logger.getLogger(MainScreen.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* @param args the command line arguments
*/
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 (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MainScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MainScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MainScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MainScreen.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() {
try {
UIManager.setLookAndFeel("com.jtattoo.plaf.texture.TextureLookAndFeel");
} catch (ClassNotFoundException ex) {
Logger.getLogger(MainScreen.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(MainScreen.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(MainScreen.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(MainScreen.class.getName()).log(Level.SEVERE, null, ex);
}
new MainScreen().setVisible(true);
}
});
}
public static void setlblMessageDetail(String msg) {
MainScreen.lblMessage.setHorizontalAlignment(JLabel.CENTER);
MainScreen.lblMessage.setText(msg);
}
// Variables declaration - do not modify
}
public类主屏幕扩展了javax.swing.JFrame{
私人桌面分拣机;
公共主屏幕(){
初始化组件();
setSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize());
sorter=新表格行sorter(tblCustomer.getModel());
TBL客户设置行分拣机(分拣机);
List findAll=UtilDAO.getDaoBasicDetails().findAll();
System.out.println(“我在这里”+findAll.size());
((DefaultTableModel)tblCustomer.getModel()).setDataVector(getDataVector(findAll),getVectorHeader());
tblCustomer.setAutoCreateRowSorter(真);
tblCustomer.getColumnModel().getColumn(0).setMinWidth(0);
tblCustomer.getColumnModel().getColumn(0).setMaxWidth(0);
}
公共静态向量getDataVector(列表listData){
向量数据向量=新向量();
for(BasicDetailsTo实例:listData){
向量行=新向量();
add(instance.getId());
add(instance.getDetails());
add(instance.getBookedBy());
添加(instance.getContactPerson());
add(instance.getMobileNo());
添加(instance.getEmail_id());
dataVector.add(行);
}
返回数据向量;
}
公共静态向量getVectorHeader(){
向量头=新向量();
标题。添加(“ID”);
标题。添加(“详情”);
标题。添加(“记账人”);
标题。添加(“联系人”);
标题。添加(“手机号码”);
标题。添加(“电子邮件ID”);
返回头;
}
私有void btnAddActionPerformed(java.awt.event.ActionEvent evt){
//TODO在此处添加您的处理代码:
显示面板(新主页(),“客户详细信息”,1200,800);
}
私有无效tblCustomerKeyPressed(java.awt.event.KeyEvent evt){
//TODO在此处添加您的处理代码:
}
private void tblcustomemousepressed(java.awt.event.MouseEvent evt){
//TODO在此处添加您的处理代码:
}
私有void btnDeleteActionPerformed(java.awt.event.ActionEvent evt){
//TODO在此处添加您的处理代码:
if(tblCustomer.getSelectedRow()=-1){
显示错误(“请选择记录”);
返回;
}
int option=displayConfirmDialog(“是否确实要删除记录?”);
if(option==JOptionPane.YES\u选项){
String recordId=tblCustomer.getValueAt(tblCustomer.getSelectedRow(),0).toString();
BasicDetailsTo实例=UtilDAO.getDaoBasicDetails().findById(Integer.parseInt(recordId));
setDeleted(Boolean.TRUE);
UtilDAO.getDaoBasicDetails().remove(实例);
List findAll=UtilDAO.getDaoBasicDetails().findAll();
getDataVector(findAll);
显示消息(“记录已删除”);
}
}
私有void btnEditActionPerformed(java.awt.event.ActionEvent evt){
//TODO在此处添加您的处理代码:
if(tblCustomer.getSelectedRow()=-1){
显示错误(“请选择记录”);
返回;
}
String recordID=tblCustomer.getValueAt(tblCustomer.getSelectedRow(),0).toString();
BasicDetailsTo实例=UtilDAO.getDaoBasicDetails().findById(Integer.parseInt(recordID));
displayPanel(新主页(实例1),“Customer”+实例.getBillingName(),12001000);
}
private void TBLCustomeMouseClicked(java.awt.event.MouseEvent evt){
//TODO在此处添加您的处理代码:
if(evt.getClickCount()==2){
String recordID=tblCustomer.getValueAt(tblCustomer.getSelectedRow(),0).toString();
BasicDetailsTo实例=UtilDAO.getDaoBasicDetails().findById(Integer.parseInt(recordID));
displayPanel(新主页(实例1),“Customer”+实例.getBillingName(),10001000);
}
}
private void btnviewhotellistationperformed(java.awt.event.ActionEvent evt){
//TODO在此处添加您的处理代码:
显示面板(新视图HotelDetails(),“酒店列表”,800700);
}
private void BTNViewagencylistationPerformed(java.awt.event.ActionEvent evt){
//TODO在此处添加您的处理代码:
显示面板(新ViewAgencyDetails(),“酒店列表”,800700);
}
private void txtSearchKeyReleased(java.awt.event.KeyEvent evt){
//TODO在此处添加您的处理代码:
如果(evt.getKeyCode()!=KeyEvent.VK_输入&&evt.getKeyCode()!=KeyEvent.VK_向下){
if(txtSearch.getText().trim().length()>0){
RowFilter筛选器=新的RowFilter(){
@凌驾
公共布尔包含(javax.swing.RowFilter.Entry
为什么DefaultTableModel
和JTableHeader
的无效是静态的
从DefaultTableModel
对于RowFilter
为什么会有两种不同的LookAndFeel
s
必须在EDT上更新DefaultTableModel
,更多信息请访问Oracle tuto