3

I import a CSV file into a DefaultTableModel, one column is formatted as double, so far so good. But if I edit a cell from this column (double) in the JTable, after that this cell is no longer a double. Now it is a string. How can I change the type of the edited cell in the TableModel? I know that I can parse a string to double with double value = Double.parseDouble(str);, but how can I ensure that this happens after editing a cell?

Do I need a new TableModel-class like:

class myTableModel extends DefaultTableModel { }

Thanks for your help.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
saduino
  • 65
  • 1
  • 5

1 Answers1

4

for example

    @Override
    public Class<?> getColumnClass(int c) {
        if (c == 1) {
            return Short.class;
        } else {
            return Integer.class;
        }
    }

or

import javax.swing.*;
import javax.swing.table.*;

public class RemoveAddRows extends JFrame {

    private static final long serialVersionUID = 1L;
    private Object[] columnNames = {"Type", "Company", "Shares", "Price"};
    private Object[][] data = {
        {"Buy", "IBM", new Integer(1000), new Double(80.50)},
        {"Sell", "MicroSoft", new Integer(2000), new Double(6.25)},
        {"Sell", "Apple", new Integer(3000), new Double(7.35)},
        {"Buy", "Nortel", new Integer(4000), new Double(20.00)}
    };
    private JTable table;
    private DefaultTableModel model;
    private javax.swing.Timer timer = null;

    public RemoveAddRows() {
        model = new DefaultTableModel(data, columnNames) {

            private static final long serialVersionUID = 1L;

            @Override
            public Class getColumnClass(int column) {
                return getValueAt(0, column).getClass();
            }
        };
        table = new JTable(model);        
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane);        
    }



    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                RemoveAddRows frame = new RemoveAddRows();
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • gentle reminder: code in an answer should be a SSCCE - all the unrelated decorum in yours will only tend to confuse a newbie ;-) – kleopatra Nov 28 '12 at 13:46
  • 1
    @mKorbel: Also consider static factories or boxed constants, for [example](http://stackoverflow.com/a/13542145/230513). – trashgod Nov 28 '12 at 15:00
  • 2
    @kleopatra: gentle suggestion: _decorum_ is tasteful behavior, while _decoration_ is adornment or embellishment. – trashgod Nov 28 '12 at 15:06
  • @trashgod thanks for the coaching :-) looks like I shouldn't post _anything_ without consulting my [favourite dictory](http://dict.leo.org/ende?lp=ende&lang=de&searchLoc=0&cmpType=relaxed&sectHdr=on&spellToler=&search=decorum) – kleopatra Nov 28 '12 at 15:18