English 中文(简体)
更新 JComboBox
原标题:Updating a JComboBox

我有一个 JComboBox 自动设置, 其方法可以在启动时从数据库中抓取全部 Id s, 我还有一个删除功能, 这将从数据库中删除选中的对象, 但是它不会从列表中删除 ID 。 执行删除方法时, 我是否可以更新 JComboBox? 如果有帮助, 我的代码会低于此值 。

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();
        }
    }
}
问题回答

说到





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...