| | |
invoking JOptionPane from user-defined class
Thread Solved |
•
•
Join Date: Jul 2007
Posts: 20
Reputation:
Solved Threads: 0
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..
Thanks
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)
import javax.swing.*; import java.lang.*; public class Joption { public class showFrame{ private String dialogTitle = "CCA"; private int styleInfo = 2; //INFORMATION_MESSAGE type private String messageToUser = ""; private String showFrame = "null"; public void setDialogTitle(String dialogTitle) { this.dialogTitle = dialogTitle; } public String getDialogTitle() { return (this.dialogTitle); } public void setStyleInfo(int styleInfo) { this.styleInfo = styleInfo; } public int getStyleInfo() { return (this.styleInfo); } public void setMessageToUser(String messageToUser) { this.messageToUser= messageToUser; } public String getMessageToUser() { return (this.messageToUser); } public void setShowFrame(String showFrame) { this.showFrame= showFrame; } public String getShowFrame() { return (this.showFrame); } public void showMessageFrame(String showFrame, String messageToUser, String dialogTitle, int infoStyle) { JOptionPane.showMessageDialog(showFrame, messageToUser, dialogTitle, infoStyle); } public void showInputPrompt(String showFrame, String messageToUser, String dialogTitle, int infoStyle) { JOptionPane.showInputPrompt(showFrame, messageToUser, dialogTitle, infoStyle); } } public static void main (String args[]) { JOptionPane.showMessageDialog(null,"Eggs are not supposed to be green.", "Message", JOptionPane.INFORMATION_MESSAGE); String input = JOptionPane.showInputDialog(null, "Please Enter a value", "hello", JOptionPane.QUESTION_MESSAGE); JOptionPane.showMessageDialog(null, "You entered the following text: " + input , "User Input", JOptionPane.INFORMATION_MESSAGE); } }
Thanks
Java Syntax (Toggle Plain Text)
public void showInputPrompt(String showFrame, String messageToUser, String dialogTitle, int infoStyle { JOptionPane.showInputPrompt(showFrame, messageToUser, dialogTitle, infoStyle); }
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)
import javax.swing.JOptionPane; public class Messages { public static String askInput(){ return askInput("Enter your data");} public static String askInput(String message){ return askInput(message, "Default input");} public static String askInput(String message, String defaultInput){ return JOptionPane.showInputDialog(null, message, defaultInput);} }
•
•
Join Date: Jul 2007
Posts: 20
Reputation:
Solved Threads: 0
•
•
•
•
Java Syntax (Toggle Plain Text)
public void showInputPrompt(String showFrame, String messageToUser, String dialogTitle, int infoStyle { JOptionPane.showInputPrompt(showFrame, messageToUser, dialogTitle, infoStyle); }
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)
import javax.swing.JOptionPane; public class Messages { public static String askInput(){ return askInput("Enter your data");} public static String askInput(String message){ return askInput(message, "Default input");} public static String askInput(String message, String defaultInput){ return JOptionPane.showInputDialog(null, message, defaultInput);} }
Java Syntax (Toggle Plain Text)
JOptionPane.showMessageDialog(...)
As for example,
Java Syntax (Toggle Plain Text)
# Filename: B.Java import myClass.showFrame # This showFrame class has all methods # to showMessageDialog and showInputDialog # and local variable declarations public static void main (String args[]) { String msg = "Test"; String winTitle="ABC Co."; int infoStyle = 2; // for JOptionPane.INFORMATION_MESSAGE showFrame aFrame = new showFrame (); aFrame.showMessageDlg(null, msg, title, 2); }
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...
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)
String response = Messages.askInput("Enter some data");
•
•
Join Date: Sep 2008
Posts: 1,568
Reputation:
Solved Threads: 196
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
http://java.sun.com/docs/books/tutor...classvars.html
•
•
Join Date: Jul 2007
Posts: 20
Reputation:
Solved Threads: 0
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?
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?
you can instantiate the class, but that won't help your.
if you would do this:
you will get an exception, since you're trying to use a static class in a non-static way.
the correct way would be:
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.
if you would do this:
Java Syntax (Toggle Plain Text)
import Messages; public class TestClass{ public static void main(String args[]){ Messages message = new Messages(); String input = message.askInput(); System.out.println("the input = " + input); } }
the correct way would be:
Java Syntax (Toggle Plain Text)
import Messages; public class TestClass{ public static void main(String args[]){ String input = Messages.askInput(); System.out.println("the input = " + input); } }
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.
![]() |
Other Threads in the Java Forum
- Previous Thread: Java Progamming with Z39.50 attributes
- Next Thread: <identifier>expecter ERROR
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card chat class client code collision component crashcourse css csv database eclipse ee error fractal free game gis givemetehcodez graphics gui html ide image integer integration j2me japplet java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linux list loan machine map method methods migrate mobile netbeans newbie objects oriented output panel phone physics problem program programming project projects radio recursion replaydirector reporting scanner se server service set sms socket software sort sql string swing test textfield threads transfer tree trolltech ubuntu utility windows






