java - Removing rows from a DefaultTableModel with a RowSorter attached doesn't remove last row -


I am new to Java for some extent, and especially new to the table, and give me a special Trouble is working.

I have a JTable that uses a custom table model which extends the defaultTableModel, and I have attached a TableRowSorter in the table below the sample app, there are two buttons - load rows in a table And remove all the selected rows from the table.

For any reason, if you select the last row in the table with any other line, when you click on the "Remove" button, it will remove all the selected rows except the last line. You can remove any other combination of it and it works fine.

What's more, if you click on the column header to sort the rows in the first (if the sequence of rows can not change), then work correctly if I load it If I add a line to sort the rows in the table clearly, the problem "goes away", but I should know what I'm doing is wrong.

Behavior, click the "Load" button to populate the table, select all the rows in the table, then click on the "Remove" button, it will remove all the rows except the last line.

In the form of a printline call, walking through the loop first reduces the selected row number. This behavior is consistent regardless of how many lines you make in the table, but only if you have selected the last line in the table.

I am using Java version 1.6.0_16. Any thoughts that I am doing wrong?

Thanks,

Which

  import javax.swing *; Import javax.swing.table. *; Import java.awt. *; Import java.awt.event. *; Import java.util.Vector; Import java.util.Arrays; Public category TableTest JFrame {Private JTable widgetTable; Private widgetTable Model widget widgets; Public Stable Zero Main (string [] Args) {Tablet Test Frame = New Tablet Test (); Frame.setSize (600, 400); Frame.setLocationRelativeTo (zero); Frame.setVisible (true); } Public tablet () {this.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE); EventQueue.invokeLater (new Runnabal) {Public Zero Run () {createUI ();}}); } Private Zero createUI () {this.setLayout (new border layout ()); Jebton Load Button = New Payton ("Load"); LoadButton.addActionListener (New Executioner) {Public Zero Action Action (Action Event e) {loadPerformed ();}}); This.add (LoadButton, Border Layout. North); Widget Tables Models = New Widget Tables Models (); Widgettables = new jquery (widget templates modal); WidgetTable.setRowSorter (New TableRowSorter & lt; WidgetTableModel & gt; (WidgetTableModel); This.add (new JScrollPane (widgetTable), BorderLayout.CENTER); Button deleted buttons = new pocket ("remove"); RemoveButton.addActionListener (New Executioner) {Public Zero Action Action (Action Event e) {removePerformed ();}}); This.add (remove button, border layout. SOTH); } Private Zero Loaded on () {widgetTableModel.addRow (New Object [] {"Widget1"}); WidgetTableModel.addRow (new object [] {"widget2"}); WidgetTableModel.addRow (new object [] {"widget3"}); WidgetTableModel.addRow (new object [] {"widget4"}); Widgettable modell.ADRO (new object [] {"widget5"}); } Private Zero removal used () {int selectedRow = WidgetTable.getSelectedRow (); While (selected RAW = 0) {System.out.println ("selectedRowCount =" + WidgetTable.getSelectedRowCount ()); Int Modarro = WidgetTableConterooundExtromold (selected rows); WidgetTableModel.removeRow (modelRow); SelectedRow = widgetTable.getSelectedRow (); }}} Class widgettable model default table model {public widget tag modell () {this.addColumn ("column 1") increases; }}  

Change "while" to "if". Select all the rows and then click on the button for some reason loses its selection in the last line I do not know why.

The detection logic should generally be done from 0 to the last line. In this way you do not have to worry about row index values ​​because the change in the form of the row has been removed. Then you use the getSelectedRows () method and repeat it in the reverse order through the array. However, I have to admit that I have never done such a sorted table so I am not sure whether this will create a problem or not.


Comments

Popular posts from this blog

MySql variables and php -

url rewriting - How to implement the returnurl like SO in PHP? -

Which Python client library should I use for CouchdB? -