Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I would like to remove selected row from JTable with AbstractTableModel using a button.

The code below works with DefaultTableModel:

...
MyTableModel mtb;
...
private String[]....
private Object[][]...
...
JTable table = new JTable(mtb)
JButton delete;
...
 public void actionPerformed(ActionEvent e) {

        if(e.getSource().equals(delete))
         {
                 if(table.getSelectedRow()<0)
                 {
                  JOptionPane.showMessageDialog(this,"Select row");

                 }
                 else
                 {
                     mtb.removeRow(table.getSelectedRow()); 

                 }
         }
     }

but it deosn't work with AbstractTablemodel.

I have a little mess in my code so here is java example from oracle page that can be used:

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
244 views
Welcome To Ask or Share your Answers For Others

1 Answer

For AbstractTableModel, you have to implement your own removeRow() based on your model's internal data structure(s), but you can study the source of DefaultTableModel as a guide on which event(s) to fire. For example,

public void removeRow(int row) {
    // remove a row from your internal data structure
    fireTableRowsDeleted(row, row);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

Just Browsing Browsing

[2] html - How to create even cell spacing within a

548k questions

547k answers

4 comments

86.3k users

...