In my application an error massage comes when the required data fields are not filled.
I did it as,

if(component.getSelectedItem().equals("")|description.getText().toString().equals("")){
         JOptionPane.showMessageDialog(null,"Please insert required details \n  ","Error",JOptionPane.ERROR_MESSAGE);
}

the problem: When I click "OK" when this massage pops up, it closes the entire window.

How can I avoid it. I want to return to the application when "OK" button pressed..

please help

thankx.

Recommended Answers

All 7 Replies

What exception, and your problem could possibly be solved by using the "short-circuit" or "||" instead of "|", because that way the second half of that if statement won't even be evaluated if the first half is false. Currently, they are both being evaluated regardless of whether the first is false or not. Also, does not "getText()" return a String? Normally that type of method will, so I don't understand the "toString()" call after that.

Also, what is the rest of the statement after the "|", as the forum seems to have let it run of the end of the window so I can't see it (after the toString call).

What exception, and your problem could possibly be solved by using the "short-circuit" or "||" instead of "|", because that way the second half of that if statement won't even be evaluated if the first half is false. Currently, they are both being evaluated regardless of whether the first is false or not. Also, does not "getText()" return a String? Normally that type of method will, so I don't understand the "toString()" call after that.

Also, what is the rest of the statement after the "|", as the forum seems to have let it run of the end of the window so I can't see it (after the toString call).

if(component.getSelectedItem().equals("")|description.getText().equals("")){

JOptionPane.showMessageDialog(null,"Please insert required details \n ","Error",JOptionPane.ERROR_MESSAGE);

}

And what about the rest of the post?

Also, closing a JOptionPane definately should not cause the entire application to close. What does the rest of the method where this statement resides look like?

Also, closing a JOptionPane definately should not cause the entire application to close. What does the rest of the method where this statement resides look like?

private void OkActionPerformed(ActionEvent evt)
		 {	
					con = new Connectelec();
					
					String j = eForm.tJno.getText();
					String d = eForm.tDno.getText();
					String c = eForm.tcomponent.getText();	
						
					
		try{			
				if(cmbcomponent.getSelectedItem().equals("")||description.getText().toString().equals("")){
				
								JOptionPane.showMessageDialog(null,"Please insert required details \n  ","Error",JOptionPane.ERROR_MESSAGE);
						
			
				}else{	
					
					con.insertDetails("insert into electrical_models.electrical_components values('"+j+"','"+d+"','"+c+"','"+cmbcomponent.getSelectedItem().toString()+"','
"+description.getText().toString()+"')");
					
				}
				
				
		
				}catch(Exception e){   
							
   									JOptionPane.showMessageDialog(null,"Error in Recording the data \n  ","Error",JOptionPane.ERROR_MESSAGE);
    			}	

}				
				
setVisible(false);						
		
		
		 }

Connectelec is a class that I created which facilitate the connection to the database. ITs insertDetails method is called here

Redesign that "insertdetails" thing. You should not be cobbling together SQL statements like this. It is far too error-prone and an extreme security risk. You have no idea how easy it is to perform an SQL Injection attack like that.

Also, is "setVisible(false);" suppossed to be outside of the method? It is, in the code you posted, which would make this utterly uncompilable.

If, in the "real" code, it is inside the method, I would say that is causing your application to exit (especially if your default close operation is EXIT_ON_CLOSE), or simply hiding it otherwise, regardless of what else happens in that method.

Redesign that "insertdetails" thing. You should not be cobbling together SQL statements like this. It is far too error-prone and an extreme security risk. You have no idea how easy it is to perform an SQL Injection attack like that.

Also, is "setVisible(false);" suppossed to be outside of the method? It is, in the code you posted, which would make this utterly uncompilable.

If, in the "real" code, it is inside the method, I would say that is causing your application to exit (especially if your default close operation is EXIT_ON_CLOSE), or simply hiding it otherwise, regardless of what else happens in that method.

thakx i got that. SetVisible() is the problem!!!!

I had no idea about sql thing at all. thakx 4 pointin out...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.