In my J2ME app, I have some forms, and some threads running on background. If in any of these threads I decide to display a message box or notification bar on top of the app, I have the problem of not knowing in which form I am, therefore I don't know which form to show after the messagebox or notification bar is hidden.
Does anyone have any suggestions?
You can get current form that is already displaying with "Display.getCurrent()".For example this canvas is a SplashScreen that get current form before displays in the screen:
import javax.microedition.lcdui.Canvas;
/* */ import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
/* */ import javax.microedition.lcdui.Graphics;
/* */ import javax.microedition.lcdui.Image;
public class StaticSplashScreen extends Canvas
implements Runnable {
private HelloMIDlet mainMidlet;
private boolean isSplashOver;
private long currentTime;
private long previousTime;
private Form currentForm;
public StaticSplashScreen(HelloMIDlet mid) {
this.mainMidlet = mid;
currentForm = (Form) this.mainMidlet.getDisplay().getCurrent();
this.previousTime = System.currentTimeMillis();
new Thread(this).start();
}
protected void paint(Graphics g) {
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0, 0, 0);
g.drawString("In the name of God", 40, 70, 0);
}
public void run() {
while (!this.isSplashOver) {
this.currentTime = System.currentTimeMillis();
if (this.currentTime - this.previousTime >= 10000L) {
this.isSplashOver = true;
}
}
this.mainMidlet.getDisplay().setCurrent(currentForm);
}
}
In this midlet you can see two forms with some commands.When you press "help" in each form,method() calls and SplashScreen diplays and after 10 seconds you can see the form that launched it again:
public class HelloMIDlet extends MIDlet implements CommandListener {
...
public void commandAction (Command command, Displayable displayable) {
...
if (command == helpCommand) {
method ();
}
...
}
public Form getForm () {
if (form == null) {
form = new Form ("Welcome");
form.addCommand (getHelpCommand());
form.setCommandListener (this);
}
return form;
}
public void method () {
if (true) {
StaticSplashScreen sss = new StaticSplashScreen(this);
this.getDisplay().setCurrent(sss);
} else {
}
}
public Form getForm1 () {
if (form1 == null) {
form1 = new Form ("form1");
form1.addCommand (getHelpCommand ());
form1.setCommandListener (this);
}
return form1;
}
}
A ticker is an object that provides scrolling text across the top of the display. A Ticker is associated with the display, not with the screen. You place a Ticker on a screen using the Screen.setTicker(Ticker t) method, as shown in the code below.
You can associate the same Ticker object with multiple screens, however. The implementation renders the Ticker on some constant portion of the display, in this case at the top of the display. Ticker is not an Item. Its derivation directly from java.lang.Object gives you a clue as to why a Ticker can be tied to the display and not to a screen. It doesn't need to be derived from Item, because it really is not something that is placed in a Form.
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Ticker;
import javax.microedition.lcdui.Form;
/**
This class demonstrates use of the Ticker MIDP UI
component class.
#see javax.microedition.lcdui.Gauge
*/
public class TickerDemo extends Form
implements CommandListener
{
private String str =
"This text keeps scrolling until the demo stops...";
private Ticker ticker = new Ticker(str);
private Command back = new Command("Back", Command.BACK, 1);
private static Displayable instance;
/**
Constructor.
*/
public TickerDemo()
{
super("Ticker demo");
instance = this;
addCommand(back);
setTicker(ticker);
setCommandListener(this);
}
...
}
Hope this will help you. Thanks
Related
EDIT: Forgot the code...
I have an app that let's the user select CSV files for viewing. I'm using JavaFX TableViews to display the data.
For one page, the user can type into a special text box. It's a custom class I made called AutoCompleteTextArea, which extends RichTextFX's StyleClassedTextArea. On other pages, this text box should be hidden. When I have just one TableView, things work fine.
vbox.getChildren().addAll(menuBar, title, subtitle, reqBox, reqTable);
But I need other pages with different TableViews. If I add another TableView to the VBox, my AutoCompleteTextArea goes away!
vbox.getChildren().addAll(menuBar, title, subtitle, reqBox, reqTable, tempTable);
The TableViews do not appear to be overlapping... Any idea why the AutoCompleteTextArea is disappearing? The other weird thing is that if I substitute a regular TextField for the AutoCompleteTextArea, things work fine!
Here's my code. You will need RichTextFX on your build path in order to run it. Use the View Menu to see the problem. The first menu item shows the AutoCompleteTextArea (in the working case). The second menu item shows a different TableView, but this is the broken case - the AutoCompleteTextArea is gone from the first page.
Line 132 is the line in question.
I hope someone is up for the challenge!
More background:
I originally wanted to have just one TableView, and update it's contents based on the user's selection in the View Menu. But I couldn't find a good way to do that, and now here I am again... (see this post: How do I clone a JavaFX TableView?)
package FLOOR;
// --- Imports
import java.util.LinkedList;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import org.fxmisc.richtext.StyleClassedTextArea;
import javafx.application.Application;
import javafx.beans.property.StringProperty;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
// --- Main Class
public class Example extends Application {
// --- All Pages
final Page[] pages = new Page[] {
new Page("Welcome!",
"Welcome Page"),
new Page("Page 1", "Shows Table_1"),
new Page("Page 2", "Shows Table_2"),
};
// --- All Tables
TableView<ObservableList<StringProperty>> reqTable = new TableView<>();
TableView<ObservableList<StringProperty>> tempTable = new TableView<>();
//TextField reqBox = new TextField();
AutoCompleteTextArea reqBox = new AutoCompleteTextArea();
// --- Current Page
final Label title = new Label();
final Label subtitle = new Label();
// --- Main
public static void main(String[] args) { launch(args); }
// --- Start
#Override
public void start(Stage stage) {
// --- Menus
// --- File Menu
// --- Import Submenu
Menu menuFile = new Menu("File");
Menu importMenu = new Menu("Import");
MenuItem reqOption = new MenuItem("Requirements");
MenuItem tempOption = new MenuItem("Templates");
importMenu.getItems().addAll(reqOption, tempOption);
//Import Requirements
reqOption.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
//TODO
}
});
//Import Templates
tempOption.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
//TODO
}
});
//Export
MenuItem export = new MenuItem("Export");
export.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
//TODO
}
});
//Exit
MenuItem exit = new MenuItem("Exit");
exit.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
System.exit(0);
}
});
menuFile.getItems().addAll(importMenu, export, new SeparatorMenuItem(), exit);
// --- View Menu
Menu menuView = new Menu("View");
//Page1
MenuItem viewRequirements = new MenuItem("Requirements");
viewRequirements.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
getPage1();
}
});
//Page2
MenuItem viewTemplates = new MenuItem("Templates");
viewTemplates.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
getPage2();
}
});
menuView.getItems().addAll(viewRequirements, viewTemplates);
// --- Menu Bar
MenuBar menuBar = new MenuBar();
menuBar.getMenus().addAll(menuFile, menuView);
// --- VBox
final VBox vbox = new VBox();
vbox.setAlignment(Pos.TOP_CENTER);
vbox.setSpacing(10);
reqTable.setMinHeight(300);
tempTable.setMinHeight(300);
reqTable.translateYProperty().set(30);
tempTable.translateYProperty().set(-275);
reqTable.setVisible(false);
tempTable.setVisible(false);
reqBox.setVisible(false);
// --- Welcome Page
title.setFont(new Font("Arial", 24));
title.translateYProperty().set(10);
title.setText(pages[0].title);
subtitle.setText(pages[0].subtitle);
subtitle.setMinHeight(30);
subtitle.setTextAlignment(TextAlignment.CENTER);
// --- Show FLOOR
vbox.getChildren().addAll(menuBar, title, subtitle, reqBox, reqTable);
//vbox.getChildren().addAll(menuBar, title, subtitle, reqBox, reqTable, tempTable);
Scene scene = new Scene(vbox, 900, 500);
stage.setScene(scene);
stage.setTitle("FLOOR");
stage.show();
}
// --- Methods
// Page Getters
private void getPage1() {
title.setFont(new Font("Arial", 24));
title.translateYProperty().set(10);
title.setText(pages[1].title);
subtitle.setText(pages[1].subtitle);
subtitle.setMinHeight(20);
reqBox.setVisible(true);
reqTable.setVisible(true);
tempTable.setVisible(false);
}
private void getPage2() {
title.setFont(new Font("Arial", 24));
title.translateYProperty().set(10);
title.setText(pages[2].title);
subtitle.setText(pages[2].subtitle);
subtitle.setMinHeight(20);
reqBox.setVisible(false);
reqTable.setVisible(false);
tempTable.setVisible(true);
}
// --- Classes
// Page
private class Page {
public String title;
public String subtitle;
public Page(String title, String subtitle) {
this.title = title;
this.subtitle = subtitle;
}
}
// AutoCompleteTextArea
public class AutoCompleteTextArea extends StyleClassedTextArea {
public final SortedSet<String> entries;
public ContextMenu entriesPopup;
public AutoCompleteTextArea() {
super();
entries = new TreeSet<>();
entriesPopup = new ContextMenu();
}
public SortedSet<String> getEntries() { return entries; }
public void populatePopup(List<String> searchResult) {
List<CustomMenuItem> menuItems = new LinkedList<>();
int maxEntries = 20;
int count = Math.min(searchResult.size(), maxEntries);
for (int i = 0; i < count; i++) {
final String result = searchResult.get(i);
Label entryLabel = new Label(result);
CustomMenuItem item = new CustomMenuItem(entryLabel, true);
menuItems.add(item);
}
entriesPopup.getItems().clear();
entriesPopup.getItems().addAll(menuItems);
}
}
}
EDIT: I believe I need help getting the selected element in the list I just managed
for it to display a new form but I'm having a lot of trouble finding code that workswith source 3.0.
I've been trying to make a application that allows a user to select a date then add
and remove events based on the selected date. So far I have created the first screen
which is a list of option for the user to choose from. These options are:
Select Date
Add Events
Remove Events
Browse Events
The issues I'm having is I can't get my head around how to display new forms based on the selected Item in the list. I found a small tutorial that allowed me to add a commandlistener which shows the selected item but I'm having trouble figuring out how it gets the item selected in the list and how I could create a new form based on the item selected?
Here's my code so far.
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.List;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
public class mainMidlet extends MIDlet implements CommandListener {
private Display display;
private List list = new List("Please Select a Option", List.IMPLICIT);
private Command select = new Command("Select", Command.SCREEN, 1);
private Form form;
Alert alert;
public mainMidlet() {
display = Display.getDisplay(this);
list.append("Select Date", null);
list.append("Add Events", null);
list.append("Remove Events", null);
list.append("Browse Events", null);
list.addCommand(select);
list.setCommandListener(this);
}
public void startApp() {
display.setCurrent(list);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command command, Displayable displayable) {
if (command == List.SELECT_COMMAND) {
String selection = list.getString(list.getSelectedIndex());
alert = new Alert("Option Selected", selection, null, null);
alert.setTimeout(Alert.FOREVER);
alert.setType(AlertType.INFO);
display.setCurrent(alert);
} else if (command == select) {
destroyApp(false);
notifyDestroyed();
}
}
}
You can add several forms and switch between them
public void commandAction(Command command, Displayable displayable) {
if (displayable == list) {
if (command == List.SELECT_COMMAND) {
switch (list.getSelectedIndex()) {
case 0: // select date
display.setCurrent(someForm);
break;
case 1: //add events
display.setCurrent(someOtherForm);
break;
}
} else if (command == select) {
destroyApp(false);
notifyDestroyed();
}
}
if (displayable == someForm) {
//but it's better practice to make each form a different class implementing CommandListener and it's own commandAction. And leave the display public static in MIDlet class
//...
}
}
i want to create a alert dialog with radiobuttons for single selection or alert dialog with Checkboxes for Multiselection in blackberry.it is possible in android.but i want in blackberry.i searched in google.but i didn't got any solution.please give any suggestions r usefull links for this problem.
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.component.CheckboxField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.DialogFieldManager;
public class CheckboxInputDialog extends Dialog{
private CheckboxField checkboxEditField;
public CheckboxInputDialog(String choices[],int values[], String label){
super(label, choices,values,Dialog.OK, Bitmap.getPredefinedBitmap(Bitmap.INFORMATION), Dialog.GLOBAL_STATUS);
checkboxEditField = new CheckboxField("Lablel",false);
net.rim.device.api.ui.Manager delegate = getDelegate();
if( delegate instanceof DialogFieldManager){
DialogFieldManager dfm = (DialogFieldManager)delegate;
net.rim.device.api.ui.Manager manager =dfm.getCustomManager();
if( manager != null ){
manager.insert(checkboxEditField, 0);
}
}
}
}
Now Call this dialog at following way...
String choices[] = { "OK", "CANCEL" };
int values[] = { Dialog.OK, Dialog.CANCEL };
CheckboxInputDialog d = new CheckboxInputDialog(choices,values,"Dialog Label");
d.show();
Output will Be:
Get Event of OK and Cancel Button.
String choices[] = { "OK", "CANCEL" };
int values[] = { Dialog.OK, Dialog.CANCEL };
final CheckboxInputDialog d = new CheckboxInputDialog(choices, values,"Dialog Label");
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
int iResponse = d.doModal();
if (iResponse == 0) {
System.out.println("Press Ok");
}else{
System.out.println("Press Cancel");
}
}
});
Hope Help full..
Create popupScreen and in this screen you can add radiobuttons and Checkboxes.
public class Custom_Popup extends PopupScreen {
public Custom_Popup() {
// TODO Auto-generated constructor stub
super( new VerticalFieldManager(Manager.VERTICAL_SCROLL),
Field.NON_FOCUSABLE | Field.USE_ALL_WIDTH );
}
}
On your event, push this screen.
UiApplication.getUiApplication().pushScreen(new MyPopup());
public class MyPopup extends PopupScreen{
public MyPopup() {
super(new VerticalFieldManager(), Field.FOCUSABLE);
add();//add checkbox , radio buttons here.
}
I’m trying to make an embedded web page on my blackberry app, and I'm having trouble setting the starting URL.
I can only set the URL when I do it from a callback from a TextField. When I try to do it after I push the new screen on, the app no longer works (it does nothing when you try to run it.
The code that works is the following:
protected boolean keyChar(char key, int status, int time)
{
if ( key == Characters.ENTER )
{
Application.getApplication().invokeLater( new Runnable()
{
public void run() {
//progressBar.reset("", 0, 100, 0);
browserField.requestContent(locationBar.getText().trim());
}
});
return true;
}
return super.keyChar(key, status, time);
}
When I try to set it automatically after everything is created, nothing happens when I run the app.
Code snippet
public MyApp()
{
pushScreen(new BrowserFieldScreen());
// if I have this here, the app will not open
Application.getApplication().invokeLater( new Runnable()
{
public void run() {
gBrowserField.requestContent("http://google.com");
}
});
}
The complete program:
package mypackage;
import java.util.Enumeration;
import java.util.Hashtable;
import net.rim.device.api.browser.field.ContentReadEvent;
import net.rim.device.api.browser.field2.BrowserField;
import net.rim.device.api.browser.field2.BrowserFieldListener;
import net.rim.device.api.system.Application;
import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.GaugeField;
import net.rim.device.api.ui.component.TextField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;
import org.w3c.dom.Document;
/**
* BrowserField2ProgressTracker
* - A BrowserField2 Mini-browser application that keeps track of page-loading progress
* and displays progress information to the user.
*/
public class MyApp extends UiApplication {
static BrowserField gBrowserField;
public static void main(String[] args){
MyApp theApp = new MyApp();
theApp.enterEventDispatcher();
}
public MyApp() {
pushScreen(new BrowserFieldScreen());
/*
if I have this hear, the ap will not open
Application.getApplication().invokeLater( new Runnable() {
public void run() {
gBrowserField.requestContent("http://google.com");
}
});
}
*/
}
/**
* BrowserFieldScreen
* - A screen that contains a location bar, a progress bar, and a browser field
*/
class BrowserFieldScreen extends MainScreen {
// The location bar where URL will be typed in
private TextField locationBar;
// The BrowserField
private BrowserField browserField;
public BrowserFieldScreen() {
// progressTracker = new BrowserFieldLoadProgressTracker(10f);
createGUI();
}
private void createGUI() {
VerticalFieldManager mainManager = new VerticalFieldManager(Field.USE_ALL_WIDTH | Field.USE_ALL_HEIGHT | Manager.VERTICAL_SCROLLBAR | Manager.HORIZONTAL_SCROLLBAR );
locationBar = new TextField() {
protected boolean keyChar(char key, int status, int time) {
if ( key == Characters.ENTER ) {
Application.getApplication().invokeLater( new Runnable() {
public void run() {
//progressBar.reset("", 0, 100, 0);
browserField.requestContent(locationBar.getText().trim());
}
});
return true;
}
return super.keyChar(key, status, time);
}
};
locationBar.setBackground(BackgroundFactory.createSolidBackground(Color.BEIGE));
locationBar.setText("http://google.com");
browserField = new BrowserField();
MyApp.gBrowserField=browserField;
mainManager.add(locationBar);
mainManager.add(browserField);
add(mainManager);
}
}
I don't think you can call invokeLater() until your app, theApp has enter the main event thread. Which is done by calling theApp.enterEventDispatcher()
instead try changing your BrowserFieldScreen constructor to:
public BrowserFieldScreen() {
// progressTracker = new BrowserFieldLoadProgressTracker(10f);
createGUI();
browserField.requestContent("http://google.com");
}
am new here. i have a slight problem; PLease look at the following code and tell me if am doing something wrong because the image is not displaying. i have made it really small so it should fit but its not displaying. i have images displaying in other screens but this main midlet would not. Here is the code:
import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* #author jay
*/
public class WShop extends MIDlet implements CommandListener {
/* Declare display variables*/
private Form mainForm;
private Display display;
private Command OK,Exit,wView, mView, myView;
/* */
Categories categories = new Categories(this);
Image image;
public WShop() {
/* initialize Screen and Command buttons that will
be used when the application starts in the class constructor*/
mainForm = new Form("Wind Shopper");
OK = new Command("OK", Command.OK, 2);
Exit = new Command("Exit", Command.EXIT, 0);
wview= new Command("wview", Command.OK, 0);
mview= new Command("mview", Command.OK, 0);
try {
/* retrieving the main image of the application*/
image = Image.createImage("/main.png");
} catch (IOException ex) {
ex.printStackTrace();
}
mainForm.addCommand(OK);
mainForm.addCommand(Exit);
mainForm.addCommand(wView);
mainForm.addCommand(mView);
mainForm.setCommandListener(this);
}
public void startApp() {
/* checks to see if the display is currently empty
and then sets it to the current screen */
if (display == null) {
display = Display.getDisplay(this);
}
display.setCurrent(mainForm);
}
/* paused state of the application*/
public void pauseApp() {
}
/* Destroy Midlet state*/
public void destroyApp(boolean unconditional) {
}
Thanks in advance.
Looks to me like you forgot to Form.append() your Image to your form.