File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Two problems with showMessageDialog Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Two problems with showMessageDialog" Watch "Two problems with showMessageDialog" New topic

Two problems with showMessageDialog

Chris Funkhouser

Joined: Dec 10, 2001
Posts: 12
I'm using a dialog box with the following code:
JOptionPane.showMessageDialog(null, "The filename must end with '.csv'", "Save File Error", JOptionPane.ERROR_MESSAGE);
Sometimes, seemingly at random, the message and the OK button don't show up. Just the dialog box and the title. Moving the mouse over it makes the OK button show up. Has anyone else experienced this and is there a way to fix it?
Also, when the message does show up, the last couple of characters are often cut off, as if there's not enough room for them. Is there any way to make sure that the dialog box will be big enough to hold the message?
Chris Funkhouser
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299

I just ran your code 10 times and didn't have a single problem. I do however have a few questions:
1. Why are you using null. Try making it model to the parent frame.
2. Are you using all Swing? Or are you mixing Swing and Awt by any chance?
3. Could you post some of the code around your JOptionPane so we can maybe better determine what is going on.

GenRocket - Experts at Building Test Data
Chris Funkhouser

Joined: Dec 10, 2001
Posts: 12
Gregg --
Thanks for the quick reply!
1. I'm using null because I don't know how to reference the parent frame. The parent frame is a class with nothing in the main method; an instance of its class gets instantiated by another class. So I'm not sure what to call it. I tried "this", and that seemed to fix the problem of the messageDialog cutting off part of the message.
2. I believe I'm only using Swing components.
3. Here's the method in which showMessageDialog is used
void dataButton_ActionPerformed(ActionEvent event)
File selectedFile;
File dummyFile = new File("Data.csv");

if (fileChooser2 == null) {
fileChooser2 = new JFileChooser();
FileFilter filter = new FileFilter() {
public boolean accept(File f) {
String fn = f.getName();
if (fn.endsWith(".csv") || f.isDirectory()) return true;
else return false;
} //end accept
public String getDescription() { return "Comma-separated Value Files (*.csv)"; }
}; //end filter
} //end if
int result = fileChooser2.showSaveDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
selectedFile = fileChooser2.getSelectedFile();
if (selectedFile.getName().endsWith(".csv")) {
//code to run if true
String dataMessage = new String("Your data has been saved as " + selectedFile.getName());
JOptionPane.showMessageDialog(null, dataMessage );
} //end if
else { JOptionPane.showMessageDialog(null, "The filename must end with '.csv'", "Save File Error", JOptionPane.ERROR_MESSAGE); }
} //end if
} //end dataButton_ActionPerformed
Thanks in advance,
I agree. Here's the link:
subject: Two problems with showMessageDialog