Friday, October 14, 2011

Java Google Translator Using Swing


MyTranslator.java
import java.awt.EventQueue;
import java.lang.reflect.Field;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;

import com.google.api.translate.Language;
import com.google.api.translate.Translate;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class MyTranslator extends JFrame {

private JPanel contentPane;
JComboBox combo_from;
JComboBox combo_to;
Class myClass;
private JLabel lblTo;
JTextArea text_to;


JTextArea text_from;
private JLabel lblLanguage;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try
{
MyTranslator frame = new MyTranslator();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
* @throws ClassNotFoundException
*/
public MyTranslator() throws ClassNotFoundException
{
Translate.setHttpReferrer("translate.google.com");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 487, 261);
contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
this.setTitle("Google Translator..");
combo_to= new JComboBox();
combo_from= new JComboBox();

//load language class for language list
myClass=Class.forName("com.google.api.translate.Language");
Field[] f=myClass.getFields();combo_to.addItem("--Select--");
combo_from.addItem("--Select--");
for (int i = 0; i < f.length; i++) { combo_to.addItem(f[i].getName()); combo_from.addItem(f[i].getName()); } lblTo = new JLabel("TO"); text_to= new JTextArea(); JButton btn_translate = new JButton("TRANSLATE"); btn_translate.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { String translation=Translate.execute(text_to.getText(),Language.valueOf(combo_to.getSelectedItem().toString()),Language.valueOf(combo_from.getSelectedItem().toString())); text_from.setText(translation); } catch (Exception e1) { // TODO Auto-generated catch block //e1.printStackTrace(); JOptionPane.showMessageDialog(contentPane,"Error In Language Translation...","Error",JOptionPane.ERROR_MESSAGE); //JOptionPane.showMessageDialog(, message, title, messageType) } } }); text_from= new JTextArea(); lblLanguage = new JLabel("Language :-"); GroupLayout gl_contentPane = new GroupLayout(contentPane); gl_contentPane.setHorizontalGroup( gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addContainerGap() .addComponent(lblLanguage) .addGap(24) .addComponent(combo_to, GroupLayout.PREFERRED_SIZE, 91, GroupLayout.PREFERRED_SIZE) .addGap(18) .addComponent(lblTo)) .addGroup(gl_contentPane.createSequentialGroup() .addGap(20) .addComponent(text_to, GroupLayout.PREFERRED_SIZE, 187, GroupLayout.PREFERRED_SIZE))) .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addGap(14) .addComponent(combo_from, GroupLayout.PREFERRED_SIZE, 91, GroupLayout.PREFERRED_SIZE) .addGap(18) .addComponent(btn_translate)) .addGroup(gl_contentPane.createSequentialGroup() .addGap(30) .addComponent(text_from, GroupLayout.PREFERRED_SIZE, 179, GroupLayout.PREFERRED_SIZE))) .addGap(32)) ); gl_contentPane.setVerticalGroup( gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addGap(12) .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) .addComponent(combo_to, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(lblTo) .addComponent(combo_from, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(btn_translate) .addComponent(lblLanguage)) .addGap(37) .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addGap(5) .addComponent(text_from, GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)) .addComponent(text_to, GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)) .addContainerGap()) ); contentPane.setLayout(gl_contentPane); } }



Note :- This Program Required google-api-translate-java-0.95.jar but some language translation are not possible...

No comments:

Post a Comment