• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Two problems with showMessageDialog

 
Chris Funkhouser
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chris Funkhouser
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic