LWUIT 1.4 : Why is there sometimes duplicates in this TextArea? - java-me

I created a class from Dialog which contains a TextArea :
public class Alert extends Dialog {
private Container c = new Container(new BorderLayout());
private Label titre = new Label("Mobile Banking");
private TextArea chp;
private Command[] comms;
public Alert(String text, Command[] comms)
{
super();
titre.setUIID("titre_alert");
titre.setAlignment(Label.CENTER);
this.comms = comms;
setAutoDispose(true);
for (int cmd=0; cmd<comms.length; cmd++)
addCommand(comms[cmd]);
chp = new TextArea();
chp.setEditable(false);
chp.setAlignment(Label.CENTER);
chp.getSelectedStyle().setBorder(null);
chp.getUnselectedStyle().setBorder(null);
chp.getSelectedStyle().setBgColor(this.getStyle().getBgColor());
chp.getUnselectedStyle().setBgColor(this.getStyle().getBgColor());
if (text.length() % 2 != 0)
text = " ".concat(text);
while (text.substring(0, (text.length()/2)+1).length() < chp.getMaxSize()/2)
{
text = " ".concat(text);
}
chp.setText(text);
c.addComponent(BorderLayout.NORTH, titre);
c.addComponent(BorderLayout.CENTER, chp);
}
public Command affiche()
{
return show(null, c, comms);
}
}
Inside a Form I start a thread which makes a HttpConnection call and other tasks. If the tasks end successfully then I call the affiche() method of the above class Alert :
alert = new Alert("Chargement effectué avec succès !", new Command[]{ok});
cntnr.removeComponent(cPatienter); // container displaying the "please wait..."
repaint(); // repainting the Form
if (alert.affiche() == ok) // showing the confirmation of successfullness of the task
{
alert.dispose();
controller.displayScreen("Menuprincipale");
}
The problem is that , sometimes , the text shown when calling the affiche() method is duplicated : it should show only the text Chargement effectué avec succès ! but sometimes it shows the text and also Chargement effectué.
So how to make it that only the text parameter is only shown but not duplicated ?

You are invoking LWUIT on a separate thread which is illegal. You need to use Display.callSerially to avoid a race condition in the text layout code. Something like this:
Display.getInstance().callSerially(new Runnable() {
public void run() {
// your LWUIT code here, no need for repaints
}
});
A better approach is to use LWUIT4IO for your networking since it does this seamlessly for you.

Related

Is there any way to change dialog cancel button in x++?

I am using Dialog class to make my dialog. I added there custom lookup and field, but it looks like cancel button doesnt work as it supposed to work. I dont know a lot about Dialog class and how it works... Is there any suggestions how to change Cancel button to its normal function - cancel the dialog. Right now it throws info that is supposed to throw out on Process when u press Ok on Dialog.
My current code:
class copyline
{
public void clicked()
{
Counter createdRecordCnt;
Trans trans;
Table table;
MultiSelectionHelper helper = MultiSelectionHelper::construct();
JournalId journalId;
helper.parmDatasource(Trans_DS);
MyClass MyClass= new MyClass();
MyClass.dialog();
trans = helper.getFirst();
while(trans.RecId != 0)
{
MyClass.parmJournalTrans(trans);
MyClass.parmJournalId(JournalId);
if (journalId)
{
MyClass.parmJournalTrans(trans);
MyClass.parmJournalId(JournalId);
MyClass.run();
}
else
{
if (MyClass.prompt())
{
MyClass.run();
journalId = MyClass.parmJournalId();
}
}
createdRecordCnt++;
trans = helper.getNext();
}
Info(strFmt("#LabelFile:Label", createdRecordCnt, MyClass.parmJournalId()));
}
}
I have class with Dialog object, validations, lookup etc.
Dialog on class:
public Object dialog()
{
Dialog dialog = super();
FormBuildStringControl control;
dialog.caption("#LabelFile:Label");
Journal = dialog.addField(extendedTypeStr(MyEDT));
control = Journal.control();
control.registerOverrideMethod(methodstr(FormStringControl, lookUp), methodStr(MyClass, journalTypeLookup), this);
return dialog;
}

How to make animations with java fx without stops or lag

I'm developing a system with the JFoenix library, and I'm using some buttons with wave animation. But when I use a button to open a new window, or load a list for example, the animation "stops" briefly while the screen loads, or the list loads. I would like to know how to run the animation before loading the list, or something like that. Do not let the animation crash. I've tried to use Threads to load the list, however, because the function that calls the thread gets inside the click function of the button, the animation locks in the same way.
Another example is when I use a JavaFX transition animation to open a new AnchorPane, while its elements are loading, the animation hangs briefly, and that takes away all the quality of the application
Example, when I click the "add" button shown in the image below, it generates a waveform on the button, then performs a function to open the AnchorPane and a transition function to move the panel to the x = 0 position. And it also carries a simple DropDownList with some elements.
The code executed when the button is clicked:
#FXML
public void btnAddClique(ActionEvent event) {
if (telaAtual != 3) { //caso a tela de cadastro nao esteja aberta
PaneAdd add = new PaneAdd();
FXMLLoader loader = new FXMLLoader(getClass().getResource("PaneAdd.fxml"));
loader.setController(add);
try {
if (paneAtual == 1) {
paneFundo2.getChildren().setAll((Node)loader.load());
paneAtual = 2;
} else {
paneFundo.getChildren().setAll((Node)loader.load());
paneAtual = 1;
}
} catch (IOException e){
System.out.println("Erro ao alterar tela, função 'btnAddClique', classe FXMLDocumentController: " + e.toString());
}
telaAtual = 3;
new Thread(new RunAnimacaoAbertura()).start();
add.init(this.listView);
}
}
After that, it runs the Animation Thread:
public class RunAnimacaoAbertura implements Runnable {
public void run() {
TranslateTransition tran = new TranslateTransition();
TranslateTransition tran2 = new TranslateTransition();
tran.setDuration(Duration.seconds(0.250));
tran2.setDuration(Duration.seconds(0.250));
tran.setNode(paneFundo);
tran2.setNode(paneFundo2);
if (paneAtual == 1) {
tran2.setFromX(0);
tran2.setToX(515);
tran.setFromX(-515);
tran.setToX(0);
} else {
tran.setFromX(0);
tran.setToX(515);
tran2.setFromX(-515);
tran2.setToX(0);
}
tran2.play();
tran.play();
}
}
And finally, it performs the function of loading the list:
public void init(JFXListView<Label> listView) {
listViewControler = listView;
cbGerentePA.setItems(arquivo.pegarListaString(1, 0));
date.setPromptText("Nascimento");
date.setEditable(false);
date.setDefaultColor(Color.web("#1abc9c"));
date.getStylesheets().add("agenda/style.css");
hBoxFundoDate.getChildren().setAll(date);
}

How to control flow of multiple Rss Files

I developed RssFeed Application using LWUIT j2me(java) for 2 xml files, now I want to show those 2 xml files on LWUIT Tabs.
That means, when my application runs, default tab will be displayed (on that tab my first Rss xml file Titles should be displayed), and when the user click on tab2 my second Rss xml titles should be displayed.
I am able to display the same titles of one rss files on both the tabs, how to control my flow to achieve my task?
Here my code:
public class XMLMidlet extends MIDlet implements ActionListener {
public XMLMidlet() {
Display.init(this);
news = new Vector();
m_backCommand = new Command("Back");
cmdExit = new Command("EXIT");
cmdDetails = new Command("Details");
}
public void startApp() {
//RssFeed URL's
String urls[] = {"http://topnews-23.rss",
"http://topstory-12.rss"};
for(int i=0;i<urls.length;i++){
ParseThread myThread = new ParseThread(this,urls[i]);
//this will start the second thread
myThread.getXMLFeed(urls[i]);
}
}
//method called by the parsing thread
public void addNews(News newsItem,String url) {
try{
news.addElement(newsItem);
form1 = new Form();
myNewsList = new List(newsVector);
newsList =new List(newsVector);
myNewsList.setRenderer(new NewsListCellRenderer());
newsList.setRenderer(new NewsListCellRenderer());
tabs=new Tabs(Component.TOP);
tabs.addTab("TopNews", myNewsList);
tabs.addTab("Topstory",newsList);
form1.addComponent(tabs);
form1.show();
}
catch(Exception e){
e.printStackTrace();
}
}
You should move below code
myNewsList = new List(newsVector);
newsList =new List(newsVector);
myNewsList.setRenderer(new NewsListCellRenderer());
newsList.setRenderer(new NewsListCellRenderer());
tabs=new Tabs(Component.TOP);
form1 = new Form();
tabs=new Tabs(Component.TOP);
tabs.addTab("TopNews", myNewsList);
tabs.addTab("Topstory",newsList);
from addNews method to constructor XMLMidlet. addNews method should use url parameter to differ for which list the newsItem is directed.
Update
Below is how I think you should implement addNews method:
public void addNews(News newsItem, String url) {
if (url.endsWith("topnews-20.rss")) {
myNewsList.addElement(newsItem);
} else if (url.endsWith("topstory-25.rss")) {
newsList.addElement(newsItem);
}
}
serRenderer does not need to be called from addNews and form1.show() should be moved to startApp.

LWUIT 1.5 How to capture tab click event, fetch content from server and show it in selected tab?

I am using LWUIT 1.5 tabs to show notifications. I have three Tabs and I fetch notifications from a php web service. I successfully fetched list of notifications for first Tab. But for next two Tabs I am failing to understand what code I should write to
Detect that second/third Tab is clicked. I know how to add commandListener to a Button. What commandListener is there for Tab selection?
How to refresh content of a Tab when new data is received from the server?
private void showNotificationList() {
try {
Form f = new Form("Notifications");
f.setScrollable(false);
f.setLayout(new BorderLayout());
final List list = new List(getNotifications()); //gets hashtables - notices
list.setRenderer(new GenericListCellRenderer(createGenericRendererContainer(), createGenericRendererContainer()));
list.setSmoothScrolling(true);
//System.out.println("adding list component to listview");
list.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i = list.getSelectedIndex();
noticeDetailsForm(notices[i]);
//Dialog. show( "title", notices[i].toString(), "ok", "exitt");
}
});
//Container c2 = new Container(new BoxLayout(BoxLayout.Y_AXIS));
//c2.addComponent(list);
Tabs tabs = new Tabs(Tabs.TOP);
tabs.addTab("Recent", list);
tabs.addTab("Urgent", new Label("urgent goes here"));
tabs.addTab("Favourites", new Label("favs goes here"));
//f.addComponent(tabs);
f.addComponent(BorderLayout.CENTER, tabs);
Command backComm = new Command("Back") {
public void actionPerformed(ActionEvent ev) {
Dashboard.dbInstance.setUpDashboard();
}
};
f.addCommand(backComm);
//System.out.println("showing lsit form");
f.show();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private Container createGenericRendererContainer() throws IOException { //System.out.println("container called");
//System.out.println("container called");
Container c = new Container(new BorderLayout());
c.setUIID("ListRenderer");
Label xname = new Label("");
Label description = new Label();
Label focus = new Label("");
Container cnt = new Container(new BoxLayout(BoxLayout.Y_AXIS));
xname.setName("Name");
xname.getStyle().setBgTransparency(0);
xname.getStyle().setFont(Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM));
//description.setFocusable(true);
description.setName("Description");
cnt.addComponent(xname);
cnt.addComponent(description);
c.addComponent(BorderLayout.CENTER, cnt);
Button thumb = new Button(Image.createImage("/res/home-work.png"));
//Image img = Image.createImage("/res/home-work.png");
c.addComponent(BorderLayout.WEST, thumb);
return c;
}
private Hashtable[] getNotifications() {
int total = notices.length;
//System.out.println(total);
Hashtable[] data = new Hashtable[total];
//Hashtable[] data = new Hashtable[5];
/data[0] = new Hashtable();
data[0].put("Name", "Shai");
data[0].put("Surname", "Almog");
data[0].put("Selected", Boolean.TRUE);/
for (int i = 0; i < total; i++) {
data[i] = new Hashtable();
//System.out.println(notices[i].getName());
data[i].put("Name", notices[i].getName());
data[i].put("Description", notices[i].getDescription());
data[i].put("Id", Integer.toString(notices[i].getId()));
}
return data;
}
1)I had the same problem and I solved it by overriding Keyreleased of the Form not the Tab
and inside it I check for the component that is focused and if it is the Tab get "tab.selectedIndex" to detect in which Tab I am and load appropriate data .
Here is Sample code(this inside the my derived form that extends Form )
**
public void keyReleased(int keyCode) {
Component p=this.getFocused();
String str= p.getClass().getName();
if(str.toLowerCase().indexOf("radiobutton")!=-1){ // Radiobutton because when u
Here do tab specific work focus on the
tab it returns radiobutton.
lwuit understands tabs as list
of radiobuttons
}**
2)and about refreshing the data I did a solution and I don't Know if its right
I get the new data , create new List and remove the old one and attach the new one then
call Form.repaint();

Java ME - Multiple forms, moving from one screen to the next

I am using Java Micro Edition and I am trying to create a simple login form with a record store. When the user enters the details I'd like to check them against the ones stored and then move onto another screen like a welcome area.
I have a feeling it has something to do with the form element and switching between it but I can't seem to get anywhere with google
try this
form = new Form("login");
form.addCommand(getExitCommand());
form.addCommand(getOkCommand());
form.setCommandListener(this);
public void commandAction(Command command, Displayable displayable) {
if (displayable == form) {
if (command == exitCommand) {
exitMIDlet();
} else if (command == okCommand) {
display.setCurrent(getWelcomeForm());
}
} else if (displayable == form1) {
if (command == backCommand) {
// do something else
}
}
}
display is something that should be created in the constructor and also above it i.e.
public class YourMidlet extends MIDlet implements CommandListener {
private Display display;
private Form form1;
private Form form2;
public YourMidlet
{
display = Display.getDisplay(this);
form 1 = new Form("hello form this is form 1");
form 2 = new Form("hello form 2");
display.setCurrent(form1);
}
}
you then do:
display.setCurrent(form2);
to switch to form 2

Resources