Hi all,i need help about JTable.I'm using netbeans..I got a table contains PLU,nama,harga,qty,total..I use TableModelListener to listen to valueschanged in column..What i need help is how to create a new row if the user press enter key,and set focus to the first column of the next row..Thanks a lot..Here are the codes :

/*
 * JualHarianFrame.java
 *
 *
 Created on October 24, 2008, 12:58 PM
 */


package han.pos.gui;

import han.pos.dao.mysql.ProdukDAOImpl;

import han.pos.db.mysql.DBConnection;


import han.pos.model.Produk;

import java.awt.event.KeyEvent;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;


import java.util.logging.Level;

import java.util.logging.Logger;

import javax.swing.event.TableModelEvent;

import javax.swing.event.TableModelListener;


import javax.swing.table.DefaultTableModel;

import javax.swing.table.TableModel;


/**
 *
 * @author  yesaya
 */
public 
class JualHarianFrame extends javax.swing.JInternalFrame implements TableModelListener
{
private List<Produk> Produk = new ArrayList<Produk>();    
/** Creates new form JualHarianFrame */    
public JualHarianFrame() 
{        
initComponents();     
tblJual.getModel().addTableModelListener(this);     
lblTotal.setText("0");    
}    
//this code is generated by netbeans 
tblJual.setAutoCreateRowSorter(true);
tblJual.setModel(new javax.swing.table.DefaultTableModel(            
new Object [][] {                
{null, null, null, null, null}            },           
 new String [] {                
"PLU", "NAMA", "HARGA", "QTY", "TOTAL"            
}        ) 
{            Class[] types = new Class [] {                
java.lang.String.class, java.lang.String.class, java.lang.Double.class, java.lang.Integer.class, java.lang.Double.class
};            
public Class getColumnClass(int columnIndex) 
{                
return types [columnIndex];            
}       }
);        
tblJual.setColumnSelectionAllowed(true);
        
tblJual.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
     tblJual.addKeyListener(new java.awt.event.KeyAdapter() 
{            
public void keyPressed(java.awt.event.KeyEvent evt) 
{                
tblJualKeyPressed(evt);            
}        
});        
jScrollPane1.setViewportView(tblJual);                        
public void tableChanged(TableModelEvent e) 
{
Double hrg,total=null;
int qty;    
if (e.getType() == TableModelEvent.UPDATE)       
{            
int row = e.getFirstRow();            
int column = e.getColumn();            
TableModel model = (TableModel)e.getSource();            
if (column==0)
{          
try 
{            
DBConnection conn = DBConnection.getInstance();            
ProdukDAOImpl dao = new ProdukDAOImpl(conn.getCon());            
String plu = model.getValueAt(row, 0).toString();           
 Produk = dao.getPlu(plu);            
for (Produk produk:Produk)
{               
String nama=produk.getNama().toString();              
 Double harga=produk.getHarga_jual();                
model.setValueAt(nama, row, 1);                
model.setValueAt(harga, row, 2);                
tblJual.setColumnSelectionInterval(3, 3);
            
}            
} catch (SQLException ex) 
{           
Logger.getLogger(JualHarianFrame.class.getName()).log(Level.SEVERE, null, ex);      
 }          
}           
else 
     if (column==3){                
qty=Integer.parseInt(model.getValueAt(row, 3).toString());                
hrg=(Double) model.getValueAt(row, 2); total=qty*hrg;                
model.setValueAt(total, row, 4);                
lblTotal.setText(String.valueOf(total));            
}       
}        
}

i don't know it for the time being. Me myself, i am on the way of reading.

Can you teach me how to use the addRow??Thanks a lot...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.