Forums Register Login

Two problems with showMessageDialog

+Pie Number of slices to send: Send
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?
Thanks,
Chris Funkhouser
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
fileChooser2.setSelectedFile(dummyFile);
fileChooser2.setFileFilter(filter);
fileChooser2.addChoosableFileFilter(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,
Chris
If somebody says you look familiar, tell them you are in porn. Or in these tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1015 times.
Similar Threads
validating user input for number of seats
this page is accessing information that is not under its control
JTable/GUI Questions
enter a value
simple? dialog box
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 05:37:29.