简体   繁体   English

如何使用数据库中的信息将 jcombobox 步进到数组

[英]How to step a jcombobox to an array with information from a database

I have a pogram in java where I have a JFrame with a Jtable with 10 rows and 4 columns in the third column is a jcombobox that goes to DB to fetch the values that belong to column 3 for the values entered in column 2 and 1 of that row.我在 java 中有一个 pogram,其中我有一个 JFrame 和一个 Jtable,在第三列中有 10 行和 4 列是一个 jcombobox,它转到 DB 以获取属于第 3 列的值,用于在第 2 列和第 1 列中输入的值那一行。 Here is the code:这是代码:

      //JCOMBOBOX CREATED
        TableColumn sportColumn = table.getColumnModel().getColumn(3);
        JComboBox comboBox = new JComboBox();
        sportColumn.setCellEditor(new DefaultCellEditor(comboBox));

comboBox.addMouseListener(new MouseAdapter() {
  public void mousePressed(MouseEvent event) {
  
      try{     
    
            int comboboxRow = table.getSelectedRow();
            String OFNUPK = (String) table.getValueAt(comboboxRow, 1);
            String TFCCMP = (String) table.getValueAt(comboboxRow, 2);

            // Se o valor for nulo ou não inteiro isto vai atirar erro
            int OFNUP = Integer.parseInt(OFNUPK);

            // Verificar se o valor é válido
            if (TFCCMP != null && !TFCCMP.isEmpty()) {
            Connection con = DriverManager.getConnection("jdbc:as400://" + host, user, pwd);
            // create new statement from connection
            Statement stmt = con.createStatement();

            // Se puderes aqui usa prepared statements como te mostrei ontem
            String s = "SELECT DISTINCT a.TFCPAI FROM $$CLI00F55.FOFFAN a, SICGA00F55.FORFAB f WHERE "
            + "a.TFCCMP = '" + TFCCMP + "' AND f.OFNUPK = " + OFNUP +" AND f.OFNUPK = a.TFSNOF";

            ResultSet rs = stmt.executeQuery(s);
            //APAGAR OS DADOS ANTERIORES
           while(rs.next())
            {
                comboBox.addItem(rs.getString(1));
            }

I'm having 2 problems when clicking on the Jcombobox in the same row its adding the same values.单击同一行中的 Jcombobox 添加相同的值时,我遇到了两个问题。 And in the next row the data that appears in the previous row also appear.在下一行中,上一行中出现的数据也会出现。

If you need more information please comment.如果您需要更多信息,请发表评论。 Thank you!谢谢!

You don't need to use any listeners.您不需要使用任何侦听器。 You need to override method getTableCellEditorComponent() in TableCellEditor .您需要覆盖TableCellEditor 中的getTableCellEditorComponent()方法。

JComboBox comboBox = new JComboBox();
sportColumn.setCellEditor(new DefaultCellEditor(comboBox)) {
    public Component getTableCellEditorComponent(JTable table,
                                                 Object value,
                                                 boolean isSelected,
                                                 int row,
                                                 int column) {
        JComboBox comboBox = (JComboBox) editorComponent
        String OFNUPK = (String) table.getValueAt(row, 1);
        String TFCCMP = (String) table.getValueAt(row, 2);

        // Se o valor for nulo ou não inteiro isto vai atirar erro
        int OFNUP = Integer.parseInt(OFNUPK);

        // Verificar se o valor é válido
        if (TFCCMP != null && !TFCCMP.isEmpty()) {
            Connection con = DriverManager.getConnection("jdbc:as400://" + host, user, pwd);
            // create new statement from connection
            Statement stmt = con.createStatement();

            // Se puderes aqui usa prepared statements como te mostrei ontem
            String s = "SELECT DISTINCT a.TFCPAI FROM $$CLI00F55.FOFFAN a, SICGA00F55.FORFAB f WHERE "
            + "a.TFCCMP = '" + TFCCMP + "' AND f.OFNUPK = " + OFNUP +" AND f.OFNUPK = a.TFSNOF";

            ResultSet rs = stmt.executeQuery(s);
            //APAGAR OS DADOS ANTERIORES
            while(rs.next())
            {
                comboBox.addItem(rs.getString(1));
            }
        }
    });

Note that you also need to make sure that your TableModel has an appropriate setValueAt() method.请注意,您还需要确保您的TableModel具有适当的setValueAt()方法。 I'm guessing that you are using DefaultTableModel .我猜你正在使用DefaultTableModel If you are, then it already has an appropriate setValueAt() method so you don't need to do anything there.如果你是,那么它已经有一个合适的setValueAt()方法,所以你不需要在那里做任何事情。

Also don't forget to close the database connection as well as handling any SQLException that may be thrown.另外不要忘记关闭数据库连接以及处理可能抛出的任何SQLException Maybe consider using PreparedStatement rather than Statement .也许考虑使用PreparedStatement而不是Statement

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM