How do I add an Infinite progress till the WebPage is fully loaded?
Form hi = new Form("Hi World");
hi.setLayout(new BorderLayout());
final BrowserComponent wb = new BrowserComponent();
hi.addComponent(BorderLayout.CENTER, wb);
final Dialog ipDlg = new InfiniteProgress().showInifiniteBlocking();
wb.setURL("https://www.codenameone.com");
wb.addWebEventListener("onLoad", new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});
hi.show();
ipDlg.dispose();
above is the code I am using.
This won't work since dialog is literally a separate Form. So the browser component will never load and won't show what's going on below.
You can use an InteractionDialog which is just a Container in the layered pane. This means the browser will keep loading and you can draw on top of it. Just add the InfiniteProgress into the InteractionDialog and show it.
I've got this Controller connected to a FXML-file with several buttons, labels, a table, etc.
I've got some popups that get initialized and shown when different buttons get clicked and that works fine.
I've got another popup that I'd like to 'pop up' when something goes wrong, so this is called when an event get's handled that has been sent from java-code in another class.
This message pop-up get's called, but the code within the Platform.runLater() isn't executed, actually freezing the GUI.
There's one distinction I've found that seems to cause this and that is that a Platform.isFxApplicationThread() that I call right before the Platform.runLater() returns false in this message pop-up where it returns true when one of the other pop-ups get called from a button-click.
As I've also tried one of those pop-ups that's normally called from a button-click and that also doesn't work when it's called from the code that get's executed because of the incoming event, I'm pretty sure this is the problem, but Platform.runLater states "This method, which may be called from any thread, will post the Runnable to an event queue and then return immediately to the caller." and that seems not true for me, so I'm kinda puzzled if this actually is the problem ...
Has anyone encountered this before and / or does anyone know what I'm doing wrong?
This works fine:
#FXML
private void btnCashClicked(ActionEvent event) {
screensController.getCashTransactionController().addCashTransactionListener(this);
labelToPay = new Label(eurosToPay + " euro");
sealbagTextField = new SealbagTextField();
PopupUtils.showCashPaymentPopup(btnSealbag, btnCashOk, labelPaid, labelSealbag, labelToPay, lblExchange,
labelExchange, labelReturnValue, eurosToPay, btnCash, this, sealbagTextField);
screensController.getMainController().startTransaction(amountInCents, PaymentType.Asap);
}
This code in the same controller class doesn't show a pop-up:
#Override
public void showErrorOnScreen(String message) {
// temporary usage of label and textfield
labelToPay = new Label(eurosToPay + " euro");
sealbagTextField = new SealbagTextField();
PopupUtils.showCashPaymentPopup(btnSealbag, btnCashOk, labelPaid, labelSealbag, labelToPay, lblExchange,
labelExchange, labelReturnValue, eurosToPay, btnCash, this, sealbagTextField);
//PopupUtils.showMessagePopup("Error", message, "Close", 374, 250, btnCancel);
}
I'm on Windows and using jre1.8.0_60
The code of the cashPopup:
public static int showCashPaymentPopup(Button btnSealbag, Button btnCashOk, Label labelPaid, Label labelSealbag, Label labelToPay, Label lblExchange, Label labelExchange, Label labelReturnAmount, int amount, Node node, PayScreen parent, SealbagTextField sealbagTextField) {
int paid = 0;
logger.debug("cashPopup is on GUI thread: " + Platform.isFxApplicationThread());
Platform.runLater(new Runnable() {
#Override
public void run() {
cashPopup.getContent().clear();
Rectangle rectangle = new Rectangle();
rectangle.setArcHeight(20);
rectangle.setArcWidth(20);
rectangle.setFill(Color.LIGHTBLUE);
rectangle.setWidth(466);
rectangle.setHeight(311);
rectangle.setStroke(Color.DARKBLUE);
rectangle.setStrokeType(StrokeType.INSIDE);
...
cashPopup.getContent().addAll(rectangle, textArea, headerLabel, lblDesc, lblAmount, labelAmount, lblPaid, labelPaid, lblToPay, labelToPay, btnCashOk, lblSealbag, labelSealbag, lblExchange, labelExchange, labelReturnAmount, btnSealbag, btnCancel);
cashPopup.show(node, 150, 164);
}
});
return paid;
}
And the showMessagePopup:
public static void showMessagePopup(String title, String text, String buttonText, int posX, int posY, Node parent) {
logger.debug("messagePopup is on GUI thread: " + Platform.isFxApplicationThread());
Platform.runLater(new Runnable() {
#Override
public void run() {
logger.debug("0");
messagePopup.getContent().clear();
Rectangle rectangle = new Rectangle();
rectangle.setArcHeight(20);
rectangle.setArcWidth(20);
rectangle.setFill(Color.LIGHTBLUE);
rectangle.setWidth(500);
rectangle.setHeight(300);
rectangle.setStroke(Color.DARKBLUE);
rectangle.setStrokeType(StrokeType.INSIDE);
Label headerLabel = new Label(title);
headerLabel.setStyle("-fx-font-size: 18; -fx-font-family: Arial;");
headerLabel.setLayoutX(15);
headerLabel.setLayoutY(10);
TextArea textArea = new TextArea();
textArea.setStyle("-fx-font-size: 14; -fx-font-family: Arial;");
textArea.setLayoutX(10);
textArea.setLayoutY(35);
textArea.setMaxWidth(480);
textArea.setMinHeight(190);
textArea.setMaxHeight(190);
textArea.setEditable(false);
textArea.setWrapText(true);
textArea.setText(text);
Button btnClose = new Button(buttonText);
btnClose.setLayoutX(180);
btnClose.setLayoutY(235);
btnClose.setPrefSize(120, 54);
btnClose.setStyle("-fx-font-size: 18; -fx-font-family: Arial; -fx-text-fill:white; -fx-background-color: linear-gradient(#8b9aa1, #456e84), linear-gradient(#c5dde7, #639fba), linear-gradient(#79abc1, #639fba); -fx-background-insets: 0,1,2; -fx-background-radius: 6,5,4;");
btnClose.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
messagePopup.hide();
}
});
messagePopup.getContent().addAll(rectangle, headerLabel, btnClose, textArea);
messagePopup.show(parent, posX, posY);
}
});
}
logger.debug("0") isn't even executed ...
Found it, by running in debug mode and suspending the Java FX thread to see what it is doing.
There's this 'other thread' that gets started from the main program and which needs to get started before the process can continue. This other thread looks like this:
pinPadAsSlaveThread = new Thread(pinPadAsSlave);
pinPadAsSlaveThread.start();
while (!pinPadAsSlave.isRunning()) {
// wait for pinPadAsSlave to be running
try {
Thread.sleep(10);
} catch(InterruptedException ie) {
// ignore
}
}
Normally this takes about 50 ms, but as the pin pad is unavailable on the network this becomes an infinite loop. That on itself should be handled of course, by letting this loop only try it for 50 times or so.
But the real problem is that this thread that is put to sleep for 10 ms all the time is the Java FX thread. I don't know why the Java FX thread is doing the setting up of the communication, as it shouldn't (and I didn't ask for that by putting it inside a platform.runLater or something alike), but the fact is: it is ...
I am interested in showing a progressbar while i switch to a different tab on my main tabpane. The tabs on my tabpane takes sometime(5-10 sec) before actually appearing. I want the progressbar to appear while the tab switching is taking place.
Download the data in a separate Task<T>, where T is a type that encapsulates all the data. Set the content of the tab to a progress bar; register an onSucceeded handler with the Task which sets the content of the tab to the display of the data, then start the task.
So this will look something like:
final Tab tab = ... ;
final ProgressBar progressBar = new ProgressBar();
final Task<MyDataType> loadDataTask = new Task<MyDataType>() {
#Override
public MyDataType call() throws Exception {
// download data...
MyDataType result = ... ;
return result ;
}
};
tab.setContent(progressBar);
loadDataTask.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
#Override
public void handle(WorkerStateEvent event) {
MyDataType data = loadDataTask.getValue();
Node tabContent = ... ; // build tab content from data
tab.setContent(tabContent);
}
});
final Thread thread = new Thread(loadDataTask);
thread.setDaemon(true);
thread.start();
Do not update any UI elements that are part of the scene graph in the Task's call(...) method. If you can measure the progress of the data download and want to show it in the progressBar (instead of an indeterminate progress bar), you can do
progressBar.progressProperty().bind(loadDataTask.progressProperty());
and then in the Task's call method, call
updateProgress(amountDone, totalAmount);
I'm building a nokia s40 app. I've set this app to be in full screen mode and I notice that when I set this full screen mode, I've lost the native back buttons.
I need to build a back button like the native back button (always on screen, in the bottom right side of the screen). I've tried it with a borderlayout and putting it on the south, but the text from the center container doesn't appears over this...and has transparent background.
If anyone has some sample code, it will be great.
this code simply located a button in the bottom right side of the screen. it's work well.
public void startApp() {
Display.init(this);
Form f = new Form();
//Create a ComponentGroup for all components except backbutton
final ComponentGroup cg = new ComponentGroup();
final Button backButton = new Button("BackButton");
// add this part to your code
f.addOrientationListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Dimension d = new Dimension(Display.getInstance().getDisplayWidth(), Display.getInstance().getDisplayHeight());
cg.setPreferredSize(d);
backButton.setX(Display.getInstance().getDisplayWidth() - backButton.getPreferredW());
}
});
//set CoordinateLayout to f
f.setLayout(new CoordinateLayout(Display.getInstance().getDisplayWidth(), Display.getInstance().getDisplayHeight()));
//Dimension d = new Dimension(Display.getInstance().getDisplayWidth(), Display.getInstance().getDisplayHeight());
cg.setPreferredH(Display.getInstance().getDisplayHeight());
cg.setPreferredW(Display.getInstance().getDisplayWidth());
cg.setLayout(new BorderLayout());
cg.addComponent(BorderLayout.NORTH, new Button("NORTH"));
cg.addComponent(BorderLayout.EAST, new Button("ESET"));
cg.addComponent(BorderLayout.WEST, new Button("WEST"));
cg.addComponent(BorderLayout.CENTER, new TextArea("sdsdsd"));
cg.setX(0);
cg.setY(0);
backButton.getStyle().setBgColor(0x2233ff);
backButton.setX(Display.getInstance().getDisplayWidth() - backButton.getPreferredW());
backButton.setY(Display.getInstance().getDisplayHeight() - backButton.getPreferredH() - f.getTitleArea().getLayoutHeight());
f.addComponent(cg);
f.addComponent(backButton);
f.show();
}
you can do like this to any form.
if you want back Button is showed in all forms you can write some OOP programming to do this.
it works well.
I am looking for a way to display images on my UI from a background thread. This background thread recieves streaming images in the form of byte array. I need to diplay it on my UI
Can anyone help with this please?
I already tried accessing the bitmap field from the background thread but nothing happens
Thanks
bmp = Bitmap.createBitmapFromBytes(jpegBytes, 0, jpegBytes.length, 1);
final Bitmap bit = bmp;
final byte [] jp = jpegBytes;
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run()
{
camView.jpegBytes = jp;
System.out.println(bit);
camView.bmpStream.setBitmap(bit);
//camView.bmpStream.
}
});
If you need to change the Main Screen means if you need to add field to Main Screen from the Background thread you need to call
invalidate();
method to repaint the screen otherwise no change will place in Screen
Ex:
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run()
{
camView.jpegBytes = jp;
System.out.println(bit);
camView.bmpStream.setBitmap(bit);
invalidate();
//camView.bmpStream.
}
});
Hey guys I managed to find the problem I had an issue with the used of references to my parent class. the above code I have posted in the question works perfect if you have a reference to the main class.