Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamic generation of row jtable

 
bhuwan tiwari
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I want to generate empty row on prees on ENTER at last row
in jtable.
plz suggest me way.
bhuwan
 
Steffen Durale
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you are using an abstract table model like this:
class myDataModel extends AbstractTableModel
{
protected String[] columnNames = new String[] { "col1", "col2" };
Object[][] myData;
int[] editAble;
final int editColumn = 1;
myDataModel( Object[][] myData )
{
this.myData = myData;
}

public int getColumnCount()
{
if( columnNames != null )
return columnNames.length;
else
return -1;
}
public int getRowCount()
{
if( myData != null )
return myData.length;
else
return -1;
}
public String getColumnName( int col )
{
return columnNames[ col ];
}
public boolean isCellEditable( int row, int column )
{
return true;
}

public Object getValueAt( int row, int col )
{
return myData[ row ][ col ];
}
public void setValueAt( Object value, int row, int col )
{
myData[ row ][ col ] = value;
fireTableCellUpdated( row, col );
}
}

you have to implement the following method:
public void addRow(Vector row) {
dataVector.addElement(row);
fireTableDataChanged();
}
call this method every time when you want to add the row
the table model checks your dataVector and should add the row by its own..
regards
Steffen
 
Steffen Durale
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and of course you have to implement a keyListener to your jTable like this:
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class TableKeyListener implements KeyListener {

public void keyTyped(KeyEvent event) {
}

public void keyReleased(KeyEvent event) {
}

public void keyPressed(KeyEvent event) {
//returnkey pressed?
if(event.getKeyCode() == 107) {
yourTableModel.addRow(this.row);
}
}
public Vector rowToAdd() {
Vector defaultRow = new Vector();
defaultRow.add("column1");
defaultRow.add("column1");
return defaultRow;
}
}

i think it should work like this

Steffen
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic