I have a SQL database and I am working on a program that will allow me to add/delete/modify records. I already managed to add records I am working on editing/deleting them.
I want to display the existing records in a table so I am using jTable. I found some code online and modified it to pull the records and display them in a jtable but i dont know how to code the rightclick and display a popup menu.
In that popup menu I want to display options such as delete record and modify record.
This is the code I am using the make the jTable and display the data:
private void menuDeleteAuthorActionPerformed(java.awt.event.ActionEvent evt) {
TableFromDatabase deleteAuthor = new TableFromDatabase();
deleteAuthor.pack();
deleteAuthor.setVisible(true);
Vector columnNames = new Vector();
Vector data = new Vector();
try
{
Connection connection = DriverManager.getConnection( url, user, password );
// Read data from a table
String sql = "SELECT * FROM Authors";
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery( sql );
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
// Get column names
for (int i = 1; i <= columns; i++)
{
columnNames.addElement( md.getColumnName(i) );
}
// Get row data
while (rs.next())
{
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++)
{
row.addElement( rs.getObject(i) );
}
data.addElement( row );
}
rs.close();
stmt.close();
connection.close();
}
catch(Exception e)
{
System.out.println( e );
}
// Create table with database data
JTable table = new JTable(data, columnNames)
{
public Class getColumnClass(int column)
{
for (int row = 0; row < getRowCount(); row++)
{
Object o = getValueAt(row, column);
if (o != null)
{
return o.getClass();
}
}
return Object.class;
}
};
JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );
JPanel buttonPanel = new JPanel();
getContentPane().add( buttonPanel, BorderLayout.SOUTH );
}
I am new to Java so please be kind in your responses. Thank you all in advance for any assistance!