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

Recommended Answers

All 2 Replies

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.

commented: Clear explanations +1

Thanks for your explainations. It solves my answer.

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.