Results 1 to 3 of 3
  1. #1
    SeanC is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

    Unhappy Problem creating dialog box

    I'm creating a word processor similar to Microsoft Word (but much simpler). I'm having a problem with a dialog box.

    When a user presses the 'new document' button, it prompts the user to enter the file name, and when the user does so, he presses ok, and the file name is stored in a variable. That works out fine, the problem is when the user presses cancel! I could think of no way to manipluate what happens when the user presses cancel, (the default action is that the file name is set as null, rather than just leaving it as it was before) so I added some code myself by identifying the length of the string - basically, if the length of the string is 0, then cancel was pressed because there was no input. However, I am getting an error. This is the code:

    Java Code:
      private void newDocument() {
            String tempFileName = JOptionPane.showInputDialog(null, "Please enter file name", "Please enter file name", JOptionPane.INFORMATION_MESSAGE);
            if (tempFileName.length() == 0) {
                tempFileName = "";
    
            } else {
                fileName = tempFileName;
                textArea.setText(null);
            }
    
            textArea.setEditable(true);
            textArea.setEnabled(true);
            enableButtons();
        }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default

    According to the API documentation the JOptionDialog returns null if the user canceled the dialog. Simply test for null being returned.

    kind regards,

    Jos

  3. #3
    SeanC is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    According to the API documentation the JOptionDialog returns null if the user canceled the dialog. Simply test for null being returned.

    kind regards,

    Jos
    Thanks! :)

Similar Threads

  1. Problem creating a new mobile app
    By yasser1988 in forum NetBeans
    Replies: 0
    Last Post: 07-22-2010, 12:02 PM
  2. save dialog problem
    By masa in forum AWT / Swing
    Replies: 2
    Last Post: 03-06-2009, 12:41 AM
  3. Problem in dialog
    By Sarinam in forum New To Java
    Replies: 5
    Last Post: 07-12-2008, 11:38 AM
  4. Creating a Number Input Dialog in SWT
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-02-2008, 07:53 PM
  5. Creating a dialog to input user/password
    By prfalco in forum New To Java
    Replies: 4
    Last Post: 02-18-2008, 07:03 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •