Example usage for java.text MessageFormat MessageFormat

List of usage examples for java.text MessageFormat MessageFormat

Introduction

In this page you can find the example usage for java.text MessageFormat MessageFormat.

Prototype

public MessageFormat(String pattern) 

Source Link

Document

Constructs a MessageFormat for the default java.util.Locale.Category#FORMAT FORMAT locale and the specified pattern.

Usage

From source file:MessageFormatReuse.java

public static void main(String args[]) {
    String pattern = "{0}K was deleted on {1}.";
    MessageFormat formatter = new MessageFormat(pattern);

    Double kb = new Double(3.5);
    Date today = new Date();
    Object[] arguments = { kb, today };

    formatter.setLocale(Locale.US);
    System.out.println(formatter.format(arguments));
}

From source file:MessageFormatApp.java

public static void main(String args[]) {
    String pattern = "The time is {0,time} and ";
    pattern += "your lucky number is {1,number}.";
    MessageFormat format = new MessageFormat(pattern);
    Object objects[] = { new Date(), new Integer((int) (Math.random() * 1000)) };
    String formattedOutput = format.format(objects);
    System.out.println(formattedOutput);
}

From source file:MainClass.java

public static void main(String[] argv) {
    String pattern = "{0}K was deleted on {1}.";
    MessageFormat formatter = new MessageFormat(pattern);

    Double kb = new Double(3.5);
    Date today = new Date();
    Object[] arguments = { kb, today };

    formatter.setLocale(Locale.US);
    System.out.println(formatter.format(arguments));

    formatter.setLocale(Locale.FRANCE);
    System.out.println(formatter.format(arguments));

    pattern = "On {1}, {0}K was deleted.";
    formatter.applyPattern(pattern);// ww w  .java 2  s .c  o m
    System.out.println(formatter.format(arguments));

    formatter.setLocale(Locale.US);
    System.out.println(formatter.format(arguments));
}

From source file:JDK6TextComponentDemo.java

public static void main(String[] args) throws Exception {
    final JTextArea textArea = new JTextArea();
    textArea.setText("text");
    JScrollPane jScrollPane = new JScrollPane(textArea);
    final MessageFormat header = new MessageFormat("My Header");
    final MessageFormat footer = new MessageFormat("My Footer");

    JPanel contentPane = new JPanel();
    contentPane.setLayout(new BorderLayout());
    contentPane.add(jScrollPane, BorderLayout.CENTER);

    JFrame frame = new JFrame();
    frame.setTitle("Text-component Printing Demo");
    frame.setSize(400, 200);/*from   w w  w .j  av a  2  s .c  o  m*/
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setContentPane(contentPane);
    frame.setVisible(true);
    textArea.print(header, footer, true, null, null, true);

}

From source file:TablePrintMessageFormat.java

public static void main(String args[]) {
    final Object rows[][] = { { "one", "1" }, { "two", "2" }, { "three", "3" }, { "four", "4" }, { "one", "1" },
            { "two", "2" }, { "three", "3" }, { "four", "4" }, { "one", "1" }, { "two", "2" }, { "three", "3" },
            { "four", "4" }, { "one", "1" }, { "two", "2" }, { "three", "3" }, { "four", "4" },

    };//from   w  w  w .j av a 2s  .com
    final Object headers[] = { "English", "#" };

    JFrame frame = new JFrame("Table Printing");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JTable table = new JTable(rows, headers);
    JScrollPane scrollPane = new JScrollPane(table);
    frame.add(scrollPane, BorderLayout.CENTER);
    JButton button = new JButton("Print");
    ActionListener printAction = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                MessageFormat headerFormat = new MessageFormat("Page {0}");
                MessageFormat footerFormat = new MessageFormat("- {0} -");
                table.print(JTable.PrintMode.FIT_WIDTH, headerFormat, footerFormat);
            } catch (PrinterException pe) {
                System.err.println("Error printing: " + pe.getMessage());
            }
        }
    };
    button.addActionListener(printAction);
    frame.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:TablePrintLastVersion.java

public static void main(String args[]) {
    final Object rows[][] = { { "one", "1" }, { "two", "2" }, { "three", "3" }, { "four", "4" }, { "one", "1" },
            { "two", "2" }, { "three", "3" }, { "four", "4" }, { "one", "1" }, { "two", "2" }, { "three", "3" },
            { "four", "4" }, { "one", "1" }, { "two", "2" }, { "three", "3" }, { "four", "4" },

    };//w w w. j  a v  a 2 s  .c  o  m
    final Object headers[] = { "English", "#" };

    JFrame frame = new JFrame("Table Printing");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JTable table = new JTable(rows, headers);
    JScrollPane scrollPane = new JScrollPane(table);
    frame.add(scrollPane, BorderLayout.CENTER);
    JButton button = new JButton("Print");
    ActionListener printAction = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                MessageFormat headerFormat = new MessageFormat("Page {0}");
                MessageFormat footerFormat = new MessageFormat("- {0} -");
                table.print(JTable.PrintMode.NORMAL, headerFormat, footerFormat, true,
                        new HashPrintRequestAttributeSet(), true);

            } catch (PrinterException pe) {
                System.err.println("Error printing: " + pe.getMessage());
            }
        }
    };
    button.addActionListener(printAction);
    frame.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:ChoiceFormatDemo.java

static void displayMessages(Locale currentLocale) {

    System.out.println("currentLocale = " + currentLocale.toString());
    System.out.println();// w w  w  .  j  a  v a  2s .c o m

    ResourceBundle bundle = ResourceBundle.getBundle("ChoiceBundle", currentLocale);

    MessageFormat messageForm = new MessageFormat("");
    messageForm.setLocale(currentLocale);

    double[] fileLimits = { 0, 1, 2 };

    String[] fileStrings = { bundle.getString("noFiles"), bundle.getString("oneFile"),
            bundle.getString("multipleFiles") };

    ChoiceFormat choiceForm = new ChoiceFormat(fileLimits, fileStrings);

    String pattern = bundle.getString("pattern");
    Format[] formats = { choiceForm, null, NumberFormat.getInstance() };

    messageForm.applyPattern(pattern);
    messageForm.setFormats(formats);

    Object[] messageArguments = { null, "XDisk", null };

    for (int numFiles = 0; numFiles < 4; numFiles++) {
        messageArguments[0] = new Integer(numFiles);
        messageArguments[2] = new Integer(numFiles);
        String result = messageForm.format(messageArguments);
        System.out.println(result);
    }
}

From source file:org.mifos.framework.util.helpers.SearchUtils.java

public static String getMessageWithSubstitution(String bundleName, Locale userLocale, String key,
        Object[] args) {//from  ww  w . j av a 2  s  .c  om
    ResourceBundle resourceBundle = ResourceBundle.getBundle(bundleName, userLocale);
    String label = resourceBundle.getString(key);
    if (args != null) {
        MessageFormat formatter = new MessageFormat(label);
        label = formatter.format(args);
    }
    return label;
}

From source file:com.clustercontrol.platform.util.apllog.EventLogger.java

public static void internal(Integer priority, OutputBasicInfo info) {
    /**     ID,,ID,ID,ID,,? */
    MessageFormat logfmt = new MessageFormat("{0,date,yyyy/MM/dd HH:mm:ss}  {1},{2},{3},{4},{5},{6}");
    // Locale?//from  w w w. ja  va  2s.com
    Locale locale = NotifyUtil.getNotifyLocale();
    //
    Object[] args = { info.getGenerationDate(), info.getPluginId(), info.getApplication(), info.getMonitorId(),
            PriorityConstant.typeToMessageCode(info.getPriority()),
            HinemosMessage.replace(info.getMessage(), locale), info.getMessageOrg() };
    String logmsg = logfmt.format(args);

    switch (priority) {
    case PriorityConstant.TYPE_CRITICAL:
        error(logmsg);
        break;
    case PriorityConstant.TYPE_WARNING:
        warn(logmsg);
        break;
    case PriorityConstant.TYPE_INFO:
        info(logmsg);
        break;
    default:
        break;
    }
}

From source file:com.alifi.jgenerator.utils.StringFormatUtils.java

public StringFormatUtils(String pattern) {
    mf = new MessageFormat(pattern);
}