Why isn’t this method adding up?

Asked

Viewed 197 times

3

I’m trying to put two numbers together by Swing and JOptionPane, but is appearing error below.

Someone to help me 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

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

Follows 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                   
}
  • It seems like other problems, like a showDialog calling another one among themselves! strange.

  • 1

    @Virgilionovic in fact are called independent (although not seeing with good eyes keep filling code of joptionpane), when the method execute, will appear the first, after returning, will appear the second, syntactically will not occur errors, but I also find this approach strange.

  • 2

    @diegofm, then, is that way. I believe this is a bad implementation and cause many problems.

  • Thank you! I have now left only Joptionpane of the Soma class. vlw

1 answer

6


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

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


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

As an alternative to the above method, I suggest you change your class Soma 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 method B_SomarActionPerformed. 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 method showInternalMessageDialog, as well as most of the methods of JOptionPane, waits as one of its parameters a container relative, if passed null, it will fetch the application’s main container, in this case, a Jframe, but this particular method expects the container to be a JInternalFrame or JDesktopPane, so the reported error is broken. Passing the this the way I did, I’m referenced to class itself TesteJInternalFrame as a relative, thus the JOptionPane will work normally, without locking the entire application window, only internal frames.

  • 1

    Thank you very much!

  • @Jose.Lemo see the edition I made, with a second way to adapt the code without the error.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.