【问题标题】:Jcombobox not selecting itemJcombobox没有选择项目
【发布时间】:2019-02-18 16:54:47
【问题描述】:

我试图将 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);


}
}

【问题讨论】:

  • DefaultComboBoxModel 的默认“选定项”在未选择任何内容时是第一个 (selectedObject = getElementAt(0))。在您的代码中,没有选择任何项目。
  • 我明白了,所以 9 只是默认值。我应该把“selectedObject = getElementAt(0)”放在哪里?感谢您的输入

标签: java database sqlite user-interface


【解决方案1】:

它将 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));
    }

}

【讨论】:

  • 我进行了编辑,但它似乎仍然不起作用。感谢您迄今为止的意见
  • 我刚刚更新了所有内容。感谢您的帮助,我仍然是这一切的初学者
猜你喜欢
  • 1970-01-01
  • 2021-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-28
相关资源
最近更新 更多