Source Code : Creating a Custom Editing Command for a JTextComponent
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
Creating a Custom Editing Command for a JTextComponent
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import javax.swing.text.TextAction;
public class Main {
public static void main(String[] argv) throws Exception {
JTextArea comp = new JTextArea();
String actionName = "Lowercase";
JFrame f = new JFrame();
f.add(new JScrollPane(comp));
f.setSize(300,300);
f.setVisible(true);
comp.getInputMap().put(KeyStroke.getKeyStroke("F2"), actionName);
comp.getActionMap().put(actionName, new TextAction(actionName) {
public void actionPerformed(ActionEvent evt) {
JTextComponent comp = getTextComponent(evt);
if (comp.getSelectionStart() == comp.getSelectionEnd()) {
if (comp.getCaretPosition() < comp.getDocument().getLength()) {
try {
int pos = comp.getCaretPosition();
Document doc = comp.getDocument();
String str = doc.getText(pos, 1).toLowerCase();
doc.remove(pos, 1);
doc.insertString(pos, str, null);
comp.moveCaretPosition(pos + 1);
} catch (Exception e) {
System.out.println();
}
}
} else {
int s = comp.getSelectionStart();
int e = comp.getSelectionEnd();
comp.replaceSelection(comp.getSelectedText().toLowerCase());
comp.select(s, e);
}
}
});
}
}
Thank with us