Add or remove controls from a form : Form « J2ME « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
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 Tutorial » J2ME » Form 
31. 3. 5. Add or remove controls from a form
import java.io.IOException;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.Ticker;
import javax.microedition.midlet.MIDlet;

public class MIDPApplication extends MIDlet implements CommandListener {
  Ticker newsTicker = new Ticker("Java J2ME");

  private List menuList = new List(null, List.IMPLICIT, menuItem, menuImage);

  private List luckyList;

  private List commList;

  private Command exitCommand = new Command("Exit", Command.EXIT, 1);

  private Command execCommand = new Command("Exe", Command.OK, 1);

  private Command menuCommand = new Command("Main", Command.SCREEN, 1);

  private Command commCommand = new Command("Contact", Command.SCREEN, 1);

  private Command luckyCommand = new Command("Plan", Command.SCREEN, 1);

  private Command saveCommand;

  String[] menuItem = "Contact""Plan" };

  String[] commItem = "Add""Search""Edit""Delete" };

  String[] luckyItem = "A""B""C""D""E""F""G""H""I""J""K""L" };

  Image[] menuImage = createImage("/S.png"), createImage("/T.png") };

  private Display display = Display.getDisplay(this);

  private String currentScreen = "";

  public void startApp() {
    menuList.setTicker(newsTicker);
    menuList.addCommand(exitCommand);
    menuList.setCommandListener(this);
    display.setCurrent(menuList);
    currentScreen = "Main";

  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
  }

  public void commandAction(Command c, Displayable s) {
    if (c == exitCommand) {
      destroyApp(false);
      notifyDestroyed();
    }

    if (c == List.SELECT_COMMAND) {
      if (currentScreen == "Main") {
        int itemIndex = menuList.getSelectedIndex();
        switch (itemIndex) {
        case 0{
          doCommunication();
          break;
        }
        case 1:
          doLucky();
        }
      else if (currentScreen == "Contact") {
        int itemIndex = commList.getSelectedIndex();
        switch (itemIndex) {
        case 0{
          doAppend();
          break;
        }
        case 1{
          doQuery();
          break;
        }
        case 2{
          doModification();
          break;
        }
        case 3{
          doDelete();
          break;
        }
        }

      else {

      }
    }

    if (c == menuCommand) {
      display.setCurrent(menuList);
      currentScreen = "Main";
    }

    if (c == commCommand) {
      doCommunication();

    }

    if (c == luckyCommand) {
      doLucky();
    }

    if (c == menuCommand) {
      doMenu();

    }

    if (c == execCommand) {
      if (currentScreen == "Plan") {
        showLucky();
      }
    }
  }

  private Image createImage(String name) {
    Image aImage = null;
    try {
      aImage = Image.createImage(name);
    catch (IOException e) {
    }
    return aImage;
  }

  private void doMenu() {
    currentScreen = "Main";
    display.setCurrent(menuList);
  }

  private void doCommunication() {
    Image[] commIcon = createImage("/S.png"), createImage("/Sk.png"),
        createImage("/M.png"), createImage("/T.png") };

    commList = new List("Contact", List.IMPLICIT, commItem, commIcon);

    commList.addCommand(luckyCommand);
    commList.addCommand(menuCommand);
    commList.setCommandListener(this);
    currentScreen = "Contact";
    display.setCurrent(commList);
  }

  private void doLucky() {
    luckyList = new List("Plan", List.EXCLUSIVE, luckyItem, null);

    luckyList.addCommand(commCommand);

    luckyList.addCommand(menuCommand);
    luckyList.addCommand(execCommand);
    luckyList.setCommandListener(this);
    currentScreen = "Plan";
    display.setCurrent(luckyList);
  }

  private void showLucky() {
    int selectedItem = luckyList.getSelectedIndex();
    Alert alert;
    String information = "";
    switch (selectedItem) {
    case 0{
      information = "A";
      break;
    }
    case 1{
      information = "B";
      break;
    }
    case 2{
      information = "C";
      break;
    }
    case 3{
      information = "D";
      break;
    }
    case 4{
      information = "E";
      break;
    }
    case 5{
      information = "F";
      break;
    }
    case 6{
      information = "G";
      break;
    }
    case 7{
      information = "H";
      break;
    }
    case 8{
      information = "I";
      break;
    }
    case 9{
      information = "J";
      break;
    }
    case 10{
      information = "K";
      break;
    }
    case 11{
      information = "L";
      break;
    }
    }

    alert = new Alert("Info", information, null, AlertType.INFO);
    alert.setTimeout(Alert.FOREVER);
    display.setCurrent(alert);
  }

  private void doAppend() {
    Form appendForm = new Form("Add");
    saveCommand = new Command("Save", Command.SCREEN, 1);
    TextField nameField = new TextField("Name", null, 10, TextField.ANY);
    TextField EMailField = new TextField("E Mail", null, 10, TextField.EMAILADDR);
    TextField ageField = new TextField("Age", null, 10, TextField.NUMERIC);
    appendForm.append(nameField);
    appendForm.append(EMailField);
    appendForm.append(ageField);
    appendForm.addCommand(saveCommand);
    appendForm.addCommand(commCommand);
    appendForm.addCommand(luckyCommand);
    appendForm.setCommandListener(this);
    currentScreen = "ContactAdd";
    display.setCurrent(appendForm);
  }

  private void doModification() {
    Form modificationForm = new Form("Edit");
    TextField nameField = new TextField("Name", null, 10, TextField.ANY);
    modificationForm.append(nameField);
    modificationForm.addCommand(execCommand);
    modificationForm.addCommand(commCommand);
    modificationForm.addCommand(luckyCommand);
    modificationForm.setCommandListener(this);
    currentScreen = "ContactEdit";
    display.setCurrent(modificationForm);
  }

  private void doQuery() {
    Form queryForm = new Form("Search");
    TextField nameField = new TextField("Name", null, 10, TextField.ANY);
    queryForm.append(nameField);
    queryForm.addCommand(execCommand);
    queryForm.addCommand(commCommand);
    queryForm.addCommand(luckyCommand);
    queryForm.setCommandListener(this);
    currentScreen = "ContactSearch";
    display.setCurrent(queryForm);
  }

  private void doDelete() {
    Form deleteForm = new Form("Delete");
    TextField nameField = new TextField("Name", null, 10, TextField.ANY);
    deleteForm.append(nameField);
    deleteForm.addCommand(execCommand);
    deleteForm.addCommand(commCommand);
    deleteForm.addCommand(luckyCommand);
    deleteForm.setCommandListener(this);
    currentScreen = "ContactDelete";
    display.setCurrent(deleteForm);
  }
}
31. 3. Form
31. 3. 1. Form demoForm demo
31. 3. 2. extends Form to create a new formextends Form to create a new form
31. 3. 3. Add controls to formAdd controls to form
31. 3. 4. GUI testGUI test
31. 3. 5. Add or remove controls from a form
31. 3. 6. creates a Form with two items, an interactive Gauge and a StringItemcreates a Form with two items, an interactive Gauge and a StringItem
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.