1,105,371 Community Members

add row in jTable use Netbeans.

Member Avatar
Kusno
Junior Poster
191 posts since Aug 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 17 [?]
Skill Endorsements: 0 [?]
 
0
 

Dear all,
I'm sorry to ask you again.

This code is my jTable that I copy from my Netbeans source code.

tblGroupAnggaran.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null},
{null, null}
},
new String [] {
"Kode group", "Nama group"
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.String.class
};
boolean[] canEdit = new boolean [] {
false, false
};


public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}


public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
tblGroupAnggaran.setColumnSelectionAllowed(true);
jScrollPane1.setViewportView(tblGroupAnggaran);
tblGroupAnggaran.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);


I have tried this code :


void isiTable(String kataKunci)
{
String SQL = "";
SQL = "SELECT * FROM mastergroupanggaran ";
if(!kataKunci.equals(""))
{
if(cmbSearch.getSelectedIndex()==0)
{
SQL += " WHERE kodegroupanggaran LIKE '%" + kataKunci + "%'";
}
else
{
SQL += " WHERE namagroupanggaran LIKE '%" + kataKunci + "%'";
}
}
SQL += " ORDER BY kodegroupanggaran";
deleteTableContents();
try{
Koneksi getCn = new Koneksi();
Connection cn = getCn.getConnection();
Statement st = cn.createStatement();
ResultSet rs = st.executeQuery(SQL);
while(rs.next())
{
String Kode = rs.getString(1);
String Nama = rs.getString(2);
String[] data = {"",Kode,Nama}; tblGroupAnggaran.addRow(data);          
}
}
catch(SQLException e)
{


}
}

But Netbeans does not recogizne that statement (in bold format), whereas if we see in Java manual book, it works.

Thanks for your advance,

Kusno

Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
1
 

The row must be added to the TableModel, not the JTable as you are attempting. In particular, that method is provided by DefaultTableModel and is not in the general TableModel interface, so you will either need to keep a reference to the DefaultTableModel that you are creating

DefaultTableModel tableModel = new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null},
{null, null}
},
new String [] {
"Kode group", "Nama group"
});  // plus all of your other extension code of course
tblGroupAnggaran.setModel(tableModel);

(though that reference will actually need to be an instance-level reference, not local as in this example) or get the model from the table and cast it to DefaultTableModel

((DefaultTableModel)tblGroupAnggaran.getModel()).addRow(data);

I would also recommend making your DefaultTableModel extension it's own separate inner class. It would be much easier to read and maintain over the anonymous inner class you are currently using.

Member Avatar
Kusno
Junior Poster
191 posts since Aug 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 17 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks for your explainations. It solves my answer.

Question Answered as of 6 Years Ago by Ezzaral
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article