/*
 * 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!

Edited 4 Years Ago by ratatat

a few remarks:
1. a gui and an interface are not the same, you may want to choose your words a bit more clear
2. "it removes this info from the combobox just well but doesn't refresh the combobox in realtime" .... sooo .. it doesn't remove the item? can you be a bit more specific about what is going on here? I assume re-instantiating the combobox would do the trick.
3. help you with what exactly? you've given us a vague description of what is going on (and wrong) but you don't show us any code, so how can we be completely sure about what you are doing (wrong)?

No time to read the whole code now, but iut seems setcbo is called to update the cb after the delete, but in setcbo I can't see where you delete the original contents - mabe your updated stuff is just being added on the end?

This article has been dead for over six months. Start a new discussion instead.