Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #1211 > unrolled thread

JTable calls setValueAt w

Started by"Lionel van den Berg" <lionel.van.den.berg@THRWHITE.remove-dii-this>
First post2011-04-27 15:32 +0000
Last post2011-04-27 15:32 +0000
Articles 3 — 2 participants

Back to article view | Back to comp.lang.java.gui


Contents

  JTable calls setValueAt w "Lionel van den Berg" <lionel.van.den.berg@THRWHITE.remove-dii-this> - 2011-04-27 15:32 +0000
    Re: JTable calls setValue "Michael Rauscher" <michael.rauscher@THRWHITE.remove-dii-this> - 2011-04-27 15:32 +0000
      Re: JTable calls setValue "Lionel van den Berg" <lionel.van.den.berg@THRWHITE.remove-dii-this> - 2011-04-27 15:32 +0000

#1211 — JTable calls setValueAt w

From"Lionel van den Berg" <lionel.van.den.berg@THRWHITE.remove-dii-this>
Date2011-04-27 15:32 +0000
SubjectJTable calls setValueAt w
Message-ID<45fd081e$1@dnews.tpgi.com.au>
  To: comp.lang.java.gui
I'll start with minimal information in case this has a simple solution

I've got a JTable, table, and I've sub-classed AbstractTableModel and 
set the JTable model to an instance of my sub-class. My model just has 
an ArrayList, dataList, that holds the data types that represent each 
row. My getRowCount() method in my model simply returns dataList.size().

I've got a delete button that calls: 
((AbstractTableModel)table.getModel()).deleteRow(table.getSelectedRow());

The deleteRow(int row) method simply does:
dataList.remove(row);
fireTableRowsDeleted(row, row);

If I have a table with two entries, select the last entry and press 
delete once it deletes the entry and there are no rows selected after it 
is removed. If I press delete again my application catches this saying 
that there is nothing selected (getSelectedRow() returns -1) but after 
JTable seems to call model.setValueAt(...) with a row index of 1 which 
is out of bounds. Of course this causes an IndexOutOfBoundsException 
when it happens.

Why is this method being called with this illegal value? The only 
solution I've found is to call:

table.setEditingRow(Table.getRowCount() -1);

But I don't like this, it seems like a hack.

Any ideas?

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#1252 — Re: JTable calls setValue

From"Michael Rauscher" <michael.rauscher@THRWHITE.remove-dii-this>
Date2011-04-27 15:32 +0000
SubjectRe: JTable calls setValue
Message-ID<etr72h$22r$1@registered.motzarella.org>
In reply to#1211
  To: comp.lang.java.gui
Lionel van den Berg schrieb:
> I'll start with minimal information in case this has a simple solution
> 

It sure has a simple solution but it seems that you didn't provide the 
necessary information to find it :)

I've created an SSCCE for you (without the phenomenon you mentioned):

import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class Test {

     static class StringListTableModel extends AbstractTableModel {
         private List<String> data;

         public StringListTableModel( List<String> data ) {
             this.data = data;
         }

         public int getColumnCount() { return 1; }
         public int getRowCount() { return data.size(); }
         public Object getValueAt( int row, int col ) {
             return data.get(row);
         }
         public boolean isCellEditable( int row, int col ) {
             return true;
         }
         public void setValueAt( Object value, int row, int col ) {
             data.set( row, (String)value );
             fireTableCellUpdated( row, col );
         }
         public void deleteRow( int row ) {
             data.remove( row );
             fireTableRowsDeleted( row, row );
         }
     }


     public static void main( String args[] ) throws Exception {
         final StringListTableModel model = new StringListTableModel(
                 new ArrayList<String>(
                 Arrays.asList("First", "Second", "Third")) );
         final JTable table = new JTable(model);

         JButton button = new JButton("Delete");
         button.addActionListener( new ActionListener() {
             public void actionPerformed( ActionEvent e ) {
                 int row = table.getSelectedRow();
                 if ( row != -1 )
                     model.deleteRow( row );
                 else
                     JOptionPane.showMessageDialog( null,
                             "No row selected." );
             }
         });

         JFrame frame = new JFrame("Test");
         frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
         frame.add( new JScrollPane(table) );
         frame.add( button, java.awt.BorderLayout.SOUTH );
         frame.pack();
         frame.setVisible( true );
     }

}

Bye
Michael

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1271 — Re: JTable calls setValue

From"Lionel van den Berg" <lionel.van.den.berg@THRWHITE.remove-dii-this>
Date2011-04-27 15:32 +0000
SubjectRe: JTable calls setValue
Message-ID<46022f0d@dnews.tpgi.com.au>
In reply to#1252
  To: comp.lang.java.gui
Michael Rauscher wrote:
> Lionel van den Berg schrieb:
>> I'll start with minimal information in case this has a simple solution
>>
> 
> It sure has a simple solution but it seems that you didn't provide the 
> necessary information to find it :)
> 
> I've created an SSCCE for you (without the phenomenon you mentioned):

Thanks. I'm going to have to look into this because at first glance the 
only difference is the line:
fireTableCellUpdated( row, col );

I use fireTableDataChanged();

I'll try your test and see what happens.

Lionel.

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web