Básicamente no funciona por que el modelo lo estas recién instanciando es normal que no tenga nada.
Te dejo un ejemplo completamente funcional:
Código Java:
Ver originalimport java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
private static final long serialVersionUID = 1L;
private boolean DEBUG = false;
private String[] columnNames
= {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"}; private Object[][] data
= {{"Kathy",
"Smith",
"Snowboarding",
new Integer(5),
(false)},
{"John",
"Doe",
"Rowing",
new Integer(3),
(true)},
{"Sue",
"Black",
"Knitting",
new Integer(2),
(false)},
{"Jane",
"White",
"Speed reading",
new Integer(20),
(true)},
{"Joe",
"Brown",
"Pool",
new Integer(10),
(false)}};
private static final long serialVersionUID = 1L;
@Override
public Class<?> getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
@Override
int total = 0;
for (int row = 0; row < model.getRowCount(); row++) {
total
= total
+ (Integer)model.
getValueAt(row,
3); }
System.
out.
print(total
+ "\n"); }
});
return okButton;
}
public Utils() {
model.addTableModelListener(this);
table.
setPreferredScrollableViewportSize(new Dimension(500,
70)); table.setFillsViewportHeight(true);
add(scrollPane);
add(getOkButton());
}
int row = e.getFirstRow();
int column = e.getColumn();
String columnName
= model.
getColumnName(column
); Object datas
= model.
getValueAt(row, column
); System.
out.
println("Column ---> " + columnName
+ ", new value is " + datas
); }
private static void createAndShowGUI() {
frame.
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE); Utils newContentPane = new Utils();
newContentPane.setOpaque(true); // content panes must be opaque
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main
(String[] args
) {
public void run() {
createAndShowGUI();
}
});
}
}