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();
        }
}
My Profile