Hi I want my JTable to implement TableModelListener, so it can react if the data of the TableModel changed. However as soon as I implement the TableModelListener in my JTable subclass, the Table is not displayed in the frame anymore.
If I create a subclass of JTable everything works well and the Table is shown in the frame:
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class JTableDisappears extends JFrame {
private DefaultTableModel watchlistTableModel;
private WatchlisttTable watchlistTable;
public JTableDisappears() {
super();
setSize(200,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
createTable();
setVisible(true);
}
private void createTable() {
String[] zeilenNamen = { "Name", "Symbol", "Price", "%" };
String[][] data = { { "1", "AAPL", "3", "4" }, { "A", "2", "C", "D" }, { "2", "B", "3", "4" } };
watchlistTableModel = new DefaultTableModel(data, zeilenNamen);
watchlistTableModel.setColumnIdentifiers(zeilenNamen);
watchlistTable = new WatchlisttTable(watchlistTableModel);
add(new JScrollPane(watchlistTable));
}
public static void main(String[] args) {
new JTableDisappears();
}
}
class WatchlisttTable extends JTable {
public WatchlisttTable(TableModel tableModel) {
super(tableModel);
}
}
But if I let my JTable subclass implement a TableModelListener like this:
class WatchlisttTable extends JTable implements TableModelListener{
public WatchlisttTable(TableModel tableModel) {
super(tableModel);
}
@Override
public void tableChanged(TableModelEvent e) {
}
}
the Table is not shown in the frame anymore. Why? And what can I do to solve this issue?