KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > javax > swing > table > DefaultTableModel

javax.swing.table
Class DefaultTableModel

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by javax.swing.table.DefaultTableModel
All Implemented Interfaces:
Serializable, TableModel
See Also:
Top Examples, Source Code, XMLEncoder, getDataVector()

public void addColumn(Object columnName)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void addColumn(Object columnName,
                      Object[] columnData)
See Also:
addColumn(Object, Vector)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void addColumn(Object columnName,
                      Vector columnData)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void addRow(Object[] rowData)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void addRow(Vector rowData)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected Vector columnIdentifiers
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected static Vector convertToVector(Object[] anArray)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected static Vector convertToVector(Object[][] anArray)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected Vector dataVector
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public DefaultTableModel()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public DefaultTableModel(int rowCount,
                         int columnCount)
See Also:
setValueAt(java.lang.Object, int, int)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public DefaultTableModel(Object[] columnNames,
                         int rowCount)
See Also:
setValueAt(java.lang.Object, int, int), setDataVector(java.util.Vector, java.util.Vector)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public DefaultTableModel(Object[][] data,
                         Object[] columnNames)
See Also:
setDataVector(java.util.Vector, java.util.Vector), getDataVector()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public DefaultTableModel(Vector columnNames,
                         int rowCount)
See Also:
setValueAt(java.lang.Object, int, int), setDataVector(java.util.Vector, java.util.Vector)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public DefaultTableModel(Vector data,
                         Vector columnNames)
See Also:
setDataVector(java.util.Vector, java.util.Vector), getDataVector()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int getColumnCount()
See Also:
TableModel.getRowCount()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String getColumnName(int column)
See Also:
AbstractTableModel, TableModel
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Vector getDataVector()
See Also:
setDataVector(java.util.Vector, java.util.Vector), newRowsAdded(javax.swing.event.TableModelEvent), newDataAvailable(javax.swing.event.TableModelEvent)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int getRowCount()
See Also:
TableModel.getColumnCount()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Object getValueAt(int row,
                         int column)
See Also:
ArrayIndexOutOfBoundsException, TableModel
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void insertRow(int row,
                      Object[] rowData)
See Also:
ArrayIndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void insertRow(int row,
                      Vector rowData)
See Also:
ArrayIndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean isCellEditable(int row,
                              int column)
See Also:
setValueAt(java.lang.Object, int, int), AbstractTableModel, TableModel
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void moveRow(int start,
                    int end,
                    int to)
See Also:
ArrayIndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[470]A jframe with a table and two buttons to move rows up and down
By Ranjith on 2004/04/15 18:49:20  Rate
//Main class ( a jframe, that has a table )  
 //and two buttons to move rows up and down 
 //the actual code is in MoveListener class. 
 import java.awt.BorderLayout; 
 import java.awt.GridLayout; 
  
  
 import javax.swing.JButton; 
 import javax.swing.JFrame; 
 import javax.swing.JPanel; 
 import javax.swing.JTable; 
 import javax.swing.table.DefaultTableModel; 
 /** 
  * @author ranjith 
  */
 
 public class MoveRow extends JFrame  {  
   JTable table = null; 
   public static final String [  ]  names =  { "Artist","Track","In stock","Price" } ; 
   public static Object [  ]  [  ]  data= {  
      { "The Beatles", "No.1", "Out of stock","$3.99" } , 
      { "The Beatles", "Rubber soule", "Available","$3.99" } , 
      { "Eric Clapton", "Cream of clapton", "Available","$3.99" } , 
      { "The doors", "Super hits", "Out of stock","$3.99" } , 
      { "Crosby Still Nash", "Not fool again", "Out of stock","$3.99" } , 
      { "Pink floyd", "Brick in the wall", "Out of stock","$3.99" }          
      } ; 
      
   JButton moveUpButton = new JButton ( "Move Up" ) ; 
   JButton moveDnButton = new JButton ( "Move Down" ) ; 
    
   /** 
    * constructor.  
    * Draws controls on the frame 
    */
 
   public MoveRow (  )  {  
     getContentPane (  ) .setLayout ( new BorderLayout (  )  ) ; 
     getContentPane (  ) .add ( getTable (  ) , BorderLayout.CENTER ) ; 
     getContentPane (  ) .add ( getButtonPanel (  ) , BorderLayout.EAST ) ; 
     setSize ( 200,300 ) ; 
     pack (  ) ; 
     setVisible ( true ) ; 
    }   
    
   /** 
    * Returns the button panel with up and down buttons. 
    * @return panel 
    */
 
   public JPanel getButtonPanel (  )  {  
     JPanel btnPanel = new JPanel (  ) ; 
     btnPanel.setLayout ( new GridLayout ( 0,1 )  ) ; 
     btnPanel.add ( moveUpButton ) ; 
     moveUpButton.addActionListener ( new MoveListener ( table,MoveListener.MOVE_UP )  ) ; 
     btnPanel.add ( moveDnButton ) ; 
     moveDnButton.addActionListener ( new MoveListener ( table, MoveListener.MOVE_DOWN )  ) ; 
     return btnPanel; 
    }  
    
   /** 
    * Returns the table, created using the data [  ]   [  ] . 
    *  
    * @return 
    */
 
   public JTable getTable (  )  {  
     DefaultTableModel dataModel = new DefaultTableModel (  ) ; 
     for  ( int i = 0; i  <  names.length; i++ )   {  
       dataModel.addColumn ( names [ i ]  ) ; 
      }  
     for ( int i =0 ; i  <  6;i++ )  {  
         dataModel.addRow ( data [ i ]  ) ; 
      }  
     table = new JTable ( dataModel ) ; 
     return table; 
    }  
  
  
   public static void main ( String [  ]  args )   {  
     MoveRow mr = new MoveRow (  ) ; 
    }  
  }  
 //////////////////////////////////////////// 
 /// MoveListener.java, the actual move code 
 //////////////////////////////////////////// 
 /* 
  * Created on Oct 23, 2003 
  * 
  * To change the template for this generated file go to 
  * Window>Preferences>Java>Code Generation>Code and Comments 
  */
 
 package trails; 
  
  
 import java.awt.event.ActionEvent; 
 import java.awt.event.ActionListener; 
  
  
 import javax.swing.JTable; 
 import javax.swing.table.DefaultTableModel; 
  
  
 /** 
  * @author ranjith 
  */
 
 public class MoveListener implements ActionListener  {  
   public static final int MOVE_UP = -1; 
   public static final int MOVE_DOWN = 1; 
   /** 
    * @param table 
    */
 
   public MoveListener ( JTable table, int direction )   {  
     myTable = table; 
     this.direction = direction; 
    }  
  
  
   JTable myTable = null; 
   int direction = MOVE_UP; 
    
   /** 
    * The actual move action. based on the direciton, 
    * calculate the destination row and simply call the 
    * moveRow method. then call a method to set the  
    * row as currently selected row. 
    */
 
   public void actionPerformed ( ActionEvent arg0 )   {  
     int selRow = myTable.getSelectedRow (  ) ; 
     //find the destination row. 
     int destRow = selRow + direction; 
     //check if the destination row is within table limits 
     if  ( destRow  > = 0 && destRow  <  myTable.getRowCount (  )  )   {  
       DefaultTableModel dtm =  ( DefaultTableModel )  myTable.getModel (  ) ; 
       dtm.moveRow ( selRow, selRow, destRow ) ; 
       //Move is done, now set selection. 
       myTable.setColumnSelectionAllowed ( false ) ; 
       myTable.setRowSelectionAllowed ( true ) ; 
       myTable.setRowSelectionInterval ( destRow, destRow ) ; 
       //make the change visible in UI. 
       myTable.repaint (  ) ; 
      }  
    }  
  
  
  }  
  
  
 


public void newDataAvailable(TableModelEvent event)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void newRowsAdded(TableModelEvent e)
See Also:
getDataVector()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void removeRow(int row)
See Also:
ArrayIndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void rowsRemoved(TableModelEvent event)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1876]rowsRemoved
By twaik_188 { at } yahoo { dot } com on 2007/04/24 06:43:01  Rate
dont know wat u say 
 


public void setColumnCount(int columnCount)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void setColumnIdentifiers(Object[] newIdentifiers)
See Also:
setNumRows(int)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void setColumnIdentifiers(Vector columnIdentifiers)
See Also:
setNumRows(int)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void setDataVector(Object[][] dataVector,
                          Object[] columnIdentifiers)
See Also:
setDataVector(Vector, Vector)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void setDataVector(Vector dataVector,
                          Vector columnIdentifiers)
See Also:
getDataVector()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void setNumRows(int rowCount)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void setRowCount(int rowCount)
See Also:
setColumnCount(int)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void setValueAt(Object aValue,
                       int row,
                       int column)
See Also:
TableModel.isCellEditable(int, int), TableModel.getValueAt(int, int), ArrayIndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags