Why is this method not adding up?

3

I'm trying to add two numbers by Swing and JOptionPane , but the below error is appearing.

Someone to help me how to solve this?

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: JOptionPane: parentComponent does not have a valid parent
    at javax.swing.JOptionPane.createInternalFrame(JOptionPane.java:1510)
    at javax.swing.JOptionPane.showInternalOptionDialog(JOptionPane.java:1286)
    at javax.swing.JOptionPane.showInternalMessageDialog(JOptionPane.java:1099)
    at javax.swing.JOptionPane.showInternalMessageDialog(JOptionPane.java:1073)
    at javax.swing.JOptionPane.showInternalMessageDialog(JOptionPane.java:1047)
    at DAO.Soma.somarNumeros(Soma.java:15)  at GUI.TesteMiniFrame.B_SomarActionPerformed(TesteMiniFrame.java:119)
    at GUI.TesteMiniFrame.access$000(TesteMiniFrame.java:6)
    at GUI.TesteMiniFrame$1.actionPerformed(TesteMiniFrame.java:33)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6525)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6290)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4881)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2750)  at

Follow the class Soma

import javax.swing.JOptionPane;

public class Soma {
    public int somarNumeros(int numero1,int numero2){
        int resultado = numero1 + numero2;
        JOptionPane.showInternalMessageDialog(null, resultado);
        return 0; 
    } 
} 

Follow the class TesteJInternalFrame

import javax.swing.JOptionPane;

public class TesteJInternalFrame extends javax.swing.JInternalFrame {

    Soma somar;    

    public TesteJInternalFrame(){
        initComponents();
        somar = new Soma();
    }

    private void B_SomarActionPerformed(java.awt.event.ActionEvent evt) {                                        
        int num1,num2,res;
        num1 = Integer.parseInt(TF_Valor1.getText());
        num2 = Integer.parseInt(TF_Valor2.getText()); 
        res = Integer.parseInt(TF_Resultado.getText());  
        TF_Resultado.setText(String.valueOf(res));  
        JOptionPane.showMessageDialog(null, somar.somarNumeros(num1,num2)); 
        //B_Somar.setText(String.valueOf());   
        //JOptionPane.showInputDialog(null,s);  

    }                                       

    private void TF_Valor1ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
    }                                         

    private void TF_Valor2ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
    }                                         


    // Variables declaration - do not modify                     
    private javax.swing.JButton B_Somar;
    private javax.swing.JTextField TF_Resultado;
    private javax.swing.JTextField TF_Valor1;
    private javax.swing.JTextField TF_Valor2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    // End of variables declaration                   
}
    
asked by anonymous 31.01.2017 / 15:09

1 answer

6

The call JOptionPane.showInternalMessageDialog(); was made to be used when you have a JDesktopPane or JInternalFrame , and in your case, it is not being called any internal frame, just passing null .

Replace with JOptionPane.showMessageDialog(null, resultado); that will resolve the error.

By analyzing your code better, I could see that there is a possibility of using showInternalMessageDialog() , since its main class inherits from JInternalFrame . Although the method already suggested above works, since JOptionPane is a modal window, it blocks access to other windows until it is closed, and if the alert is for internal frames only, this behavior may not be desirable.

As an alternative to the above method, I suggest you change your Soma class as below:

import javax.swing.JOptionPane;

public class Soma {
    public int somarNumeros(int numero1,int numero2){
        int resultado = numero1 + numero2;
        JOptionPane.showInternalMessageDialog(this, resultado);
        return resultado; 
    } 
} 

And then, just change the line where you display the result, inside the JInternalFrame, in the B_SomarActionPerformed method. It would look like this:

import javax.swing.JOptionPane;

public class TesteJInternalFrame extends javax.swing.JInternalFrame {

    Soma somar;    

    public TesteJInternalFrame(){
        initComponents();
        somar = new Soma();
    }

    private void B_SomarActionPerformed(java.awt.event.ActionEvent evt) {                                        
        int num1,num2,res;
        num1 = Integer.parseInt(TF_Valor1.getText());
        num2 = Integer.parseInt(TF_Valor2.getText()); 
        res = Integer.parseInt(TF_Resultado.getText());  
        TF_Resultado.setText(String.valueOf(res));  

        JOptionPane.showInternalMessageDialog(this, somar.somarNumeros(num1,num2)); 
        //B_Somar.setText(String.valueOf());   
        //JOptionPane.showInputDialog(null,s);  

    }                                       

    private void TF_Valor1ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
    }                                         

    private void TF_Valor2ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
    }                                         


    // Variables declaration - do not modify                     
    private javax.swing.JButton B_Somar;
    private javax.swing.JTextField TF_Resultado;
    private javax.swing.JTextField TF_Valor1;
    private javax.swing.JTextField TF_Valor2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    // End of variables declaration                   
}

The showInternalMessageDialog ", like most methods of JOptionPane , expects as one of your parameters a

31.01.2017 / 15:15