我希望我的JTable在添加或删除表中的数据时自动更新,但无法刷新: 你能帮我找到错误吗?
package gui;
public class Cek extends JFrame implements ActionListener, ListSelectionListener
{
public static ConnectionManager conn = new ConnectionManager();
private String driver = "com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://localhost/db_uas";
private String username = "root";
private String password = "shasa";
DefaultTableModel tableModel = new DefaultTableModel();
private JTabbedPane tabPane;
private JLabel label1;
private JPanel panel1,panel2,panel3;
private JTable table = new JTable();
private String judulKolom[] = {"Mata Uang" , "Harga"};
private DefaultTableModel tbm;
private int nmax = 50;
int harga_kamar;
ListSelectionModel listMod;
int row=0;
Object data[][] = new Object[nmax][2];
JLabel lblMataUang = new JLabel("Mata Uang :");
JTextField jtfMataUang = new JTextField();
JLabel lblEditHarga = new JLabel("Edit Harga :");
JTextField jtfEditHarga = new JTextField();
JButton jbEdit = new JButton("Update");
JLabel lblAddMataUang = new JLabel("Mata Uang Baru :");
JLabel lblAddHarga = new JLabel("Nilai Tukar : Rp.");
JTextField jtfAddMataUang = new JTextField();
JTextField jtfAddHarga = new JTextField();
JButton btnAdd = new JButton("Add");
JButton btnPaiipaii = new JButton("Exittt");
JButton btnDelete = new JButton("Delete");
String ket;
ArrayList<String> shasa1 = new ArrayList<>();
JLabel lblDeleteMU = new JLabel("Delete Mata Uang : ");
public Cek(){
super("List Data");
setSize(500, 500);
setResizable(false);
setLocationRelativeTo (null);
setVisible (true);
tabPane = new JTabbedPane ();
label1 = new JLabel ("Panel pertama", SwingConstants.CENTER);
jbEdit.addActionListener(this);
btnAdd.addActionListener(this);
btnPaiipaii.addActionListener(this);
btnDelete.addActionListener(this);
for(int i=0;i<data.length;i++){
data[i][0] = new Object();
data[i][1] = new Object();
}
table = new JTable();
tbm = new DefaultTableModel(new Object[][]{}, judulKolom);
table.setModel(tbm);
table.setBounds(5, 5, 300, 300);
tampil();
lblMataUang.setBounds(330, 10, 200, 50);
jtfMataUang.setBounds(330, 50, 150, 20);
lblEditHarga.setBounds(330, 60, 200, 50);
jtfEditHarga.setBounds(330, 100, 150, 20);
jbEdit.setBounds(330, 130, 80, 30);
btnPaiipaii.setBounds(330, 180, 100, 20);
jtfMataUang.setEditable(false);
listMod = table.getSelectionModel();
listMod.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listMod.addListSelectionListener(this);
panel1 = new JPanel ();
panel1.setLayout(null);
panel1.add(table);
panel1.add(lblMataUang);
panel1.add(jtfMataUang);
panel1.add(lblEditHarga);
panel1.add(jtfEditHarga);
panel1.add(jbEdit);
panel1.add(btnPaiipaii);
panel2 = new JPanel ();
panel2.setLayout(null);
lblAddMataUang.setBounds(20, 20, 100, 30);
jtfAddMataUang.setBounds(150, 20, 150, 30);
lblAddHarga.setBounds(20, 100, 100, 30);
jtfAddHarga.setBounds(150, 100, 150, 30);
btnAdd.setBounds(100, 300, 100, 30);
panel2.add(lblAddMataUang);
panel2.add(jtfAddMataUang);
panel2.add(lblAddHarga);
panel2.add(jtfAddHarga);
panel2.add(btnAdd);
panel3 = new JPanel();
panel3.setLayout(null);
showComboBox();
String daftarMataUang[] = shasa1.toArray(new String[shasa1.size()]);
JComboBox mataUang = new JComboBox(daftarMataUang);
mataUang.addActionListener(this);
mataUang.setBounds(20, 20, 300, 30);
lblDeleteMU.setBounds(20, 50, 250, 30);
btnDelete.setBounds(150, 250, 100, 30);
panel3.add(mataUang);
panel3.add(lblDeleteMU);
panel3.add(btnDelete);
tabPane.addTab ("Update/Perubahan", null, panel1);
tabPane.addTab("Add", null,panel2);
tabPane.addTab("Delete", null,panel3);
getContentPane().add(tabPane);
}
public void resetTable()
{
for (int i = 0; i < table.getRowCount(); i++) {
for (int j = 0; j < table.getColumnCount(); j++) {
table.setValueAt(null, i, j);
}
}
refreshTable();
}
public void showComboBox()
{
conn.connect();
String query = "select mata_uang from nilai_tukar";
try
{
Statement stmt = conn.con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while(rs.next())
{
shasa1.add(rs.getString("mata_uang"));
}
}
catch(Exception e1)
{
e1.printStackTrace();
}
conn.disconnect();
}
public void tampil()
{
try
{
int row = tbm.getRowCount();
tbm.addRow(judulKolom);
ConnectionManager conn = new ConnectionManager();
conn.connect();
Statement stmt = conn.con.createStatement();
ResultSet rs = stmt.executeQuery("select * from nilai_tukar");
int No=0;
while(rs.next())
{
No++;
String no_kamar = rs.getString("mata_uang");
double harga = rs.getDouble("harga");
data[No][0] = no_kamar;
data[No][1] = harga;
tbm.addRow(data[No]);
row++;
}
rs.close();
conn.disconnect();
}
catch(Exception exc)
{
exc.printStackTrace();
}
}
public void refreshTable(){
table.setValueAt("Mata Uang", row, 0);
table.setValueAt("Harga", row, 1);
row++;
try {
Class.forName(driver);
Connection conns = DriverManager.getConnection(url, username, password);
PreparedStatement query = (PreparedStatement) conns.prepareStatement("select * from nilai_tukar");
ResultSet rs = query.executeQuery();
while (rs.next()) {
table.setValueAt(rs.getString("mata_uang"), row, 0);
table.setValueAt(rs.getDouble("harga"), row, 1);
row++;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jbEdit)
{
String mu ;
double temp =0;
mu = jtfMataUang.getText();
temp = Double.parseDouble(jtfEditHarga.getText());
try
{
ConnectionManager conn = new ConnectionManager();
conn.connect();
String query ="update nilai_tukar set harga="+temp+" where mata_uang='"+mu+"'";
Statement stmt1 = conn.con.createStatement();
int updateField = stmt1.executeUpdate(query);
JOptionPane.showMessageDialog(null, "Data Anda Terupdate !!");
resetTable();
}
catch (Exception e2)
{
e2.printStackTrace();
}
}
if(e.getSource()==btnAdd)
{
ConnectionManager conn = new ConnectionManager();
conn.connect();
if(!jtfAddHarga.getText().equals("") && !jtfAddMataUang.getText().equals(""))
{
double h = Double.parseDouble(jtfAddHarga.getText()) ;
String mu = jtfAddMataUang.getText();
String query = "insert into nilai_tukar values(?,?)";
try
{
PreparedStatement stmt = (PreparedStatement) conn.con.prepareStatement(query);
stmt.setString(1, mu);
stmt.setDouble(2, h);
stmt.executeUpdate();
JOptionPane.showMessageDialog(null, "New Data Added Succecfully");
table.repaint();
}
catch(Exception e1){
e1.printStackTrace();
}
conn.disconnect();
}
else
{
JOptionPane.showMessageDialog(null, "Data Harus Diisi");
}
}
if(e.getSource() == btnPaiipaii)
{
dispose();
new GUI();
}
try
{
JComboBox<String> test = (JComboBox)e.getSource();
ket = (String) test.getSelectedItem();
lblDeleteMU.setText("Delete Mata Uang : " + ket);
}
catch (Exception e2)
{
}
if(e.getSource() == btnDelete)
{
ConnectionManager conn = new ConnectionManager();
conn.connect();
System.out.println(ket);
String SQL = "DELETE FROM nilai_tukar WHERE mata_uang = '"+ket+"'";
try
{
PreparedStatement stmt = (PreparedStatement) conn.con.prepareStatement(SQL);
stmt.executeUpdate();
System.out.println(ket);
JOptionPane.showMessageDialog(null, "Data Deleted Succesfully");
conn.connect();
String query2 = "select mata_uang from nilai_tukar";
try
{
Statement stmt2 = conn.con.createStatement();
ResultSet rs2 = stmt2.executeQuery(query2);
while(rs2.next())
{
System.out.println(rs2.getString("mata_uang"));
shasa1.add(rs2.getString("mata_uang"));
}
}
catch(Exception e1)
{
e1.printStackTrace();
}
conn.disconnect();
showComboBox();
String daftarMataUang[] = shasa1.toArray(new String[shasa1.size()]);
JComboBox mataUang = new JComboBox(daftarMataUang);
mataUang.addActionListener(this);
mataUang.setBounds(20, 20, 300, 30);
panel3.add(mataUang);
}
catch(Exception e1){
e1.printStackTrace();
}
conn.disconnect();
}
}
@Override
public void valueChanged(ListSelectionEvent e) {
int maxRows;
int[] selRows;
Object value;
if (!e.getValueIsAdjusting()) {
selRows = table.getSelectedRows();
if (selRows.length > 0) {
jtfMataUang.setText((String) data[table.getSelectedRow()][0]);
jtfEditHarga.setText(data[table.getSelectedRow()][1].toString());
}
}
}
public static void main(String[] args) {
new Cek();
}
}