简体   繁体   中英

How to Make empty row editable in JXTable

I have a jXTable that is linked to a MySQL database. I am using the DefaultTableModel. I have it so far that the user can edit the data in the table. I have been trying to add an "Insert New Row" button but was unsuccessful. In the meantime, I thought I could just have an empty row added at the end of the table every time the user updates the table. I was able to get my empty row with the addRow() method, but it isn't editable. Here is my code so far. Any tips on how to get the empty row editable?

     public void rowSetChanged(RowSetEvent event) {         
             Object[] record;
             Object[] emptyrow;
             String [] columnNames=new String [] {"DATE RECEIVED", "INCOME NAME", 
                "DESCRIPTION", "AMOUNT", "FUND ALLOCATION"};

             DefaultTableModel dtm=new DefaultTableModel(columnNames,0){
                    Class[] types = new Class [] {
                        java.sql.Date.class, java.lang.String.class, java.lang.String.class, 
                        java.lang.Object.class, java.lang.Float.class, java.lang.String.class};
                        public Class getColumnClass(int columnIndex) {
                            return types [columnIndex];
                        }
                };              

             try {    
                IncomeUI.frs.beforeFirst();                   
                        while (IncomeUI.frs.next()){
                            record=new Object[]{IncomeUI.frs.getDate("DateReceived"), 
                            IncomeUI.frs.getString("IncomeName"), IncomeUI.frs.getString("Description"), 
                            IncomeUI.frs.getBigDecimal("Amount"),IncomeUI.frs.getString("FundAllocation")};
                        dtm.addRow(record);
               }              
             } catch (SQLException ex) {ex.printStackTrace();} 

             emptyrow = new Object[]{" ", " ", " ", " ", " "};
             dtm.addRow(emptyrow);

            this.setModel(dtm);   
            this.setShowHorizontalLines(false);
            this.setShowVerticalLines(false);     
            IncomeUI.jScrollPane2.setViewportView(this);  
            //System.out.println(event.toString());
}

Well, not a 100% fix but it works. I ended up just adding code to add a new row with fake data (such as "Edit Me"). The user can just re-edit that. It works for now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM