java - Jcombobox 未选择项目

标签 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);


}
}

最佳答案

它会将 9 插入数据库,因为所选项目始终为 9,这是因为您首先将其添加到组合框。为了插入选定的值,您必须使用 ActionListener.然后你就可以接受用户的选择并做任何你想做的事情。

其用法示例:

import java.awt.FlowLayout;
import java.io.FileNotFoundException;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class ComboBox extends JFrame {

    public ComboBox() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(300, 300);
        setLocationRelativeTo(null);
        getContentPane().setLayout(new FlowLayout());
        Integer[] numbers = { 4, 5, 8, 123, 42, 634 };
        JComboBox<Integer> comboBox = new JComboBox<>(numbers);
        comboBox.setSelectedItem(42); // The initial selection is 42.
        comboBox.addActionListener(e -> {
            int selectedNumber = (int) comboBox.getSelectedItem();
            System.out.println("Selected number: " + selectedNumber);
            // Do whatever with selected number
        });
        add(comboBox);
    }

    public static void main(String[] args) throws FileNotFoundException {
        SwingUtilities.invokeLater(() -> new ComboBox().setVisible(true));
    }

}

关于java - Jcombobox 未选择项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54742659/

相关文章:

java - 在 jasypt 中使用 BasicTextEncryptor 进行 iOS 和 Java 加密

ios - 在 Xcode 的编译时禁用 -ffast-math

python - OperationalError "unable to open database file"使用 SQLAlchemy 和 SQLite3 处理查询结果

sql - 如何转储某些 SQLite3 表的数据?

java - Android 上均匀间隔按钮

java - 更改 Protocol Buffer 中字段的数据类型

SQL查询麻烦

java - 更改 Java 项目的数据库类型?

mysql - 将具有 2 个 FK 的表连接到一张表

java - ArrayList 的大小