Introduction
Suppose you are writing in a text editor like Notepad. You have written a long series of text, but now you realize that you wanted some text to be
in uppercase and by mistake you wrote them in lowercase, then in Notepad there is no feature to correct this. But you may use this software to accomplish this using these simple steps:
- Just cut/copy the text you want to convert into uppercase or lowercase (required
one time only).
- Press F7 to convert text to uppercase letters or F8 converts to lowercase letters (the text is not visible yet).
- Now perform paste operation either by Ctrl + v or by mouse right click + paste.
- Close the main window to exit the app (required one time only).
Using the Code
I will give an overview of the code which has been used to do this:
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JLabel;
Above are the imports which will be needed by the program.
StringSelection
is for storing the updated text to be placed in the clipboard.DataFlavor
objects are constant and never change once instantiated. They represent the data format.Transferable
can be used to provide data for a transfer operation.
Now we will define the program:
public class TextConverter extends JFrame
{
public native int getKey ();
static
{
System . loadLibrary ("TextConverter");
}
Here:
- We have defined a native method also whose function is
getKey()
. The purpose of this is to log keypress and determine when user presses F7, F8, F10, F12. - We load the DLL which logs the key.
public static void main(String args[])
{
JFrame textconverter=new JFrame("Text Converter");
textconverter.setSize(700,700);
JLabel startinfo0=new JLabel("The Software has Started Running...");
JLabel startinfo=new JLabel("F10 --> Hide this screen");
JLabel startinfo1=new JLabel("F12 --> Unhide the Screen");
JLabel startinfo2=new JLabel("Steps for Usage:");
JLabel startinfo3=new JLabel("1)Just cut/copy the text you want to convert into uppercase or lowercase");
JLabel startinfo4=new JLabel("2)Press F7 to convert text to Uppercase letters " +
"or F8 converts to Lowercase letters(The text is not visible yet).");
JLabel startinfo5=new JLabel("3)Now perform paste operation either by ctrl+v or by mouse right click+paste");
JLabel startinfo6=new JLabel("4)Close this window to exit the app.");
JLabel blank=new JLabel("");
JLabel startinfo7=new JLabel("Software has been Developed by....");
JLabel startinfo8=new JLabel("Anurag Jain");
JLabel startinfo9=new JLabel("Software Engineer");
JLabel startinfo10=new JLabel("(cs.anurag.jain@gmail.com)");
JLabel startinfo11=new JLabel("Project Homepage: https://sourceforge.net/projects/caseconverter/");
JLabel startinfo12=new JLabel("For any problems or feedback " +
"you may contact me directly at cs.anurag.jain@gmail.com");
startinfo.setVisible(true);
textconverter.add(startinfo);
textconverter.add(startinfo1);
textconverter.add(startinfo2);
textconverter.add(startinfo3);
textconverter.add(startinfo4);
textconverter.add(startinfo5);
textconverter.add(startinfo6);
textconverter.add(blank);
textconverter.add(blank);
textconverter.add(blank);
textconverter.add(startinfo7);
textconverter.add(startinfo8);
textconverter.add(startinfo9);
textconverter.add(startinfo10);
textconverter.add(startinfo11);
textconverter.add(startinfo12);
textconverter.setVisible(true);
textconverter.getContentPane().setLayout(new GridLayout(16,2));
textconverter.setDefaultCloseOperation(EXIT_ON_CLOSE);
Here:
- I have added a
JFrame
. This JFrame
will be used to guide user on how to use this software.
while(true)
{
int value=new TextConverter().getKey();
Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
try {
if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
String text = (String)t.getTransferData(DataFlavor.stringFlavor);
if(value==118)
text=text.toUpperCase();
if(value==119)
text=text.toLowerCase();
if(value==121)
textconverter.setVisible(false);
if(value==123)
textconverter.setVisible(true);
StringSelection ss = new StringSelection(text);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
}
} catch (UnsupportedFlavorException e) {
} catch (IOException e) {
}
}
}
}
Here:
- We run an infinite loop because we want the software to run as a background service.
- Now we obtain the key which is pressed by the user using the
get
method. The
get
method is defined in TextConverter.dll. - We make use of
Toolkit
to obtain the contents from the clipboard and store them in the transferable object. - We check whether our transferable object supports
stringFlavor
. - Now we obtain the clipboard data from the
Transferable
object making use of
getTransferData(DataFlavor.stringFlavor)
. - Now we check which key has been pressed. If it is F7 (118) then we convert text into uppercase, otherwise if it's F8 (119),
then lowercase. In case of F10 (121), we hide screen, and for F12 (123), we show the frame.
- After this we rewrite the clipboard with this new modified data.
- Now if the data you copied was xyz, then after this program runs, the data will become XYZ.
Full source code:
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class TextConverter extends JFrame
{
public native int getKey ();
static
{
System . loadLibrary ("TextConverter");
}
public static void main(String args[])
{
JFrame textconverter=new JFrame("Text Converter");
textconverter.setSize(700,700);
JLabel startinfo0=new JLabel("The Software has Started Running...");
JLabel startinfo=new JLabel("F10 --> Hide this screen");
JLabel startinfo1=new JLabel("F12 --> Unhide the Screen");
JLabel startinfo2=new JLabel("Steps for Usage:");
JLabel startinfo3=new JLabel("1)Just cut/copy the text you want to convert into uppercase or lowercase");
JLabel startinfo4=new JLabel("2)Press F7 to convert text to Uppercase letters " +
"or F8 converts to Lowercase letters(The text is not visible yet).");
JLabel startinfo5=new JLabel("3)Now perform paste operation either by ctrl+v or by mouse right click+paste");
JLabel startinfo6=new JLabel("4)Close this window to exit the app.");
JLabel blank=new JLabel("");
JLabel startinfo7=new JLabel("Software has been Developed by....");
JLabel startinfo8=new JLabel("Anurag Jain");
JLabel startinfo9=new JLabel("Software Engineer");
JLabel startinfo10=new JLabel("(cs.anurag.jain@gmail.com)");
JLabel startinfo11=new JLabel("Project Homepage: https://sourceforge.net/projects/caseconverter/");
JLabel startinfo12=new JLabel("For any problems or feedback you " +
"may contact me directly at cs.anurag.jain@gmail.com");
startinfo.setVisible(true);
textconverter.add(startinfo);
textconverter.add(startinfo1);
textconverter.add(startinfo2);
textconverter.add(startinfo3);
textconverter.add(startinfo4);
textconverter.add(startinfo5);
textconverter.add(startinfo6);
textconverter.add(blank);
textconverter.add(blank);
textconverter.add(blank);
textconverter.add(startinfo7);
textconverter.add(startinfo8);
textconverter.add(startinfo9);
textconverter.add(startinfo10);
textconverter.add(startinfo11);
textconverter.add(startinfo12);
textconverter.setVisible(true);
textconverter.getContentPane().setLayout(new GridLayout(16,2));
textconverter.setDefaultCloseOperation(EXIT_ON_CLOSE);
while(true)
{
int value=new TextConverter().getKey();
Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
try {
if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
String text = (String)t.getTransferData(DataFlavor.stringFlavor);
if(value==118)
text=text.toUpperCase();
if(value==119)
text=text.toLowerCase();
if(value==121)
textconverter.setVisible(false);
if(value==123)
textconverter.setVisible(true);
StringSelection ss = new StringSelection(text);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
}
} catch (UnsupportedFlavorException e) {
} catch (IOException e) {
}
}
}
}
Points of Interest
This software runs in the background, so there is no GUI (one screen but you can hide it). Now it is too easy to convert cases with this software.
The best thing is it is not dependent on any editor or screen, it will work anywhere.