This page was last modified 23:04, 3 July 2008.
A textbox clipboard
From Forum Nokia Wiki
The following example adds copy and paste (clipboard) operations to a TextBox component.
import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class SimpleClipBoard extends MIDlet implements CommandListener { private Display display; // Reference to Display object private TextBox tbClip; // Main textbox private Command cmExit; // Command to exit private Command cmStartMark; // Command to start marking a block private Command cmCopy; // Command to copy to clipboard private Command cmPaste; // Command to paste into textbox private int beginOffset = 0; // The start index of copy private char[] clipBoard = null; // The clipboard private int clipBoardChars = 0; // Number of chars in clipboard public SimpleClipBoard() { display = Display.getDisplay(this); // Create the Commands. Notice the priorities assigned cmExit = new Command("Exit", Command.EXIT, 1); cmStartMark = new Command("Mark", Command.SCREEN, 2); cmCopy = new Command("Copy", Command.SCREEN, 3); cmPaste = new Command("Paste", Command.SCREEN, 4); tbClip = new TextBox("Clip Board", "Tee to grn", 15, TextField.ANY); tbClip.addCommand(cmExit); tbClip.addCommand(cmStartMark); tbClip.addCommand(cmCopy); tbClip.addCommand(cmPaste); tbClip.setCommandListener(this); // Allocate a clipboard big enough to hold the entire textbox clipBoard = new char[tbClip.getMaxSize()]; } public void startApp() { display.setCurrent(tbClip); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable s) { if (c == cmStartMark) { beginOffset = tbClip.getCaretPosition(); } else if (c == cmCopy && (tbClip.getCaretPosition() > beginOffset)) { // Allocate an array to hold the current textbox contents char[] chr = new char[tbClip.size()]; // Get the current textbox contents tbClip.getChars(chr); // The count of characters in the clipboard clipBoardChars = tbClip.getCaretPosition() - beginOffset; // Copy the text into the clipboard // arraycopy(source, sourceindex, dest, destindex, count) System.arraycopy(chr, beginOffset, clipBoard, 0, clipBoardChars); } else if (c == cmPaste) { // Make sure the paste will not overrun the textbox length. if ((tbClip.size() + clipBoardChars) <= tbClip.getMaxSize()) tbClip.insert(clipBoard, 0, clipBoardChars, tbClip.getCaretPosition()); } else if (c == cmExit) { destroyApp(false); notifyDestroyed(); } } }
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Place 2 text box in 2 different rows - how? | mibuyt | Mobile Java General | 3 | 2003-09-29 11:55 |
| Database in Python S60 | Faisal Rehman | Python | 14 | 2008-07-31 17:29 |
| Add from Contacts | white_dragon | Mobile Java General | 5 | 2007-08-18 14:11 |
| CPeriodic::Start() leads to KERN-EXEC 0 | peter.koellner | General Symbian C++ | 3 | 2004-08-18 21:07 |
| j2me phone can't able to read plain text from servlets - why? | mibuyt | Mobile Java General | 5 | 2003-09-29 13:30 |
