views:

95

answers:

5

Hello,

i'm looking for a small framework to have all my messages stored in a common way. I'll give an example for better understanding.

In a part of my code, in a particular JFrame i've an alert something like this:

JOptionPane.showMessageDialog(null, "Error, you must provide an integer value", "ERROR", JOptionPane.ERROR_MESSAGE);

So, this string: "Error, you must provide an integer value". I would like to have it in a particular "log", or something like that, so i can do something like this:

JOptionPane.showMessageDialog(null, Messages.getMessage(Messages.INTEGER_VALUE), "ERROR", JOptionPane.ERROR_MESSAGE);

Hard to explain, hope you can help me.

Thanks!

+11  A: 

Sounds like you need a ResourceBundle. It allows you to maintain locale-sensitive user-displayable messages keyed against a code.

It's not an external framework, it's part of the JavaSE API.

skaffman
+1 for straight-to-the-point.
glowcoder
To add to this reply, for a tutorial on ResourceBundle see: http://java.sun.com/docs/books/tutorial/i18n/resbundle/index.html
Great, just what i was looking for! Thanks brother.
santiagobasulto
A: 

Create a static class called "Messages"

Inside this, have a method called getMessage which accepts an integer, and return the correct error message corresponding to the code.

Kyle
A: 

Sounds more like you want a lookup table for your error messages. You could get fancy and actually make a class to do this for you or do something easy:

String errors[] = {"Some error","Some other error"};
JOptionPane.showMessageDialog(null,errors[0],"ERROR", JOptionPane.ERROR_MESSAGE);

or

Map<String,String> errors = new HashMap<String,String>();
errors.put("PROVIDE_INT","Error, you must provide an integer value");
JOptionPane.showMessageDialog(null,errors.get("PROVIDE_INT"),"ERROR", JOptionPane.ERROR_MESSAGE);
JamesG
+2  A: 

I think something like cal10n might be the type of framework you're looking for.

http://cal10n.qos.ch/

Kevin Stembridge
+1  A: 

Or... you can write your own mini messaging utility, like this:-

public class MessageUtil {
    enum Message {
        ERROR_INTEGER_REQUIRED("Error", "Error, you must provide an integer value"), 
        ERROR_STRING_REQUIRED("Error", "Error, you must provide a string value"), 
        ERROR_BLA_BLA("Error", "Error, you are doomed"),
        INFO_DATA_SAVED("Note", "Data is successfully saved");

        String  title;
        String  msg;

        private Message(String title, String msg) {
            this.title = title;
            this.msg = msg;
        }
    }

    public static void display(Message message) {
        JOptionPane.showMessageDialog(null, message.msg, message.title, JOptionPane.ERROR_MESSAGE);
    }
}

Then, you can do something like this:-

MessageUtil.display(ERROR_INTEGER_REQUIRED); 
limc