/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Gui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
/**
*
* @author K
*/
public class Genres extends JFrame implements ActionListener {
Connection conn = null;
private Statement stmt, s;
private ResultSet result, rst1;
JLabel lblName;
JLabel lblRegistered;
JTextField txtNewName;
JButton btnSave;
JComboBox cboListed;
JButton btnRemove;
JLabel blank1;
JLabel blank2;
//Statement stmt;
Genres() {
super(" Movie Genres ");
{
getContentPane().setBackground(Color.yellow);
}
setLayout(new GridLayout(0, 2));
lblName = new JLabel("Name");
lblRegistered = new JLabel("Registered");
txtNewName = new JTextField(20);
btnSave = new JButton("Save");
cboListed = new JComboBox();
btnRemove = new JButton("Remove");
JLabel blank1 = new JLabel("");
JLabel blank2 = new JLabel("");
add(lblName);
add(txtNewName);
add(blank1);
add(btnSave);
btnSave.addActionListener(this);
btnSave.setActionCommand("save");
add(lblRegistered);
add(cboListed);
cboListed.setSelectedItem(0);
add(blank2);
add(btnRemove);
btnRemove.addActionListener(this);
btnRemove.setActionCommand("Remove");
setcbo();
pack();
show();
}
public void actionPerformed(ActionEvent e) {
String rezolt;
if (e.getActionCommand().equals("save")) {
try {
stmt = Db_Conn.ConnectDb().createStatement();
rezolt = "INSERT INTO Genres ( Genre) VALUES ( '" + txtNewName.getText() + "')";
int result = stmt.executeUpdate(rezolt);
if (result > 0) {
//aJOptionPane.showMessageDialog(null, "Registration details successfully updated", "Success", JOptionPane.INFORMATION_MESSAGE);
setcbo();
txtNewName.setText("");
}//if closed
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Error:" + ex);
}
} else if (e.getActionCommand().equals("Remove")) {
try {
Statement st1 = Db_Conn.ConnectDb().createStatement();
String sql = "Delete from genres where Genre ='" + cboListed.getSelectedItem() + "'";
int result1 = st1.executeUpdate(sql);
if (result1 > 0) {
JOptionPane.showMessageDialog(null, "Payment has been successfully removed", "Success", JOptionPane.INFORMATION_MESSAGE);
setcbo();
cboListed.setSelectedItem("");
}
} catch (SQLException sqlex) {
sqlex.printStackTrace();
}
dispose();
setcbo();
this.show();
}
}
private void setcbo() {
try {
ResultSet rst = Db_Conn.ConnectDb().createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE).executeQuery("SELECT * FROM genres");
while (rst.next()) {
cboListed.addItem(rst.getString(2));
}
} catch (Exception n) {
n.printStackTrace();
}
}
public static void main(String[] args) {
Genres interface1 = new Genres();
//Movies interface2 = new Movies();
//Customers interface3 = new Customers();
//Rentals interface4 = new Rentals();
}
}
hello guys, i need help with my java interface. it dooes save information to the database quite fine. this aved information is then used to populate a COMBOBOX from which i should be able to click on an item and remove it from the database and hence the combobox. the problem is it removes this info from the combobox just well but doesn't refresh the combobox in realtime and i have to run the application once more before i can see that my change has been effected. please help... thanks!