Title: RE: Add new lines to the JTable

Hi,

first of all, you need to call fireTableDataChanged() from _within_ your TableModel incrementRow() method:

class MyTableModel extends AbstractTableModel {
  //other methods here
  ....

  void incrementRow() { //notice the lowercase i, this is convention in java!
        System.out.println("IncrementRow()");
        nRows++;
        fireTableDataChanged();
    }  
}

I think the setValueAt() of the model is only called when pressing ENTER, so you confirm the change you did.

regards,

Wim

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
Sent: woensdag 4 februari 2004 16:30
To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
Subject: RE: Add new lines to the JTable


Thanks for this answer, I look in the table and I never remember to go to
the TableModel.
 
Onether question. Know I have the Table with the cells editable.
If I chose another column the setValueAt is called.
If I press the button the setValueAt is not called.
If I go to the TextArea the setValueAt is not called.
if I close the Frame the setValueAt is not called.
This isn't suposed?
 
Simple code. I'm seeing just the output.
 
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
 
class MyTableModel extends AbstractTableModel {
    int nRows = 5;
    public int getRowCount() {
        return nRows;
    }
 
    public int getColumnCount() {
        return 4;
    }
               
    public Object getValueAt(int arg0, int arg1) {
        return new Integer(arg0*arg1);
    }
   
    void IncrementRow() {
        System.out.println("IncrementRow()");
        nRows++;
    }
   
    public boolean isCellEditable(int nRow, int nCol) {
        System.out.println("isCellEditable(int nRow, int nCol)");
        return true;
    }
 
    public void setValueAt(Object arg0, int arg1, int arg2) {
        System.out.println("setValueAt(Object arg0, int arg1, int arg2)");
    }
}
 
public class Main {
    static JFrame m_frame;
    public static void main(String[] ARGS) {       
        m_frame = new JFrame("Teste");
        m_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        m_frame.getContentPane().setLayout(new BorderLayout());
           
        final JTable table = new JTable();
       
        final MyTableModel myTableModel = new MyTableModel();
        table.setModel(myTableModel);
 
        final JScrollPane scrollPane = new JScrollPane(table);
           
        m_frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
 
        JButton bt = new JButton("XXX");
        bt.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
                myTableModel.IncrementRow();
                myTableModel.fireTableDataChanged();
            }
        });
 
        m_frame.getContentPane().add(bt, BorderLayout.NORTH);
       
        m_frame.getContentPane().add(new JTextField(), BorderLayout.SOUTH);
           
        m_frame.pack();
        m_frame.setVisible(true);
    }
}
 
- - - - - - - - DISCLAIMER - - - - - - - -
Unless indicated otherwise, the information contained in this message is privileged and confidential, and is intended only for the use of the addressee(s) named above and others who have been specifically authorized to receive it. If you are not the intended recipient, you are hereby notified that any dissemination, distribution or copying of this message and/or attachments is strictly prohibited. The company accepts no liability for any damage caused by any virus transmitted by this email. Furthermore, the company does not warrant a proper and complete transmission of this information, nor does it accept liability for any delays. If you have received this message in error, please contact the sender and delete the message. Thank you.

Reply via email to