• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Two problems with showMessageDialog

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
What's that smell? Hey, sniff this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic