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.
– novic
@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.
– user28595
@diegofm, then, is that way. I believe this is a bad implementation and cause many problems.
– novic
Thank you! I have now left only Joptionpane of the Soma class. vlw
– Jose.Lemo