my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes Dynamic generation of  row jtable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Dynamic generation of  row jtable " Watch "Dynamic generation of  row jtable " New topic
Author

Dynamic generation of row jtable

bhuwan tiwari
Greenhorn

Joined: Jan 04, 2001
Posts: 19
Hi
I want to generate empty row on prees on ENTER at last row
in jtable.
plz suggest me way.
bhuwan
Steffen Durale
Greenhorn

Joined: May 25, 2001
Posts: 13
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

Joined: May 25, 2001
Posts: 13
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamic generation of row jtable