Results 1 to 3 of 3
  1. #1
    gdanelian is offline Member
    Join Date
    Nov 2008
    Posts
    2
    Rep Power
    0

    Default Sick of duplicating JOptionPane.showMessageDialog

    Hello all!
    I am new so please be patient and please help (!)

    First of all here is my code:
    import javax.swing.JOptionPane;


    public class Tut20 {

    public static void main(String[] args) {

    //call on method to add5 and print
    addFiveToInteger();

    }//end main

    //set up user method
    public static void addFiveToInteger(){
    String userNumber;
    int integer;
    userNumber = JOptionPane.showInputDialog("Enter a number, we will add five");
    integer = Integer.parseInt(userNumber);
    //print message
    printMessage(userNumber, integer);
    }//end user method

    //create a print method
    public static void printMessage(String output, int integer){
    output = "The number you entered is: " + integer + " Adding 5 to this gives: " + (integer + 5);
    JOptionPane.showMessageDialog(null, output, "Tut20" , JOptionPane.INFORMATION_MESSAGE);

    }
    }//end class


    All the program does is read in an integer and adds 5.
    PROBLEM: What if i wanted to add another method that took the same userNumber and added 10 to it. Is it really necessary to keep on duplicating:
    JOptionPane.showMessageDialog(null, output, "Tut20" , JOptionPane.INFORMATION_MESSAGE);
    Obviously the output message will be different, how would I set this up? I am assuming that experiences javapeeps only write JOptionPane..as little as possible!

    Thank you for your time, please help me bust this duplicate code!
    :)

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    JOptionPane.showMessageDialog() works the same way as MessageDialog.openInformation() correct? I don't think there's any set method that could help you but you could work around it by just creating a method showDialog() and calling that each time you need to show it. Of course your going to need to send your string variables in as parameters.

    Alternatively, you could create an output shell with a view that has a text label( a little ugly compared to the message pane) but this would make it easier to change the text whenever you needed.

  3. #3
    gdanelian is offline Member
    Join Date
    Nov 2008
    Posts
    2
    Rep Power
    0

    Default

    Figured it out :)


    import javax.swing.JOptionPane;


    public class Tut20 {

    public static void main(String[] args) {

    //call on method to add5 and print
    addFiveToInteger();

    }//end main

    //set up user method
    public static void addFiveToInteger(){
    String userNumber;
    int integer;
    userNumber = JOptionPane.showInputDialog("Enter a number, we will add five");
    integer = Integer.parseInt(userNumber);

    //print message
    printMessage(userNumber, integer);
    printMessage2(integer, userNumber);
    }//end user method

    //create a print method
    public static void printMessage(String output, int integer){
    output = "The number you entered is: " + integer + " Adding 5 to this gives: " + (integer + 5);
    printJOption(integer, output);

    }//end method 1

    public static void printMessage2(int integer, String output){
    output = "The number you entered is: " + integer + " Adding 10 to this gives: " + (integer + 10);
    printJOption(integer, output);

    }//end method2

    //create a JOptionPane Method
    public static void printJOption(int int1, String output){
    JOptionPane.showMessageDialog(null, output, "Tut20" , JOptionPane.INFORMATION_MESSAGE);
    }
    }//end class

Similar Threads

  1. Replies: 1
    Last Post: 10-29-2008, 07:50 AM
  2. JOptionPane
    By whosadork in forum New To Java
    Replies: 2
    Last Post: 10-23-2008, 02:17 AM
  3. About JOptionPane.showMessageDialog
    By jhetfield18 in forum AWT / Swing
    Replies: 2
    Last Post: 11-02-2007, 10:45 PM
  4. About JOptionPane.showMessageDialog
    By jhetfield18 in forum Advanced Java
    Replies: 0
    Last Post: 11-02-2007, 10:56 AM
  5. Replies: 1
    Last Post: 07-31-2007, 04:01 AM

Posting Permissions

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