JTextField: cut() : JTextField « javax.swing « Java by API






JTextField: cut()

 

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

public class Main {
  JTextField jtf = new JTextField(15);
  JButton jbtnCut = new JButton("Cut");
  JButton jbtnPaste = new JButton("Paste");
  JButton jbtnCopy = new JButton("Copy");

  public Main() {
    JFrame jfrm = new JFrame("Cut, Copy, and Paste");
    jfrm.setLayout(new FlowLayout());
    jfrm.setSize(230, 150);
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    jbtnCut.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent le) {
        jtf.cut();
        update();
      }
    });

    jbtnPaste.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent le) {
        jtf.paste();
        update();
      }
    });

    jbtnCopy.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent le) {
        jtf.copy();
        update();
      }
    });

    jtf.addCaretListener(new CaretListener() {
      public void caretUpdate(CaretEvent ce) {
        update();
      }
    });

    jfrm.add(jtf);
    jfrm.add(jbtnCut);
    jfrm.add(jbtnPaste);
    jfrm.add(jbtnCopy);
    jfrm.setVisible(true);
  }

  private void update() {
    System.out.println("All text: " + jtf.getText());

    if (jtf.getSelectedText() != null)
      System.out.println("Selected text: " + jtf.getSelectedText());
    else
      System.out.println("Selected text: ");
  }

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

   
  








Related examples in the same category

1.JTextField.LEFT
2.JTextField.LEADING
3.JTextField.TRAILING
4.new JTextField(String text)
5.new JTextField(String text, int columns)
6.JTextField: addActionListener(ActionListener s)
7.JTextField: addFocusListener(FocusListener l)
8.JTextField: addKeyListener(KeyListener l)
9.JTextField: copy()
10.JTextField: getActions()
11.JTextField: getCaretPosition()
12.JTextField: getDocument()
13.JTextField: getHorizontalVisibility()
14.JTextField: getHorizontalVisibility() (2)
15.JTextField: getInputMap()
16.JTextField: getKeymap()
17.JTextField: getScrollOffset()
18.JTextField: paste()
19.JTextField: registerKeyboardAction(ActionListener anAction,String aCommand,KeyStroke aKeyStroke,int aCondition)
20.JTextField: requestFocus()
21.JTextField: modelToView(int pos)
22.JTextField: read(Reader in, Object desc)
23.JTextField: setDocument(Document doc)
24.JTextField: setFocusAccelerator(char aKey)
25.JTextField: setHorizontalAlignment(int alignment)
26.JTextField: setInputVerifier(InputVerifier input)
27.JTextField: setPreferredSize(Dimension preferredSize)
28.JTextField: setNavigationFilter(NavigationFilter filter)
29.JTextField: setTransferHandler(TransferHandler newHandler)
30.JTextField: write(Writer out)