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


Groups > comp.lang.java.gui > #1252

Re: JTable calls setValue

From "Michael Rauscher" <michael.rauscher@THRWHITE.remove-dii-this>
Subject Re: JTable calls setValue
Message-ID <etr72h$22r$1@registered.motzarella.org> (permalink)
Newsgroups comp.lang.java.gui
References <45fd081e$1@dnews.tpgi.com.au>
Date 2011-04-27 15:32 +0000
Organization TDS.net

Show all headers | View raw


  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

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web