TextBox Shared ClipBoard : TextBox TextField « J2ME « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » J2ME » TextBox TextFieldScreenshots 
TextBox Shared ClipBoard
TextBox Shared ClipBoard

/*--------------------------------------------------
* SharedClipBoard.java
*
* A List component that can show one of two
* textboxes. Each textbox shares a common clipboard
*
* 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 SharedClipBoard extends MIDlet implements CommandListener
{
  private Display display;    // Reference to Display object
  private List lsMain;       // Main list
  private TextBoxCB tbClip1; // Textbox with clipboard  
  private TextBoxCB tbClip2; // Textbox with clipboard  
  private Command cmExit;    // Command to exit
  
  public SharedClipBoard()
  {
    display = Display.getDisplay(this);

    // Create list
    lsMain = new List("ClipBoard Testing", List.IMPLICIT)
    lsMain.append("TextBox1"null);
    lsMain.append("TextBox2"null);
    
    // Create the exit command, add to list
    cmExit = new Command("Exit", Command.EXIT, 1);
    lsMain.addCommand(cmExit);
    lsMain.setCommandListener(this);
    
    // Allocate textboxes     
    tbClip1 = new TextBoxCB("TextBox1""Email Address:  john@corej2me.com"50, TextField.ANY, this);
    tbClip2 = new TextBoxCB("TextBox2""Web Address:  www."50, TextField.ANY, this);
  }
      
  public void startApp() 
  {
    showList();
  }
  
  public void pauseApp()
  {
  }
     
  public void destroyApp(boolean unconditional)
  {
  }

  public void showList()
  {
    // Display the list component
    display.setCurrent(lsMain);
  }
  
  public void commandAction(Command c, Displayable s)
  {
    // If an implicit list generated the event
    if (c == List.SELECT_COMMAND)
    {
      switch (lsMain.getSelectedIndex())
      {
        case 0:
          // Display textbox 1
          display.setCurrent(tbClip1);
          break;
  
        case 1:
          // Display textbox 2        
          display.setCurrent(tbClip2);
          break;
      }
    }
    else if (c == cmExit)
    {
      destroyApp(false);
      notifyDestroyed();
    
  }
}

/*--------------------------------------------------
* TextBoxCB.java

* A textbox that includes a clipboard. This class
* encapsulates all the commands necessary to show
* a menu for mark, copy, cut and paste.
*
* Example from the book:     Core J2ME Technology
* Copyright John W. Muchow   http://www.CoreJ2ME.com
* You may use/modify for any non-commercial purpose
*-------------------------------------------------*/
class TextBoxCB extends TextBox implements CommandListener
{
  private ClipBoard clipboard;  // The clipboard class
  private Command cmBack;      // Command to go back
  private Command cmStartMark; // Command to start marking a block
  
  private Command cmCopy;      // Command to copy to clipboard
  
  private Command cmCut;       // Command to cut to clipboard 
  
  private Command cmPaste;     // Command to paste into textbox
  private SharedClipBoard midlet; // The midlet
  
  public TextBoxCB(String title, String text, int maxSize, int constraints, 
                   SharedClipBoard midlet)
  {
    // Call the TextBox constructor
    super(title, text, maxSize, constraints);
    
    // Save reference to MIDlet so we can access its methods
    this.midlet =  midlet;
   
    // Create the Commands. Notice the priorities assigned
    cmBack = new Command("Back", Command.BACK, 1);
    cmStartMark = new Command("Mark", Command.SCREEN, 2);
    cmCopy = new Command("Copy", Command.SCREEN, 3);
    cmCut = new Command("Cut",  Command.SCREEN, 4);
    cmPaste = new Command("Paste", Command.SCREEN, 5);
    
    this.addCommand(cmBack);
    this.addCommand(cmStartMark);
    this.addCommand(cmCopy);
    this.addCommand(cmCut);
    this.addCommand(cmPaste);    
    this.setCommandListener(this);

    // Create a clipboard
    clipboard = new ClipBoard(this);
  }
  
  public void commandAction(Command c, Displayable s)
  {
    if (c == cmStartMark)
    {
      // Start to mark a block of text
      clipboard.startMark();  
    }
    else if (c == cmCopy)
    {
      // Copy text to clipboard
      clipboard.copy();
    }
    else if (c == cmCut)
    {
      // Cut text to clipboard
      clipboard.cut();                  
    }
    else if (c == cmPaste)
    
      // Paste from clipboard to textbox
      clipboard.paste();
    }
    else if (c == cmBack)
    {
      // Return to the list component
      midlet.showList();
    }
  }
}

/*--------------------------------------------------
* ClipBoard.java
*
* Core clipboard code -  mark, copy, cut and paste
*
* Example from the book:     Core J2ME Technology
* Copyright John W. Muchow   http://www.CoreJ2ME.com
* You may use/modify for any non-commercial purpose
*-------------------------------------------------*/
class ClipBoard
{
  //--------------------------------------------------
  // Allocate clipboard. Note, both clipboard & count
  // of characters are shared across all instances
  //--------------------------------------------------
  private static char[] _clipBoard = new char[1000];
  private static int _clipBoardCount = 0;
  
  private int beginOffset = 0;  // The start index of copy
  private TextBox tb = null;   // textbox for this instance

  /*--------------------------------------------------
  * Hold reference to textbox that created clipboard
  *-------------------------------------------------*/  
  public ClipBoard(TextBox textbox)
  {
    tb = textbox;
  }

  /*--------------------------------------------------
  * Set starting point of copy
  *-------------------------------------------------*/  
  public void startMark()
  {
    beginOffset = tb.getCaretPosition();
  }

  /*--------------------------------------------------
  * Copy text into the clipboard
  *-------------------------------------------------*/  
  public void copy()
  {
    copy2clipboard();
  }

  /*--------------------------------------------------
  * Copy text into the clipboard. If successful copy,
  * delete text from the textbox.
  *-------------------------------------------------*/  
  public void cut()
  {
    if (copy2clipboard())
      tb.delete(beginOffset, tb.getCaretPosition() - beginOffset);
  }

  /*--------------------------------------------------
  * Do the actual copy into the clipboard
  *-------------------------------------------------*/
  private boolean copy2clipboard()
  {
    // Can only mark (copy/cut) going forward
    if (tb.getCaretPosition() > beginOffset)
    {
      // Allocate an array to hold the current textbox contents
      char[] chr = new char[tb.size()];
      
      // Get the current textbox contents
      tb.getChars(chr);

      // The count of characters in the clipboard      
      _clipBoardCount = tb.getCaretPosition() - beginOffset;
      
      // Copy the text into the clipboard
      // arraycopy(source, sourceindex, dest, destindex, count)
      System.arraycopy(chr, beginOffset, _clipBoard, 0, _clipBoardCount);
      return true;
    }
    else
      return false;    
  }
    
  /*--------------------------------------------------
  * Paste text from clipboard into the textbox 
  *-------------------------------------------------*/  
  public void paste()
  {
    // Make sure the paste will not overrun the textbox length
    if ((tb.size() + _clipBoardCount<= tb.getMaxSize())
      tb.insert(_clipBoard, 0, _clipBoardCount, tb.getCaretPosition());
  }
}


           
       
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. Simple ClipBoardSimple ClipBoard
8. Login MidletLogin Midlet
9. TextBox MIDlet
10. TextBox MIDlet 2
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.