-1

I have got this action performed for a JButton in my JDialog that I want it to add rows each time I press it. But it doesn't work as I want and it doesn't add rows to my viewing table.

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    int activity_number = -1;
    for (HashMap.Entry<Integer ,String> entry : ad_activity_table_hashmap.entrySet()){
        if (entry.getValue() == jComboBox2.getSelectedItem()){
            activity_number = entry.getKey();
        }
    }
    if (jTextField17.getText().equals("") || jTextField19.getText().equals("")){
        JOptionPane.showConfirmDialog(null, "لطفا همه فيلد ها را پر كنيد", "Message", JOptionPane.PLAIN_MESSAGE);
    }
    else if (!FinancialDocumentsJFrame.checkDocumentAmountText(jTextField17.getText())){
        JOptionPane.showConfirmDialog(null, "ورودي مبلغ فعاليت نامعتبر است لطفا دوباره بررسي كنيد", "Message", JOptionPane.PLAIN_MESSAGE);
    }
    else if (!FinancialDocumentsJFrame.checkNumberText(jTextField19.getText())){
        JOptionPane.showConfirmDialog(null, "ورودي شماره قرارداد نامعتبر است لطفا دوباره بررسي كنيد", "Message", JOptionPane.PLAIN_MESSAGE);
    }
    else if (jComboBox2.getSelectedIndex()==-1){
        JOptionPane.showConfirmDialog(null, "شرح فعاليت انتخاب نشده است لطفا دوباره بررسي كنيد", "Message", JOptionPane.PLAIN_MESSAGE);
    } 
    else if (!activity_hashmap.isEmpty()){  
        if (activity_number == -1){
            System.out.println("Error in Activity Number");
            JOptionPane.showConfirmDialog(null, "اشكال در شماره فعاليت لطفا دوباره تلاش كنيد", "Message", JOptionPane.PLAIN_MESSAGE);
        }
        else {
           boolean exists = activity_hashmap.containsKey(activity_number); 
           if (exists){
               JOptionPane.showConfirmDialog(null, "ورودي موجود است لطفا دوباره بررسي كنيد", "Message", JOptionPane.PLAIN_MESSAGE);
           } 
        }

    }
    else {

        int activity_amount = Integer.parseUnsignedInt(jTextField17.getText());
        int activity_contract_number = Integer.parseUnsignedInt(jTextField19.getText());
        int activity_document_number = Integer.parseUnsignedInt(jTextField13.getText());
        String activity_st = jComboBox2.getSelectedItem().toString();

        Activity act = new Activity(activity_st, activity_number, activity_amount, activity_contract_number, activity_document_number);
        act.printactivitytInformation();
        activity_hashmap.put(activity_number, act);
        if (jTable1.getRowCount()==0){
            System.out.println("No Rows!!!"); 
            count_activities_amount = activity_amount;
            Object[] row = { activity_contract_number, Integer.parseUnsignedInt(jTextField16.getText())-count_activities_amount,  activity_amount, activity_st, activity_document_number };
            DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
            model.addRow(row);
        }
        else {
            System.out.println("Have Rows!!!");
            for (int j=1;j<=jTable1.getRowCount();j++){
                count_activities_amount += Integer.parseUnsignedInt(jTable1.getValueAt(j, 3).toString());
            }
            Object[] row = { activity_contract_number, Integer.parseUnsignedInt(jTextField16.getText())-count_activities_amount,  activity_amount, activity_st, activity_document_number };
            DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
            model.addRow(row);
        }
        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment( SwingConstants.CENTER);
        jTable1.setDefaultRenderer(String.class, centerRenderer);
        jTable1.repaint();
        jButton2.setEnabled(true);
        jButton3.setEnabled(true);
        jTextField18.setText(count_activities_amount.toString());
        }


}   

When I press the button for the second time it doesn't create a new act from my Activity class and act.printactivitytInformation(); does not show information.

It seems as if the code those not work after this line.

Daniel Waghorn
  • 2,997
  • 2
  • 20
  • 33
Sparrow7000
  • 79
  • 1
  • 11

1 Answers1

0

At last I found the problem accidentally.

I was working on the code posted at Create dynamic table to add new entry with button then I was cutting and pasting my if clauses that I found out code here

else if (!activity_hashmap.isEmpty()){  
    if (activity_number == -1){
        System.out.println("Error in Activity Number");
        JOptionPane.showConfirmDialog(null, "اشكال در شماره فعاليت لطفا دوباره تلاش كنيد", "Message", JOptionPane.PLAIN_MESSAGE);
    }
    else {
       boolean exists = activity_hashmap.containsKey(activity_number); 
       if (exists){
           JOptionPane.showConfirmDialog(null, "ورودي موجود است لطفا دوباره بررسي كنيد", "Message", JOptionPane.PLAIN_MESSAGE);
       } 
    }

} 

dose not have any adding Activity object to the table. So after the first time clicking on the button I'm not adding any data to the table. :)

That is why they say check your if clauses even if you are sure about it. ;)

Sparrow7000
  • 79
  • 1
  • 11