Pages

Ads 468x60px

Tuesday, March 12, 2013

Java: Show Urdu Text on Swing/AWT/SWT Components




This code will Show Urdu Text on Swing/AWT/SWT Components. You will have to use unicode values for Urdu characters.


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Factory extends JFrame implements ActionListener{

private static final long serialVersionUID = 1L;
JPanel bottomPanel = new JPanel();
JPanel centerPanel = new JPanel();

JLabel lblDate = new JLabel();
JLabel lblSale = new JLabel("\u06A9\u0644 \u0641\u0631\u0648\u062E\u062A :          ");
JLabel lblSpent = new JLabel("\u06A9\u0644 \u0631\u0642\u0645 \u062E\u0631\u06CC\u062F \u067E\u0631 \u062E\u0631 \u0686 :          ");
JLabel lblDailywages = new JLabel("\u062F\u06CC\u06C1\u0627 \u0691 \u06CC \u062F\u0627\u0631 \u06A9\u06CC \u062F\u06CC\u06C1\u0627 \u0691 \u06CC :          ");
JLabel lblOtherExpenses = new JLabel("\u062F\u0648\u0633\u0631\u06D2 \u0627\u062E\u0631\u0627\u062C\u0627\u062A :          ");
JLabel lblBankDeposit = new JLabel("\u0628\u06CC\u0646\u06A9 \u0645\u06CC\u06BA \u062C\u0645\u0639 :          ");

JTextField txtSale = new JTextField();
JTextField txtSpent = new JTextField();
JTextField txtDailywages = new JTextField();
JTextField txtOtherExpenses = new JTextField();
JTextField txtBankDeposit = new JTextField();

JButton btnSave = new JButton("\u0645\u062D\u0641\u0648\u0638 \u06A9\u0631 \u0648 ");

public Factory() {

lblSale.setFont(new Font("Segoe UI",1,16));
lblSpent.setFont(new Font("Segoe UI",1,16));
lblOtherExpenses.setFont(new Font("Segoe UI",1,16));
lblDailywages.setFont(new Font("Segoe UI",1,16));
lblBankDeposit.setFont(new Font("Segoe UI",1,16));
lblDate.setFont(new Font("Segoe UI",1,16));

txtSale.setPreferredSize(new Dimension(200, 30));
txtSpent.setPreferredSize(new Dimension(200, 30));
txtOtherExpenses.setPreferredSize(new Dimension(200, 30));
txtDailywages.setPreferredSize(new Dimension(200, 30));
txtBankDeposit.setPreferredSize(new Dimension(200, 30));

GridLayout layout = new GridLayout(5,1);
                layout.setVgap(8);
                centerPanel.setLayout(layout);
     
JPanel saleGridPanel = new JPanel(new GridLayout(1,2));
JPanel salePanel = new JPanel();
saleGridPanel.add(txtSale);
saleGridPanel.add(lblSale);
salePanel.add(saleGridPanel);
centerPanel.add(salePanel);

JPanel spentGridPanel = new JPanel(new GridLayout(1,2));
JPanel spentPanel = new JPanel();
spentGridPanel.add(txtSpent);
spentGridPanel.add(lblSpent);
spentPanel.add(spentGridPanel);
centerPanel.add(spentPanel);

JPanel wagesGridPanel = new JPanel(new GridLayout(1,2));
JPanel wagesPanel = new JPanel();
wagesGridPanel.add(txtDailywages);
wagesGridPanel.add(lblDailywages);
wagesPanel.add(wagesGridPanel);
centerPanel.add(wagesPanel);

JPanel expensesGridPanel = new JPanel(new GridLayout(1,2));
JPanel expensesPanel = new JPanel();
expensesGridPanel.add(txtOtherExpenses);
expensesGridPanel.add(lblOtherExpenses);
expensesPanel.add(expensesGridPanel);
centerPanel.add(expensesPanel);

JPanel bankGridPanel = new JPanel(new GridLayout(1,2));
JPanel bankPanel = new JPanel();
bankGridPanel.add(txtBankDeposit);
bankGridPanel.add(lblBankDeposit);
bankPanel.add(bankGridPanel);
centerPanel.add(bankPanel);

bottomPanel.add(btnSave);
btnSave.addActionListener(this);

setTitle("\u0641\u06CC\u06A9\u0679\u0631\u06CC ");
                setPreferredSize(new Dimension(500,600));
                setLayout(new BorderLayout());
getContentPane().add(centerPanel, BorderLayout.CENTER);
getContentPane().add(bottomPanel, BorderLayout.SOUTH);
                pack();
                setDefaultCloseOperation(EXIT_ON_CLOSE);
                setVisible(true);
                setLocationRelativeTo(null);
}

public static void main(String[] args){
new Factory();
}

@Override
public void actionPerformed(ActionEvent e) {

if(e.getSource() == btnSave){
javax.swing.JOptionPane.showMessageDialog(null,"Saved", "Saved", javax.swing.JOptionPane.INFORMATION_MESSAGE);
}

}

}

References:
For Urdu text use Google Translator http://translate.google.com/
For Unicode Conversion http://rishida.net/tools/conversion/

Monday, March 11, 2013

Java: Play Background Music



You can use following code to play background music in your Java application. You will need to add 'jl1.0.jar' in your Java project. You can download it from
http://www.javazoom.net/javalayer/sources.html


BackgroundMusic.java:


import java.io.BufferedInputStream;
import java.io.FileInputStream;
import javazoom.jl.player.Player;

public class BackgroundMusic {

    private String filename;
    private static Player player;
    Thread playMusic;
    // constructor that takes the name of an MP3 file

    public BackgroundMusic(String filename) {
        this.filename = filename;
    }

    // play the MP3 file to the sound card
    public void play() {
        try {
            FileInputStream fis = new FileInputStream(filename);
            BufferedInputStream bis = new BufferedInputStream(fis);
            player = new Player(bis);
        } catch (Exception e) {
            System.out.println("Problem playing file " + filename);
            System.out.println(e);
        }
    }

    public void start() {
        play();
        playMusic = new Thread(new PlayMusic());
        playMusic.start();
    }

    public void stop() {
        close();
        playMusic = null;
    }

    public void close() {
        if (player != null) {
            player.close();
        }
    }

    class PlayMusic implements Runnable {

        public void run() {
            try {
                player.play();
            }
            catch (Exception e) {
                System.out.println(e);
            }
        }
    }
}


TestBackgroundMusic.java:

import java.util.logging.Level;
import java.util.logging.Logger;

public class TestBackgroundMusic {
    
    public static void main(String[] args){
        
        try {
            BackgroundMusic bm = new BackgroundMusic("FileName.mp3");
            bm.start();
            Thread.sleep(10000);
            bm.stop();
        } catch (InterruptedException ex) {
            System.out.println(ex);
        }
    }
    
}

IBM: Java com.tivoli.pd.jcfg.SvrSslCfg Utility




This (com.tivoli.pd.jcfg.SvrSslCfg) utility configures your application in secure domain which is required for communication between TAM-eb and your application. I will configure and run a demo application. These are prerequisites for running the utility:

* WAS, LDAP (TDS) and WebSEAL are configured (Tested by running)
* Java installed
Note: Run these commands on the terminal

$java -version (Java Runtime version)
$javac -version (Java compiler version)

Both should be same like
Runtime: java version "1.6.0_32-ea"
              Java(TM) SE Runtime Environment (build 1.6.0_32-ea-b01)

Compile: javac 1.6.0_32-ea

* Set System Environmental Variables in $HOME/.bashrc or /root/.bashrc like


  PATH=/usr/java/jdk1.6.0/bin:$PATH
  export JAVA_HOME=/usr/java/jdk1.6.0
  export CLASSPATH=/usr/java/jdk1.6.0/lib/tools.jar:./

If you get following exception while running com.tivoli.pd.jcfg.SvrSslCfg utility
Exception: noclassdeffounderror com.ibm.security.x509.x509certimpl
OR
 PD.properties can't be loaded

Solution: 

* Run this command on terminal
$export PATH=/opt/IBM/TDI/V6.1.1/jvm/jre/bin:$PATH

* Run this command on terminal
$which java

Output should be: /opt/IBM/TDI/V6.1.1/jvm/jre/bin

* Run this command on terminal
$/opt/PolicyDirector/sbin/pdjrtecfg -action config -host tameb -port 7135 -java_home /usr/j2se/jre -domain Default -config_type full


Output should be: Configuration of Access Manager Runtime for Java is in progress.
This might take several minutes.
Configuration of Access Manager Runtime for Java completed successfully.

* In $HOME/.bashrc or /root/.bashrc, add following line:

export CLASSPATH=/usr/java/jre1.6.0_30/lib/ext/PD.jar:$CLASSPATH

* Run this command on terminal
$echo $CLASSPATH

Output should be: /usr/java/jre1.6.0_30/lib/ext/PD.jar:/usr/java/jdk1.6.0_32/lib/tools.jar:./

* Run this command on the terminal
$cat /opt/PolicyDirector/etc/pdjrte_paths

Output should be: /opt/IBM/WebSphere/AppServer/java/jre
/usr/java/jre1.6.0_30

Run com.tivoli.pd.jcfg.SvrSslCfg Utility


Configure Your Application:
$java -Dpd.cfg.home=/opt/IBM/WebSphere/AppServer/java/jre com.tivoli.pd.jcfg.SvrSslCfg -action config -admin_id sec_master -admin_pwd object00 -appsvr_id tameb -port 8880 -mode local -policysvr tameb:7135:1 -authzsvr tameb:7136:1
-cfg_file /opt/PolicyDirector/example/pdadminapi_demo/java/configuration/app.properties
-key_file /opt/PolicyDirector/example/pdadminapi_demo/java/keyfile/app.ks -cfg_action create

Note:
* If app.ks file already exists then value for -cfg_action will be 'replace'
* If you have already configured an application, then before configuring another you have to unconfig previous one using this command

Unconfig Your Application:
$java -Dpd.cfg.home=/opt/IBM/WebSphere/AppServer/java/jre com.tivoli.pd.jcfg.SvrSslCfg -action unconfig -admin_id sec_master -admin_pwd object00 -appsvr_id tameb -policysvr tameb:7135:1 -cfg_file /opt/PolicyDirector/example/pdadminapi_demo/java/configuration/app.properties

Now Run your demo application using this command
$java PDAdminDemo sec_master file:///opt/PolicyDirector/example/pdadminapi_demo/java/configuration/app.properties c=us

Note:
* Your configuration string should start like this, 'file:///' (config_str = file:///) or you will get something like specified protocol error.

Exceptions: 

java.lang.ClassNotFoundException: com.ibm.crypto.fips.provider.IBMJCEFIPS
java.lang.ClassNotFoundException: com.ibm.crypto.provider.IBMJCE
java.lang.ClassNotFoundException: com.ibm.security.util.DerValue
java.lang.ClassNotFoundException: com.ibm.jsse2.IBMJSSEProvider2

Solution: 

Run these commands on terminal or add these lines in $HOME/.bashrc or /root/.bashrc

export CLASSPATH=/usr/java/jre1.6.0_30/lib/ext/ibmjcefips.jar:$CLASSPATH
export CLASSPATH=/usr/java/jre1.6.0_30/lib/ext/ibmjceprovider.jar:$CLASSPATH
export CLASSPATH=/usr/java/jre1.6.0_30/lib/ext/ibmpkcs.jar:$CLASSPATH
export CLASSPATH=/usr/java/jre1.6.0_30/lib/ext/ibmjsseprovider2.jar:$CLASSPATH

Exception: 

"HPDJA0109W A nonnull value is being passed to an unsupported argument" is thrown while creating a user using PDUser.createUser 

OR


java.util.MissingResourceException: can't find bundle for base name locale english US:
PDUser.CreateUser()

Solution:

While creating a PD user, value for the description should be null.

Helping Commands:

* Check Servers Status
$pd_start status

* To view process ports
$$lsof -i

References: 

http://www-01.ibm.com/support/docview.wss?uid=swg21284688