JButton没有按预期工作

时间:2015-08-04 08:45:27

标签: java jtable jbutton rows

我已经在我的JDialog中为JButton执行了此操作,我希望每次按下它时都添加行。但它并不像我想的那样工作,也没有向我的查看表中添加行。

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


}   

当我第二次按下按钮时,它不会从我的Activity类创建新动作,act.printactivitytInformation();不显示信息。

似乎代码在这一行之后不起作用。

1 个答案:

答案 0 :(得分:0)

最后我意外地发现了这个问题。

我正在处理Create dynamic table to add new entry with button发布的代码 然后我正在剪切并粘贴我在这里找到代码的if子句

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

} 

没有任何向表添加Activity对象。因此,在第一次点击按钮后,我没有向表中添加任何数据。 :)

这就是为什么他们说即使你确定也检查你的if条款。 ;)