java - jtable checkbox checked row remove from table when click button? -


The code below is a part of my button action. When I click the Save button, the selected row will be deleted from the table row by

Action Display Code

  @Override ... !!! 'ActionPerformed (ActionEvent e) {Zero if (e.getSource () == btnSave) {(I int = 0; I & lt; retunTable.getRowCount (); i ++) {Boolean chked = Boolean.valueOf ( RetunTable.getValueAt (i, 4) .toString ()); String Datacol 1 = Rettabletable.get Value (i, 1) .toString (); If (chked) {JOptionPane.showMessageDialog (empty, datacol 1); ColVaules.add (dataCol1); Returnbook (); Default table model DM = (default table model) retunTable.getModel (); }}}}}  

Try it out if you already have class getColumnClass ( ) of the model, then toString () There are no need for your trying to do what should be overridden. getValueAt () should return an artist-worthy boolean object, in addition to which if you are going to loop and are dynamically removing rows in the loop , You need to keep in mind that the line number of the model will be reduced with the removal of each of the lines, even if it will need to be i - each row is removed See examples below.

Import java.awt.BorderLayout; Import java.awt.event.ActionEvent; Import java.awt.event.ActionListener; Import javax.swing.JButton; Import javax.swing.JFrame; Import javax.swing.JScrollPane; Import javax.swing.JTable; Import javax.swing.SwingUtilities; Import javax.swing.table.DefaultTableModel; Public Square TestCheckedTable {Public TestCheckedTable} {string [] cols = {"col 1", "col 2", "col 3"}; Object [] [] data = new object [15] []; {Data [i] = new object [] {"hello", "world", incorrect} for (int i = 0; i & lt; data.lamp; i ++); } Ultimate DefaultTel Model Model = New DefaultTel Model (Data, Column) {@ Override Public Class & lt ;? & Gt; GetColumnClass {return col == 2? Boolean Category: String Class; }}; JTable table = new JTable (model); Pocket button = new pocket ("delete checked row"); Button.addActionListener (new ActionListener) {public zero-actionPerformed (ActionEvent E) {(int i = 0; I & lt; model.getRowCount (); i ++) {Boolean Check = (Boolean) model.getValueAt (i, 2); if (checked) {model.removeRow (i); i--;}}}}); GeFame Frame = New Geoffram ("Test"); Frame.add (new JScrollPane (table)); Frame Add (Button, Border Layout. SOTH); Frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Frame.pack (); Frame.setVisible (true); } Public static void (String [] args) {SwingUtilities.invokeLater (New Runnable) {Public Zero Run (New TestCheckedTable ();}}); }}

Comments