943,852 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2079
  • Java RSS
Mar 11th, 2009
-1

invoking JOptionPane from user-defined class

Expand Post »
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..
Java Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Caled is offline Offline
22 posts
since Jul 2007
Mar 11th, 2009
0

Re: invoking JOptionPane from user-defined class

Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 935
Solved Threads: 356
Nearly a Posting Maven
stultuske is offline Offline
2,497 posts
since Jan 2007
Mar 11th, 2009
0

Re: invoking JOptionPane from user-defined class

Click to Expand / Collapse  Quote originally posted by stultuske ...
Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  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
Java Syntax (Toggle Plain Text)
  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,
Java Syntax (Toggle Plain Text)
  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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Caled is offline Offline
22 posts
since Jul 2007
Mar 11th, 2009
0

Re: invoking JOptionPane from user-defined class

That is exactly what the static methods on the utility class that stultuske suggested above would do for you.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Mar 11th, 2009
0

Re: invoking JOptionPane from user-defined class

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(...)
???
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Caled is offline Offline
22 posts
since Jul 2007
Mar 11th, 2009
0

Re: invoking JOptionPane from user-defined class

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
Java Syntax (Toggle Plain Text)
  1. String response = Messages.askInput("Enter some data");
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Mar 11th, 2009
0

Re: invoking JOptionPane from user-defined class

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
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Mar 11th, 2009
0

Re: invoking JOptionPane from user-defined class

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Caled is offline Offline
22 posts
since Jul 2007
Mar 11th, 2009
0

Re: invoking JOptionPane from user-defined class

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Mar 13th, 2009
0

Re: invoking JOptionPane from user-defined class

you can instantiate the class, but that won't help your.
if you would do this:
Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 935
Solved Threads: 356
Nearly a Posting Maven
stultuske is offline Offline
2,497 posts
since Jan 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Java Progamming with Z39.50 attributes
Next Thread in Java Forum Timeline: <identifier>expecter ERROR





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC