invoking JOptionPane from user-defined class

Thread Solved

Join Date: Jul 2007
Posts: 20
Reputation: Caled is an unknown quantity at this point 
Solved Threads: 0
Caled Caled is offline Offline
Newbie Poster

invoking JOptionPane from user-defined class

 
0
  #1
Mar 11th, 2009
Hello Experts,

I need to use JOptionPane a lot in my Java exercises. Instead clustering each Java files with this JOptionPane syntax plus the parameters, I've decided to use sort of class called showFrame which has the method to show message dialog and input dialog. At the moment, im putting everything in one Java file since im still stuck. But once successful, then I'll put it in a separate java file. My problem is that, I duno how to invoke the methods in showFrame class. I did put it in my main method but generates compile error.

I want to be to call the showFrame constructor and the parameters is based on values set in the local method that call this constructor. The default value is initialized in the showFrame class (this value will be used if user does not pass any arguments in the local method that call the showFrame constructor.

Here is my coding:
Can someone point me in the right direction, please..
  1. import javax.swing.*;
  2. import java.lang.*;
  3.  
  4. public class Joption {
  5.  
  6.  
  7. public class showFrame{
  8.  
  9. private String dialogTitle = "CCA";
  10. private int styleInfo = 2; //INFORMATION_MESSAGE type
  11. private String messageToUser = "";
  12. private String showFrame = "null";
  13.  
  14.  
  15. public void setDialogTitle(String dialogTitle)
  16. {
  17. this.dialogTitle = dialogTitle;
  18. }
  19.  
  20. public String getDialogTitle()
  21. {
  22. return (this.dialogTitle);
  23. }
  24.  
  25. public void setStyleInfo(int styleInfo)
  26. {
  27. this.styleInfo = styleInfo;
  28. }
  29.  
  30. public int getStyleInfo()
  31. {
  32. return (this.styleInfo);
  33. }
  34.  
  35. public void setMessageToUser(String messageToUser)
  36. {
  37. this.messageToUser= messageToUser;
  38. }
  39.  
  40. public String getMessageToUser()
  41. {
  42. return (this.messageToUser);
  43. }
  44.  
  45.  
  46. public void setShowFrame(String showFrame)
  47. {
  48. this.showFrame= showFrame;
  49. }
  50.  
  51. public String getShowFrame()
  52. {
  53. return (this.showFrame);
  54. }
  55.  
  56.  
  57. public void showMessageFrame(String showFrame, String messageToUser, String dialogTitle, int infoStyle)
  58. {
  59. JOptionPane.showMessageDialog(showFrame, messageToUser, dialogTitle, infoStyle);
  60.  
  61. }
  62.  
  63.  
  64. public void showInputPrompt(String showFrame, String messageToUser, String dialogTitle, int infoStyle)
  65. {
  66. JOptionPane.showInputPrompt(showFrame, messageToUser, dialogTitle, infoStyle);
  67.  
  68. }
  69.  
  70. }
  71.  
  72. public static void main (String args[]) {
  73.  
  74.  
  75.  
  76. JOptionPane.showMessageDialog(null,"Eggs are not supposed to be green.", "Message", JOptionPane.INFORMATION_MESSAGE);
  77.  
  78. String input = JOptionPane.showInputDialog(null, "Please Enter a value", "hello", JOptionPane.QUESTION_MESSAGE);
  79.  
  80. JOptionPane.showMessageDialog(null, "You entered the following text: " + input , "User Input", JOptionPane.INFORMATION_MESSAGE);
  81. }
  82. }

Thanks
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: invoking JOptionPane from user-defined class

 
1
  #2
Mar 11th, 2009
  1. public void showInputPrompt(String showFrame, String messageToUser, String dialogTitle, int infoStyle
  2. {
  3. JOptionPane.showInputPrompt(showFrame, messageToUser, dialogTitle, infoStyle);
  4. }

what exactly is it you're trying to do here? asking the user for input? do notice you're returning nothing (hence, the void)

If I understand correctly what it is you're trying to do, it might be easier to do this by creating a static class. just a short example:
  1. import javax.swing.JOptionPane;
  2.  
  3. public class Messages {
  4.  
  5. public static String askInput(){ return askInput("Enter your data");}
  6. public static String askInput(String message){ return askInput(message, "Default input");}
  7. public static String askInput(String message, String defaultInput){ return JOptionPane.showInputDialog(null, message, defaultInput);}
  8.  
  9. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 20
Reputation: Caled is an unknown quantity at this point 
Solved Threads: 0
Caled Caled is offline Offline
Newbie Poster

Re: invoking JOptionPane from user-defined class

 
0
  #3
Mar 11th, 2009
Originally Posted by stultuske View Post
  1. public void showInputPrompt(String showFrame, String messageToUser, String dialogTitle, int infoStyle
  2. {
  3. JOptionPane.showInputPrompt(showFrame, messageToUser, dialogTitle, infoStyle);
  4. }

what exactly is it you're trying to do here? asking the user for input? do notice you're returning nothing (hence, the void)

If I understand correctly what it is you're trying to do, it might be easier to do this by creating a static class. just a short example:
  1. import javax.swing.JOptionPane;
  2.  
  3. public class Messages {
  4.  
  5. public static String askInput(){ return askInput("Enter your data");}
  6. public static String askInput(String message){ return askInput(message, "Default input");}
  7. public static String askInput(String message, String defaultInput){ return JOptionPane.showInputDialog(null, message, defaultInput);}
  8.  
  9. }
Make it in this way.. I have many Java Files as for instance, A.Java, B.Java, C.Java, etc.... I want to be able to invoke showMessage dialog method of JOptionPane without having to type the full syntax
  1. JOptionPane.showMessageDialog(...)
. The same applies to showInputDialog method. Because Im gonna use these two methods to display simple java-window prompt nothing fancy.

As for example,
  1.  
  2. # Filename: B.Java
  3.  
  4. import myClass.showFrame
  5. # This showFrame class has all methods
  6. # to showMessageDialog and showInputDialog
  7. # and local variable declarations
  8.  
  9. public static void main (String args[])
  10. {
  11. String msg = "Test";
  12. String winTitle="ABC Co.";
  13. int infoStyle = 2; // for JOptionPane.INFORMATION_MESSAGE
  14. showFrame aFrame = new showFrame ();
  15. aFrame.showMessageDlg(null, msg, title, 2);
  16.  
  17. }

in this local main, I can specify the parameter and pass it to the showMessageDlg method. If user does not specify any parameters, the default one will be used - which has been initialized to some values. My intention of doing this is to avoid retyping the lengthy JOptionPane Syntax plus make my coding more easier to read and modify..I did try several methods but to no avail.

Thanks...
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: invoking JOptionPane from user-defined class

 
0
  #4
Mar 11th, 2009
That is exactly what the static methods on the utility class that stultuske suggested above would do for you.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 20
Reputation: Caled is an unknown quantity at this point 
Solved Threads: 0
Caled Caled is offline Offline
Newbie Poster

Re: invoking JOptionPane from user-defined class

 
0
  #5
Mar 11th, 2009
so in order to call the utility class
i need to instantiate the class
Message aMsg = new Message()
then to use the method that has the JOptionPane
aMsg.askInput(...)
???
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: invoking JOptionPane from user-defined class

 
0
  #6
Mar 11th, 2009
No, you don't need to instantiate the class to call static methods. Just prefix the method with the class name. So using stultuske's class above you would simply call
  1. String response = Messages.askInput("Enter some data");
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,568
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: invoking JOptionPane from user-defined class

 
0
  #7
Mar 11th, 2009
Static methods exist for the entire class -- although I'm not sure if this is technically correct, you can think of it as there being 'one copy' for the entire class. Static methods exist whether or not you create an Object of that class type, hence the reason why you can invoke them the way Ezzaral explained. More about static methods:

http://java.sun.com/docs/books/tutor...classvars.html
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 20
Reputation: Caled is an unknown quantity at this point 
Solved Threads: 0
Caled Caled is offline Offline
Newbie Poster

Re: invoking JOptionPane from user-defined class

 
0
  #8
Mar 11th, 2009
Thanks everyone...
But how can I invoke that method from other class? I want to create a separate utility class for this showMessageDialog and showInputDialog. If im working in b.java, then I have to import this utility class right? I dont think i can invoke the method,if im in b.java using className.methodName?? can I?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: invoking JOptionPane from user-defined class

 
1
  #9
Mar 11th, 2009
Yes, you can. You do have to import the class, but you don't have to instantiate it. Static methods are invoked directly from the class name.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: invoking JOptionPane from user-defined class

 
0
  #10
Mar 13th, 2009
you can instantiate the class, but that won't help your.
if you would do this:
  1. import Messages;
  2.  
  3. public class TestClass{
  4. public static void main(String args[]){
  5. Messages message = new Messages();
  6. String input = message.askInput();
  7. System.out.println("the input = " + input);
  8. }
  9. }
you will get an exception, since you're trying to use a static class in a non-static way.
the correct way would be:
  1. import Messages;
  2.  
  3. public class TestClass{
  4. public static void main(String args[]){
  5. String input = Messages.askInput();
  6. System.out.println("the input = " + input);
  7. }
  8. }

you can use these static methods in any class you want, as long as you either have that Messages-class in the same directory as the class you try to use it in, or if you remember to import the class.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC