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..

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

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:

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);}

}
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:

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);}

}

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

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,

# 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...

That is exactly what the static methods on the utility class that stultuske suggested above would do for you.

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(...)
???

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

String response = Messages.askInput("Enter some data");

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/tutorial/java/javaOO/classvars.html

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?

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.

you can instantiate the class, but that won't help your.
if you would do this:

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);
  }
}

you will get an exception, since you're trying to use a static class in a non-static way.
the correct way would be:

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.