0

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. 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. And in the next row the data that appears in the previous row also appear.

If you need more information please comment. Thank you!

camickr
  • 321,443
  • 19
  • 166
  • 288
  • 2
    1) Don't add a MouseListener to the combo box. If you want to do processing when the data in the JTable is changed, then add a TableModelListener to the TableModel. See: https://stackoverflow.com/a/3541876/131872 for a working example. I'm not sure I understand your question, but the same editor is used for every row in the column. If you are trying to have a different editor for each row then check out: https://stackoverflow.com/a/4211552/131872 for one approach. – camickr Oct 30 '20 at 15:30
  • Apart from column 3, are the other `JTable` columns editable? How do you initially create the `TableModel`? – Abra Oct 31 '20 at 09:36
  • @Abra Yes they are editable – HAAS BOOSTED Oct 31 '20 at 17:45

1 Answers1

0

You don't need to use any listeners. You need to override method getTableCellEditorComponent() in TableCellEditor.

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. I'm guessing that you are using DefaultTableModel. If you are, then it already has an appropriate setValueAt() method so you don't need to do anything there.

Also don't forget to close the database connection as well as handling any SQLException that may be thrown. Maybe consider using PreparedStatement rather than Statement.

Abra
  • 19,142
  • 7
  • 29
  • 41