Java GUI Programming

How to add uniocde to JOptionPane.showInputDialog in java – Uniocde example

This slideshow requires JavaScript.

In this example i will show create Unicode based Input dialog to your application.In here you can see lot of information’s about Unicode based java applications.First of all i had to define Unicode font for my input dialog.Because when i type Sinhala text in my text box which i used to get name from user it showed me only boxes.But after i set default font my application it has solved.See the code

MyFont.setUIFont(new javax.swing.plaf.FontUIResource("Iskoola pota",Font.BOLD,18)); // setting the default font for application

To use that method i have a class and that class look like below

public class MyFont {

    /*

      Below code I extracted from
     http://www.java-forums.org/java-tips/6522-swing-changing-component-default-font.html
      then i customized it.

     */

    public static void setUIFont (javax.swing.plaf.FontUIResource f){

    java.util.Enumeration keys = UIManager.getDefaults().keys();
    while (keys.hasMoreElements()) {
      Object key = keys.nextElement();
      Object value = UIManager.get (key);
      if (value instanceof javax.swing.plaf.FontUIResource)
        UIManager.put (key, f);
      }
    }

}

Then i have to change the text on the JButtons.This is how i did that

// customize the joption pane button font
        UIManager.put("OptionPane.cancelButtonText", "අවලංගු කරන්න");
        UIManager.put("OptionPane.noButtonText", "නැත");
        UIManager.put("OptionPane.okButtonText", "හරි");
        UIManager.put("OptionPane.yesButtonText", "ඔව්");

Then i wanted to set my question messages to my native language

// message display for ask name
      String askName =
            "<html><p><font color=\"#000\" " +
            "size=\"5\" face=\"Iskoola pota\">ඔබගේ නම ඇතුලත් කරන්න" +
            "</font></p></html>";

      // message display for ask age
      String askAge =
            "<html><p><font color=\"#000\" " +
            "size=\"5\" face=\"Iskoola pota\">ඔබගේ වයස ඇතුලත් කරන්න" +
            "</font></p></html>";

      // message display display error message
      String errorMgs =
            "<html><p><font color=\"#000\" " +
            "size=\"5\" face=\"Iskoola pota\">කරුණාකර නිවැරදි වයස ඇතුලත් කරන්න.." +
            "</font></p></html>";

Then I implemented the rest of application like below.See the complete code below

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package joptionpaneinputdialog;
import java.awt.Font;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
/**
 *
 * @author Damith
 */
public class Main {

    public static void main(String[] args)  {

    MyFont.setUIFont(new javax.swing.plaf.FontUIResource("Iskoola pota",Font.BOLD,18)); // setting the default font for application

        // customize the joptin pane button font
        UIManager.put("OptionPane.cancelButtonText", "අවලංගු කරන්න");
        UIManager.put("OptionPane.noButtonText", "නැත");
        UIManager.put("OptionPane.okButtonText", "හරි");
        UIManager.put("OptionPane.yesButtonText", "ඔව්");

       // message display for ask name
      String askName =
            "<html><p><font color=\"#000\" " +
            "size=\"5\" face=\"Iskoola pota\">ඔබගේ නම ඇතුලත් කරන්න" +
            "</font></p></html>";

      // message display for ask age
      String askAge =
            "<html><p><font color=\"#000\" " +
            "size=\"5\" face=\"Iskoola pota\">ඔබගේ වයස ඇතුලත් කරන්න" +
            "</font></p></html>";

      // message display display error message
      String errorMgs =
            "<html><p><font color=\"#000\" " +
            "size=\"5\" face=\"Iskoola pota\">කරුණාකර නිවැරදි වයස ඇතුලත් කරන්න.." +
            "</font></p></html>";

        try {

            // apply new look and feel theme
            javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.aluminium.AluminiumLookAndFeel");

             String name = JOptionPane.showInputDialog(null,
                                              askName,
                                              "දත්ත ඇතුලත් කිරීම",
                                              JOptionPane.QUESTION_MESSAGE);// getting the name

            int age = Integer.parseInt(JOptionPane.showInputDialog(null,
                                              askAge,
                                              "දත්ත ඇතුලත් කිරීම",
                                              JOptionPane.QUESTION_MESSAGE)); // getting the age

      // message output to user.I used it to veryfy that entered font is valied

             String output =
            "<html><p><font color=\"#000\" " +
            "size=\"5\" face=\"Iskoola pota\">"+name+" :" +"ඔබගේ වයස "+age+
            "</font></p></html>";

             JOptionPane.showMessageDialog(null, output,"තොරතුරු පනිවිඩය",JOptionPane.INFORMATION_MESSAGE);

    // display message using joption pane

        } catch (NumberFormatException e) {

            // display error message with uniocde font

            JOptionPane.showMessageDialog(null, errorMgs,"තොරතුරු පනිවිඩය",JOptionPane.ERROR_MESSAGE);

        }

      catch(Exception ex)
        {
          JOptionPane.showMessageDialog(null, ex);
        }

    }

}

I think you can understand what is the process.Same as this you can use this for any type of GUI

20 thoughts on “How to add uniocde to JOptionPane.showInputDialog in java – Uniocde example

  1. මගේ laptop එකේ JDK 7 setup එක run වෙන්නේ වත් නෑ… මම anit-virus එක turn off කරලා, firewall turn off කරලා try කරා… command-prompt එකෙන් run කරාම process එක හැදෙනවා… ඒත් තත්පර කීපයකදී kill වෙනවා… මම මේ දවස්වල සිංහල ටයිපින් ටියුටර් එකක් code කරන්න ඕන… මගේ project එකට… සිංහල හරියට render වෙන්නේ නෑ දැනට use කරන්නේ JDK6 upadte 32.. විසදුමක් කියන්න පුළුවම්ද???

    1. Best JDK for develop Sinhala Unicode applications is JDK 7. Others might have some rendering errors. But i have no idea why you can’t install current JDK.
      1.First Uninstall current JDK.
      2.Restart computer
      3.Download relevant JDK (32 bit or 64 bit)
      4.Install

      If you still have problem there may be a virus attack on your computer. I’m not sure about it.And please check weather your current JDK .exe is corrupted or not .

      1. Thank you for the post. but it doesn’t render the characters such as ‘ප්‍ර’ correctly. Please try using the words such as ප්‍රවීන් with the above mentioned letters.

  2. Great Great work….
    I am in need of your help, Im an amature in Java.
    Im using JavaFX to develop an application and in that I need to insert a TextField or a TextArea that supports english characters and sinhala characters.
    Can you hep me with the code that performs this.

  3. hi Damith’s

    thanks for your post, but i did try it for the hindi language.but it didn’t worked. below is my code for the same. what i am seeing in database are values like कंप्यूटर.
    please help.

    import java.sql.*;
    import java.net.*;

    public class HindiTest
    {
    public static void main(String[] args) throws Exception

    {

    try
    {
    System.out.println(“hi”);
    Class.forName(“com.mysql.jdbc.Driver”);
    //useUnicode=true&characterEncoding=UTF-8
    Connection connection = DriverManager.getConnection(“jdbc:mysql://localhost:3306/hindi_test?useUnicode=true&characterEncoding=UTF-8”, “root”, “canvasm”);

    System.out.println(“connection created”);

    Statement st = connection.createStatement();

    System.out.println(“last query executed”);
    int x = st.executeUpdate(“INSERT INTO hindi VALUES (‘कंप्यूटर’)”);

    ResultSet rs = st.executeQuery(“select * from hindi”);

    while(rs.next())
    {
    String val = new String( rs.getBytes(“data”), “UTF-8”);//rs.getString(“data”);
    //new String( rs.getBytes(“message”), “UTF-8”);
    System.out.println();
    System.out.println(val);
    }

    System.out.println(x);

    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    }
    }

  4. Hi,

    I want to take input in Hindi via JOptionPane.showInputDialog()
    problem is here that when user type in text box it is in English font.

    How can i make it possible to type in any font in Textbox given on inputDialog ?

  5. Hi,
    When I run the abow programe, I can see all the Sinhala unicode characters in lables and buttons, But when I type in that text box it typed English letters even I change my input language to Sinhala. At the same time, when I type in a text editor it typed Sinhala charactors. I use Oracle java 1.8.
    What can I do?

    1. I’m sorry to inform that currently i can’t help you because i don’t have even install jdk on my machine. Please do some R&D on that find by your self. Once you find the solution, post the solution in here. It may help to another one.

  6. hi macho.. oyage me wede nm pattai aaa. mn sinhala jop aka try kala.. ath UIManager akan error akak anawa. put method ake perameter weradi kiyala akata mokada karanne..

    UIManager.put(“OptionPane.cancelButtonTextවලංගු කරන්න”);

  7. මචං මම සිංහලෙන් software එකක් හදනවා එක හදන්න උඹගේ මේ post ටික මාරම support එකක් උනා උඹට පින් බන් නැත්නම් මගේ මේ වැඩේ මග නවතින්න තිබුන උගේ අනාගතේ සාර්ථක වෙන්න කියල හදවතින්ම සුබ පතන බ්‍රෝ

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s