Results 1 to 5 of 5
  1. #1
    shedo is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default tablemodellistener problem

    Hi all,
    I have a problem with the tablemodellistener method, i add this method in my table(code) but when i edit the cell in the table and press enter, the velue returns like the previous one.

    Can you help me?

    This is my code(table):

    Java Code:
    import java.util.Vector;
    import javax.swing.event.TableModelEvent;
    import javax.swing.event.TableModelListener;
    import javax.swing.table.TableModel;
    
    /**
     *
     * @author Shedo
     */
    public class TabOperatoriAssegnamento extends javax.swing.JFrame {
    
        TableModel dataModel;
        Calendario x = new Calendario();
        Server [] server;
        server_Ass serverAss;
        String [] tableList;
        String operatore;
        /** Creates new form TabOperatoriAssegnamento */
        public TabOperatoriAssegnamento(Server [] array,String nome) {
            server=array;
            serverAss=(server_Ass)array[2];
            operatore=nome;
             try {
                tableList=serverAss.getTableList();
            } catch (LoginException e) {}
            //serverAss.caricaArray("11_2010")
            
    
            Vector v=new Vector(serverAss.getAssOperatore(nome, x.getDataOggi()).length);
    
            for(int i=0;i<serverAss.getAssOperatore(nome, x.getDataOggi()).length;i++){
                v.add(serverAss.getAssOperatore(nome, x.getDataOggi())[i]);
    
    
             }
            dataModel = new MyModelAss(v);
            initComponents();
    
            [B]dataModel.addTableModelListener(new TableModelListener() {
    
          public void tableChanged(TableModelEvent e) {
             System.out.println(e);
          }
        });[/B]
    
        }
    
    
    
        /** 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() {
    
            jScrollPane1 = new javax.swing.JScrollPane();
            jTable1 = new javax.swing.JTable();
            jLabel1 = new javax.swing.JLabel();
            choice1 = new java.awt.Choice();
            choice2 = new java.awt.Choice();
            jLabel2 = new javax.swing.JLabel();
            jLabel3 = new javax.swing.JLabel();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowActivated(java.awt.event.WindowEvent evt) {
                    formWindowActivated(evt);
                }
            });
    
            jTable1.setModel(dataModel);
            jScrollPane1.setViewportView(jTable1);
    
            jLabel1.setText("Vista Assegnamenti per operatore");
    
            jLabel2.setText("Giorno");
    
            jLabel3.setText("Mese_Anno");
    
            jButton1.setText("Indietro");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            jButton2.setText("Cerca");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(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)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addGap(53, 53, 53)
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(choice1, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(24, 24, 24)
                            .addComponent(jLabel3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(choice2, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(77, 77, 77)
                            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 838, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addGap(34, 34, 34)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jButton2, 0, 0, Short.MAX_VALUE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addComponent(choice1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(choice2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGap(18, 18, 18)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 292, Short.MAX_VALUE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jButton1)
                    .addGap(12, 12, 12))
            );
    
            pack();
        }// </editor-fold>
    
    
    
    
    
    
    
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
            dispose();
            Operatori operatori = new Operatori(server);
            operatori.setVisible(true);
        }                                        
    
    
        public String reversedate(String x)
        {
         String tempMese;
         String tempAnno;
         tempMese=x.substring(0,2);
         tempAnno=x.substring(3,7);
         return tempAnno+"-"+tempMese;
    }
        private void formWindowActivated(java.awt.event.WindowEvent evt) {                                     
            // TODO add your handling code here:
            for(int i=tableList.length-1;i>=0;i--){
                choice2.add(tableList[i]);
            }
            choice2.select(x.getMeseAnno());
            for(int i=0;i<x.GetGiorni().length;i++)
            {
             choice1.add(x.GetGiorni()[i]);
            }
            choice1.select(x.GetGiorni()[Integer.parseInt(x.getDataOggi().substring(8))]);
        }                                    
    
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
            if(choice1.getSelectedItem().compareTo("Giorno")==0)
            {
             try{
                serverAss.caricaArray(choice2.getSelectedItem());
            } catch (LoginException e) {}
            int lunghezza=serverAss.getAssOperatore(operatore).length;
            Vector x=new Vector(lunghezza);
            for(int i=0;i<lunghezza;i++){
                x.add(serverAss.getAssOperatore(operatore)[i]);
            }
            dataModel = new MyModelAss(x);
            jTable1.setModel(dataModel);
            }
            else {
            String giorno = choice1.getSelectedItem();
            String mese_anno = choice2.getSelectedItem();
            String temp= reversedate(mese_anno);
            if(Integer.parseInt(giorno)<10)
            temp+="-0"+giorno;
            else
            temp+="-"+giorno;
            try{
                serverAss.caricaArray(choice2.getSelectedItem());
            } catch (LoginException e) {}
            int lunghezza=serverAss.getAssOperatore(operatore, temp).length;
            Vector x=new Vector(lunghezza);
            for(int i=0;i<lunghezza;i++){
                x.add(serverAss.getAssOperatore(operatore, temp)[i]);
            }
            dataModel = new MyModelAss(x);
            jTable1.setModel(dataModel);
         
    
            
    
            
          }
    
        }                                        
    
        /**
        * @param args the command line arguments
        */
        
        // Variables declaration - do not modify
        private java.awt.Choice choice1;
        private java.awt.Choice choice2;
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTable jTable1;
        // End of variables declaration
    
    }
    
    
    [B][COLOR="Red"]This is my code(MyModel):[/COLOR][/B]
    
    import java.util.Vector;
    import javax.swing.event.TableModelListener;
    import javax.swing.table.AbstractTableModel;
    import javax.swing.table.TableModel;
    
    /**
     *
     * @author shedo
     */
    public class MyModelAss extends AbstractTableModel   {
    
    
        String[] ColName = {"Operatore","Id","Appalto","Data","inizio diurno","fine diurno","inizio notturno","fine notturno","inizio festivo","fine"
                + "festivo","inizio diurno","fine diurno","inizio notturno","fine notturno","inizio festivo","finefestivo"};
        Vector v = null;
    
        protected TableModel model;
    
    
        public MyModelAss(Vector v)
        {
    
         this.v=v;
        }
    
        public int getColumnCount(){
            return ColName.length;
        }
        public int getRowCount(){
            return v.size();
        }
        public Object getValueAt(int row,int col){
            gestioneAssegnamenti b = (gestioneAssegnamenti)v.elementAt(row);
              String val = null;
              // la stringa corrispondente alla colonna
              switch (col){
                case 0: val = b.getString()[0]; break;
                case 1: val = b.getString()[1]; break;
                case 2: val = b.getString()[2]; break;
                case 3: val = b.getString()[3]; break;
                case 4: val = b.getString()[4]; break;
                case 5: val = b.getString()[5]; break;
                case 6: val = b.getString()[6]; break;
                case 7: val = b.getString()[7]; break;
                case 8: val = b.getString()[8]; break;
                case 9: val = b.getString()[9]; break;
                case 10: val = b.getString()[10]; break;
                case 11: val = b.getString()[11]; break;
                case 12: val = b.getString()[12]; break;
                case 13: val = b.getString()[13]; break;
                case 14: val = b.getString()[14]; break;
                case 15: val = b.getString()[15]; break;
    
    
    
                default: val = "";
                }
                return val;
        }
    
        public String getColumnName(int columnIndex)
        {
         return ColName[columnIndex];
        }
    
        public boolean isCellEditable(int row,int col){
            return true;
        }
    
    
    
    }
    Moderator Edit: code tags added
    Last edited by Fubarable; 11-10-2010 at 03:52 PM. Reason: Moderator Edit: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Code tags added so your code is readable. To see how to do this yourself, please check out the first link in my signature.

    When you use an AbstractTableModel, you are responsible for everything in the model. I don't see where you've implemented setValueAt(...), and so it makes sense to me that your table will not allow updates. My initial recommendation is that you create this method for your model and use it when updating the table, but a better rec is to use the DefaultTableModel if possible since does much more for you and it's a lot easier to use. If you absolutely must use AbstractTableModel (or even if not), you absolutely must read the Swing tutorial on how to use before moving forward.

    Luck.

  3. #3
    shedo is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    thanks for the quick replay... can you give me any link with tutorial or example for this topic...

    thanks again... Ivan

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    shedo is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

Similar Threads

  1. TableModelListener
    By NateLogan in forum AWT / Swing
    Replies: 6
    Last Post: 03-15-2010, 09:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •