Right Click JTextField cut copy paste

In this post you can learn an example on how to implement right click option for cut, copy and paste in a JTextField in java swings. This concept is used in many swing application development.

      JTextField textfield;
      textfield = new JTextField(15);
    
      JPopupMenu popup = new JPopupMenu();
      JMenuItem item = new JMenuItem(new DefaultEditorKit.CutAction());
      item.setText("Cut");
      popup.add(item);
      item = new JMenuItem(new DefaultEditorKit.CopyAction());
      item.setText("Copy");
      popup.add(item);
      item = new JMenuItem(new DefaultEditorKit.PasteAction());
      item.setText("Paste");
      popup.add(item);
      textfield.setComponentPopupMenu(popup);

Change value of static variable by method

Actually we have learned that static variable does not change its value and it is not possible to change the value of a static variable. But in this post you can learn an example in java on how to change the value of a static variable by using a method in java.

InitializeByMethod.java

package com.udhaya;

class StaticDemo {
        static String name;
public static void staticVariable() {
        name = name + " " + "kumar";
        System.out.println("Value of static variable after method calling"
                + name);
        }
}
public class InitializeByMethod {
public static void main(String[] args) {
        System.out.println("Initial value of static variable"
                        + StaticDemo.name);
        StaticDemo.name = "udhaya";
        System.out.println("Value of static variable after initialization"
                        + StaticDemo.name);
        StaticDemo.staticVariable();
        }
}

What is the Latest Java Version

 
"What is the latest java version?" is an important question today in majority of the interviews. This post explains you the list of Java Versions.
 
Java Version SE 7
Java Version SE 6
J2SE Version 5.0
J2SE Version 1.4
J2SE Version 1.3
J2SE Version 1.2 
JDK Version 1.1 
JDK Version 1.0
 
Features of Java SE 7

Automatic Null Handling
Binary Literals and underscore in literals
Diamond Syntax
Java nio Package
Multiple Exception Handling
Strings in the Switch Statement
Support for the Dynamic Languages
Type Inference for the Generic Instance Creation
Try with Resources

Java Complete Video Tutorial

                                                          Core Java Video Tutorial

 
 
                                                       Java Collections Video Tutorial

                                    

Restful Services in Java
 
 
Java J2EE
 
 
Servlets
 
 
Hibernate
 
 
Struts 2
 
 
 Spring
 
 

How to get Input from User in Java

import java.util.Scanner;

class getuserinput
{
public static void main(String args[])
{
int a;
float b;
String c;
System.out.println("Enter integer");
a = in.nextInt();
System.out.println("The Value is "+a);
Scanner in = new Scanner(System.in);
System.out.println("Enter float");
b = in.nextFloat();
System.out.println("The Value is "+b);  
System.out.println("Enter string");
c = in.nextLine();
System.out.println("The Value is "+c);
}
}

Java Code to Read Text File

import java.io.*;
class ReadFile
{
public static void main(String args[])
{
try{
FileInputStream fstream = new FileInputStream("C:\\java\\demo\\file.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String Data;
while ((Data = br.readLine()) != null)   {
System.out.println (Data);
}
in.close();
}
catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
}
}
}

Java Code to Send Email

package Javamails;

import javax.mail.*;
import java.util.*;

public class Javamail
{
public static void main(String[] args)throws MessagingException
{
String from = "udhaya1987devtest@gmail.com",
password = "",
host = "smtp.gmail.com",
port = "8000",
subject = "Hi",
message = "Hi Team";
Properties props = new Properties();
props.put("mail.smtp.user", from);
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port",port);
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallb","false");
SecurityManager security = System.getSecurityManager();
try
{
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
MimeMessage msg = new MimeMessage(session);
msg.setText(message);
msg.setSubject(subject);
msg.setFrom(new InternetAddress(from));
msg.addRecipient(Message.RecipientType.TO,new InternetAddress(to);
Transport.send(msg);
}
catch(Exception mex)
{
mex.printStackTrace();
}
}
}

class SMTPAuthenticator extends javax.mail.Authenticator
{
String from = "sender_id@gmail.com",
password = "password",
host = "smtp.gmail.com",
port = "8000",
subject = "Hi",
message = "Hi Team";
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(from, password);
}
}
My Profile