Simple ClipBoard : TextBox TextField « J2ME « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » J2ME » TextBox TextFieldScreenshots 
Simple ClipBoard
Simple ClipBoard


/*--------------------------------------------------
* SimpleClipBoard.java
*
* Example from the book:     Core J2ME Technology
* Copyright John W. Muchow   http://www.CoreJ2ME.com
* You may use/modify for any non-commercial purpose
*-------------------------------------------------*/
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 examples in the same category
1. TextField CaptureTextField Capture
2. Phone BookPhone Book
3. Hello TextBox MIDletHello TextBox MIDlet
4. Hide TextHide Text
5. TextBox CaptureTextBox Capture
6. GUI Test in MIDletGUI Test in MIDlet
7. TextBox Shared ClipBoardTextBox Shared ClipBoard
8. Login MidletLogin Midlet
9. TextBox MIDlet
10. TextBox MIDlet 2
w___w_w___.___ja__v__a_2___s_.c__o___m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.