Jcombobox没有选择项目

时间:2019-02-18 07:56:33

标签: java database sqlite user-interface

我试图将Jcombobox值插入我的sqlite数据库中,但是每当我运行该程序时,它只会在我的数据库中插入“ 9”。请指教。我正在尝试将用户选择的整数导入我的sqlite数据库。由于某些原因,即使使用动作侦听器,仍将默认值9输入到表中,但不确定为什么。这是完整的代码,不包括连接和Jtable。

public class create extends JFrame {

private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                create frame = new create();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
    Connection connect = null;
    private JTextField textField_2;

    String uniqueString = UUID.randomUUID().toString();
    private JTextField textField_3;


/**
 * Create the frame.
 */
public create() {
    connect = connection.dbConnector();

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBackground(new Color(204, 204, 204));
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lblStudentName = new JLabel("Student ID:");
    lblStudentName.setBounds(96, 69, 72, 16);
    contentPane.add(lblStudentName);

    textField = new JTextField();
    textField.setBounds(173, 64, 216, 26);
    contentPane.add(textField);
    textField.setColumns(10);

    JLabel lblGrade = new JLabel("Grade:");
    lblGrade.setBounds(125, 107, 47, 16);
    contentPane.add(lblGrade);

    JComboBox comboBox = new JComboBox();
    comboBox.addItem(9);
    comboBox.addItem(10);
    comboBox.addItem(11);
    comboBox.addItem(12);
    comboBox.setBounds(173, 102, 72, 29);
    contentPane.add(comboBox);
    comboBox.setSelectedItem(9); 
    comboBox.addActionListener(comboBox); 
    int selectedNumber = (int)comboBox.getSelectedItem();

    JLabel lblInputBookOver = new JLabel("Teacher:");
    lblInputBookOver.setBounds(111, 146, 61, 21);
    contentPane.add(lblInputBookOver);

    textField_1 = new JTextField();
    textField_1.setBounds(173, 143, 216, 26);
    contentPane.add(textField_1);
    textField_1.setColumns(10);

    JButton button = new JButton("<");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            for (int count = 0; count <= 0; count++) {
                //dispose();
                options sc = new options();
                sc.setVisible(true);
            }
        }
    });
    button.setBounds(6, 6, 30, 29);
    contentPane.add(button);

    JLabel lblStudentname = new JLabel("Student Name:");
    lblStudentname.setBounds(74, 31, 99, 16);
    contentPane.add(lblStudentname);

    textField_2 = new JTextField();
    textField_2.setBounds(173, 26, 216, 26);
    contentPane.add(textField_2);
    textField_2.setColumns(10);

    JLabel lblEmail = new JLabel("Email:");
    lblEmail.setBounds(125, 180, 42, 26);
    contentPane.add(lblEmail);

    textField_3 = new JTextField();
    textField_3.setBounds(173, 180, 216, 26);
    contentPane.add(textField_3);
    textField_3.setColumns(10);

    JButton btnCheckout = new JButton("Checkout");
    btnCheckout.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            final String uniqueString = UUID.randomUUID().toString().replace("-", "");

            try {
                String query = "insert into data ('Name', 'Student ID', 'Teacher', 'Grade', 'Email', 'Ebook') values (?,?,?,?,?,?)";
                PreparedStatement pst = connect.prepareStatement(query);
                pst.setString(1,textField_2.getText() );
                pst.setString(2,textField.getText() );
                pst.setString(3,textField_1.getText() );
                pst.setInt(4, selectedNumber);
                pst.setString(5,textField_3.getText() );
                pst.setString(6, uniqueString);

                for (int count = 0; count <= 0; count++) {
                    //dispose();
                    confirm sc = new confirm();
                    sc.setVisible(true);
                    count = 0;
                    }

                pst.execute();                  
                pst.close();

            }

            catch (Exception w){
                w.printStackTrace();

            }   
        }
    });
    btnCheckout.setBounds(173, 218, 117, 29);
    contentPane.add(btnCheckout);


}
}

1 个答案:

答案 0 :(得分:0)

由于所选项目始终为9,因此它将9插入数据库中,这是因为您首先将其添加到了组合框中。为了插入选定的值,您将必须使用ActionListener.,然后才能进行用户的选择并做任何您想做的事情。

其用法示例:

if (!ctx._source.user_ids.contains(users)){ctx._source.user_ids += users}