【问题标题】:How to step a jcombobox to an array with information from a database如何将 jcombobox 步进到包含数据库信息的数组
【发布时间】:2020-10-30 10:14:51
【问题描述】:

我在 java 中有一个 pogram,其中我有一个带有 Jtable 的 JFrame,第三列中有 10 行和 4 列是一个 jcombobox,它转到 DB 以获取属于第 3 列的值,以获取在第 2 列中输入的值和该行的 1 个。 代码如下:

      //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));
            }

单击同一行中的 Jcombobox 添加相同的值时,我遇到了 2 个问题。 并且在下一行中,上一行中出现的数据也会出现。

如果您需要更多信息,请发表评论。 谢谢!

【问题讨论】:

  • 1) 不要在组合框中添加 MouseListener。如果要在 JTable 中的数据发生变化时进行处理,则在 TableModel 中添加一个 TableModelListener。有关工作示例,请参阅:stackoverflow.com/a/3541876/131872。我不确定我是否理解您的问题,但列中的每一行都使用相同的编辑器。如果您尝试为每一行使用不同的编辑器,请查看:stackoverflow.com/a/4211552/131872 以获得一种方法。
  • 除第 3 列外,其他 JTable 列是否可编辑?您最初是如何创建 TableModel 的?
  • @Abra 是的,它们是可编辑的

标签: java swing jcombobox


【解决方案1】:

您不需要使用任何侦听器。您需要在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));
            }
        }
    });

请注意,您还需要确保您的 TableModel 具有适当的 setValueAt() 方法。我猜你正在使用DefaultTableModel。如果你是,那么它已经有一个合适的setValueAt() 方法,所以你不需要在那里做任何事情。

不要忘记关闭数据库连接以及处理任何可能抛出的SQLException。也许考虑使用PreparedStatement 而不是Statement

【讨论】:

    猜你喜欢
    • 2021-10-16
    • 2016-12-20
    • 2020-03-15
    • 2011-12-21
    • 2016-07-05
    • 1970-01-01
    • 1970-01-01
    • 2011-01-06
    • 1970-01-01
    相关资源
    最近更新 更多