1

I have a JComboBox that is automatically set with a method that grabs all Id's from my Database on start up, I also have a delete function, that will delete the selected object from the database, but it doesn't delete the id from the list. Is there any way I can update the JComboBox when the delete method is executed? My code is below if that is any help.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import java.sql.*;

import java.util.ArrayList;

public class EditMember extends JFrame {

    String[] positions = {"", "Trainee", "Writer", "Moderator", "Administrator"};

    JComboBox _id = new JComboBox(getId());
    JComboBox _position = new JComboBox(positions);

    JTextField _name = new JTextField(10);
    JTextField _username = new JTextField(10);
    JPasswordField _password = new JPasswordField(10);

    JButton edit = new JButton("Edit Member");
    JButton delete = new JButton("Delete Member");
    JButton generate = new JButton("Generate Report");
    JButton exit = new JButton("Exit");
    JButton load = new JButton("Load in");

    Toolkit kit = Toolkit.getDefaultToolkit();
    Dimension screenSize = kit.getScreenSize();
    int screenHeight = (int)screenSize.getHeight();
    int screenWidth = (int)screenSize.getWidth();

    public EditMember() {
        super("Edit Member");
        setLayout(new GridLayout(7,1,1,1));

        add(_id);
        add(load);
        load.addActionListener(new LoadInListener());

        add(new JLabel("Name:"));
        add(_name);

        add(new JLabel("Username:"));
        add(_username);

        add(new JLabel("Password:"));
        add(_password);

        add(new JLabel("Position:"));
        add(_position);

        add(edit);
        add(delete);
        delete.addActionListener(new DeleteListener());
        add(generate);
        add(exit);
        exit.addActionListener(new ExitListener());

        setSize(406, 200);
        setResizable(false);
        setLocation(screenWidth/4, screenHeight/4);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        new EditMember();
    }

    public Object[] getId() {
        Connection con;
        Statement stmt;
        ResultSet rs;

        //Object[] returnId;
        ArrayList<Object> returnId = new ArrayList<Object>();
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection("jdbc:odbc:collegesys","root","0blivi0n");

            stmt =con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
            rs = stmt.executeQuery("SELECT `id` FROM main");

            while(rs.next()) {
                returnId.add(rs.getObject("id"));
            }

            con.close();
        } catch(Exception e) {
            e.printStackTrace();
        }
        return returnId.toArray(new Object[returnId.size()]);
    }

    public class ExitListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            dispose();
        }
    }

    public class LoadInListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String giveId = _id.getSelectedItem().toString();

            populate(giveId);
        }
    }

    public class DeleteListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            delete();
        }
    }

    public void delete() {
        Connection con;
        Statement stmt;
        ResultSet rs;
        int idA;
        String name, username, password, position;

        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection("jdbc:odbc:collegesys","root","0blivi0n");

            stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
            PreparedStatement pstmt = con.prepareStatement("DELETE FROM `main` WHERE ID = ?");
            pstmt.setInt(1, (int)_id.getSelectedItem());
            pstmt.execute();
            // udpate JComboBox here
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public void populate(String a) {
        Connection con;
        Statement stmt;
        ResultSet rs;
        int idA;
        String name, username, password, position;

        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection("jdbc:odbc:collegesys","root","0blivi0n");

            stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
            rs = stmt.executeQuery("SELECT * FROM main");

            while(rs.next()) {
                idA = rs.getInt("id");
                name = rs.getString("name");
                username = rs.getString("username");
                password = rs.getString("password");
                position = rs.getString("position");
                if(Integer.parseInt(a) == idA) {
                    _name.setText(name);
                    _username.setText(username);
                    _password.setText(password);
                    _position.setSelectedItem(position);
                }
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}
Tharwen
  • 3,057
  • 2
  • 24
  • 36
Nathan Kreider
  • 516
  • 3
  • 7
  • 16

1 Answers1

2

As stated here you could do something like so:

public void delete()
{
    ...
    SwingUtils.invokeLater(new Runnable()
    {
        @Override
        public void run()
        {
            DefaultComboBoxModel model = new DefaultComboBoxModel( yourStringArray );
            comboBox.setModel( model );
        }
    });
}

yourStringArray is whatever you would like to render within the JComboBox. I haven't been around Swing in a while but usually whatever GUI changes you need to do will require you to go through the SwingUtils.invokeLater(Runnable). This will make your changes operate through the Event Dispatcher Thread (EDT) which is the thread taking care the GUI. Most examples online to do not do this, reason being that most of them seem to be making changes within Event Listeners, which themselves run within the EDT.

Community
  • 1
  • 1
npinti
  • 51,780
  • 5
  • 72
  • 96