Re: JTable calls setValue
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