Modal window with progressbar for long tasks - java

In some cases, I want show to user a modal window with a progress bar when the long running query. (For individual UI items, I use the method setEnabled (true/ false) but I want more elegant solution.)
For example, in the entry point, until all elements not initialized -
public void onModuleLoad() {
// initialization of the interface here
}
And also, for example, when completing the dependent list box (relationship one to many)
#UiHandler("listBox")
public void onListBoxChange(ChangeEvent event) {
someService.findDependencies(id, new AsyncCallback<List<DependencyDTO>>() {
public void onFailure(Throwable caught) {
// exception handling here
}
public void onSuccess(List<DependencyDTO> data) {
// listBox filling here
}
});
}
In Vaadin applications I can added to the listener the following code, for example -
...
Thread thread = new Thread() {
#Override
public void run() {
// some initialization here
window.removeWindow(blockerWindow);
}
};
thread.start();
blockerWindow = new BlockerWindow();
window.addWindow(blockerWindow);
...
In my case, I can use the following method to display a window with a progress bar -
private void freezeInterface() {
blockerWindow = new BlockerWindow()
blockerWindow.setGlassEnabled(true);
blockerWindow.setAnimationEnabled(true);
blockerWindow.setModal(true);
blockerWindow.center();
blockerWindow.show();
}
And the method to hide window -
private void unfreezeInterface() {
blockerWindow.hide();
}
The question is, when hide the window.
For example, at the entry point there are series of queries -
...
service1.findDependenciesForListBox1(id1, new AsyncCallback<List<Dependency1DTO>>() {
public void onFailure(Throwable caught) {
// exception handling here
}
public void onSuccess(List<Dependency1DTO> data) {
// listBox1 filling here
}
});
service2.findDependenciesForListBox2(id2, new AsyncCallback<List<Dependency2DTO>>() {
public void onFailure(Throwable caught) {
// exception handling here
}
public void onSuccess(List<Dependency2DTO> data) {
// listBox2 filling here
}
});
serviceN.findDependenciesForListBoxN(idN, new AsyncCallback<List<DependencyNDTO>>() {
public void onFailure(Throwable caught) {
// exception handling here
}
public void onSuccess(List<DependencyNDTO> data) {
// listBoxN filling here
}
});
And so on.
The answers come in a previously unknown sequence and hide the window in one of the methods onSuccess I can not.
I can use a timer, but I do not know beforehand the time which to pass in schedule.
...
blockerWindow.show();
private void unfreezeInterface() {
timer = new Timer() {
public void run() {
blockerWindow.hide();
}
};
timer.schedule(15000);
}
...
How to properly implement this in GWT?

If you know the number of responses to get, you can use such approach:
class TaskCompletedHandler{ // inner class
private static final int NUMBER_OF_RESPONSES = 4;//just example
private int tasksCompleted;
public void notifyOfCompletedTask(){
tasksCompleted++;
if (tasksCompleted == NUMBER_OF_RESPONSES){
blockerWindow.hide();
}
}
}
create instance of this class before showing modal window and then notify this handler in AsyncCallback
service1.findDependenciesForListBox1(id1, new AsyncCallback<List<Dependency1DTO>>() {
public void onFailure(Throwable caught) {
taskCompletedHandler.notifyOfCompletedTask();
// exception handling here
}
public void onSuccess(List<Dependency1DTO> data) {
taskCompletedHandler.notifyOfCompletedTask();
// listBox1 filling here
}
});

Related

Javafx button doesn't respond after first call (I call methode in task thread)

I have initialized parseBtn onAction but because the work in parseFunction will be more than one minute I created Task to do the work in new Thread(task).start() the function is working in background will but after finishing parseBtn doesn't respond any more.
I think because the work in another thread the button doesn't notify that work finished
#FXML private Button parseBtn;
public void initialize(){
parseBtn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
new Thread(task).start();
}
});
}
Task<Void> task = new Task<Void>() {
#Override
public Void call() throws Exception {
//do something
return null;
}
};
the process inside the task is done but the button doesn't respond any more
I found the solution my mistake was that task can't be calling multiple time.
class parser extends Thread{
#Override
public void run() {
//work to do
}
}
public void initialize(){
parseBtn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
parser p = new parser();
p.start();
}
});
}

JDialog invisible, components clickable

I'm using a JDialog instatiated at startup of the application, to show messages several times. Sometimes the dialog and its controls are invisible, but clickable.
The JDialog is instantiated only once and set to visible 'true' each time a message should be shown and then set to visible 'false' till the next message should be shown.
To exlude multithreading related problems, i always use SwingUtilities.invokeLater(...) for ui calls, when a thread creates a message and shows the dialog.
Because its a huge project and my problem isn't related to any specific code, i don't post code but describe the problem. The problems seems not to be reproducible but happens sometimes, so it might be a threading problem despite running each related call on the EDT.
What am I doing wrong?
public class MessageHandler {
private volatile static MessageHandler messageHandler = null;
private List<Message>messages = null;
private volatile WeakReference<MessagesPanelControl> view = null;
private final Object viewSynchronizationObject = new Object();
private MessageHandler() {
messages = new ArrayList<Message>();
}
public static MessageHandler getInstance() {
MessageHandler result = messageHandler;
if (result == null) {
synchronized (MessageHandler.class) {
result = messageHandler;
if (result == null)
messageHandler = result = new MessageHandler();
}
}
return result;
}
public void registerView(MessagesPanelControl view) {
this.view = new WeakReference<MessagesPanelControl>(view);
}
public void addMessage(final Message message) {
synchronized (viewSynchronizationObject) {
messages.add(message);
Collections.sort(messages);
updateView();
}
}
private void updateView() {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
synchronized (viewSynchronizationObject) {
if (view == null) {
return;
}
MessagesPanelControl mpc = view.get();
if (mpc != null) {
mpc.updateView();
}
}
}
});
}
}
In the MainFrame class i'm doing this initialization once at startup:
MessagesPanelControl mp = new MessagesPanelControl();
MessageHandler.getInstance().registerView(mp);
LockPane messageBasicPane = new LockPane(this, mp);
And then in different threads this is called to show a message via the MessageHandler Singleton:
MessageHandler.getInstance().addMessage(Message.getSimpleMessage("Error", "Fatal error occured", Message.MessageIcon.ERROR));
I didn't post all details, but all necessary parts to understand the whole problme, hope it makes it more understandable.
The MessagePanelControl (mpc) is a class, that extends JPanel. Its updateView()-method creates the message controlls based on the MessageHandler's message list like buttons, labels and icons. Finally the method sends a Delegate like command to the main frame to show the JDialog containing the MessagePanelControl.
Summarized it does:
messageList.size()>0: create message panels for each message in list in MessageHandler
messageList.size()>0: show JDialog with MessagePanelControl
messageList.size()<=0: hide JDialog with MessagePanelControl
public void updateView() {
synchronized (viewMPCSynchronizationObject) {
Utils.throwExceptionWhenNotOnEDT();
JPanel messagesListPanel = new JPanel();
scrollPane.setViewportView(messagesListPanel);
scrollPane.setBorder(null);
messagesListPanel.setLayout(new BoxLayout(messagesListPanel, BoxLayout.Y_AXIS));
if (MessageHandler.getInstance().getMessages() != null && MessageHandler.getInstance().getMessages().size() > 0) {
[...]
//Create buttons, text icons... for each message
[...]
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MainFrame().showMessageBoard();
}
});
} else {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MainFrame().closeMessageBoard();
}
});
}
repaint();
}
}
MainFrame:
//Show Messageboard
public void showMessageBoard() {
if (messageBasicPane != null) {
messageBasicPane.setVisible(true);
messageBasicPane.repaint();
}
}
[...]
//Close Messageboard
public void closeMessageBoard() {
if (messageBasicPane != null) {
messageBasicPane.setVisible(false);
}
}
This line creates the JDialog, in detail:
[...]
public LockPane(JFrame parentFrame, JComponent componentToShow, Dimension paneSize, float opacity, ModalityType modality) {
super(parentFrame, true);
Utils.throwExceptionWhenNotOnEDT();
createDialog(paneSize, opacity, modality);
if (componentToShow != null) {
add(componentToShow);
}
pack();
}
private void createDialog(Dimension paneSize, float opacity, ModalityType modality) {
Utils.throwExceptionWhenNotOnEDT();
setUndecorated(true);
setModalityType(modality);
if (opacity < 1 && opacity >= 0)
com.sun.awt.AWTUtilities.setWindowOpacity(this, opacity);
setSize(paneSize);
setPreferredSize(paneSize);
setMaximumSize(paneSize);
setBounds(0, 0, paneSize.width, paneSize.height);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
[...]
A new observation with Java VisualVM is, that the AWT-EventQueue isn't blocked, only sometime there are small periods of 'wait' but nothing blocking. Another strange thing is, that sometimes my JDialog is fully transparent (invisible) and sometimes its white with the desired opacity.
In this function, you are essentially trying to await the Runnable passed to the SwingUtilities.invokeLater which invokeLater submits to the EDT to get executed. The lock that you are holding on viewSynchronizationObject will block EDT if it is locked by other application thread which is actually evident from your code as you have used this variable in several other places.
private void updateView() {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
synchronized (viewSynchronizationObject) {
if (view == null) {
return;
}
MessagesPanelControl mpc = view.get();
if (mpc != null) {
mpc.updateView();
}
}
}
},false);
}
We should never block EDT from executing it's task other wise our application freezes. Please read my posted answer here for details on how the Swing event and rendering task is performed by EDT and EventQueue.
Although, your application logic is not known to us, you can remove the synchronized (viewSynchronizationObject) {} from invokeLater, instead you can put SwingUtilities.invokeLater(new Runnable() {} inside this synchronized block:
synchronized (viewSynchronizationObject)
{
SwingUtilities.invokeLater(new Runnable() { /* your code */ } );
}

Java swing - when cancel button clicked don't loop

I have a gui, that is having a Login prompt added.
while(notValidLogIn){
LoginPrompt.getDetails() //a static method that
}
Hwoever, the loginPrompt is a Jdialog, with a parent JFrame. How can I stop looping of cancel clicked, I could put System.exit(0) in cancel action performed. But don't want to stop everything, I want something like :
while(notValidLogIn && LoginPrompt.isNotCancelled()){
LoginPrompt.getDetails(); //a static method that creates an instance of login JDialog()
}
In a recent project I was working on, I've implemented an event based solution. The idea is JDialog notify to its parent JFrame how the login process went and this last one may or may not continue its execution. This way I have no loops and keep separate responsibilities: The schema would be something like this:
LoginEvent:
This is the event itself. Not that complicated:
class LoginEvent extends EventObject {
public static final int LOGIN_SUCCEEDED = 0;
public static final int LOGIN_FAILED = 1;
public static final int LOGIN_DIALOG_CLOSED = 2;
private int id;
public LoginEvent(Object source, int id) {
super(source);
this.id = id;
}
public int getId() {
return id;
}
}
LoginListener
An interface to handle these LoginEvents:
public interface LoginListener extends EventListener {
public void handleLoginEvent(LoginEvent evt);
}
Login Dialog
This class has to mantain a List with subscribed LoginListeners:
class LoginDialog {
List<LoginListener> listeners = new ArrayList<>();
JDialog dialog;
JButton accept;
JButton cancel;
public void show() {
//create and show GUI components
}
public void close() {
if(dialog != null) {
dialog.dispose();
}
}
...
public void addLoginListener(LoginListener loginEventListener) {
if(!listeners.contains(loginEventListener)) {
listeners.add(loginEventListener);
}
}
public void removeLoginListener(LoginListener loginEventListener) {
listeners.remove(loginEventListener);
}
public void dispatchLoginEvent(LoginEvent evt) {
for(LoginListener loginListener: listeners) {
loginListener.handleLoginEvent(evt);
}
}
}
Adding action listeners to accept and cancel buttons:
accept.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// validate login data
if(loginValid) {
dispatchLoginEvent(new LoginEvent(dialog, LoginEvent.LOGIN_SUCCEEDED));
} else {
dispatchLoginEvent(new LoginEvent(dialog, LoginEvent.LOGIN_FAILED));
}
}
});
cancel.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
dispatchLoginEvent(new LoginEvent(dialog, LoginEvent.LOGIN_DIALOG_CLOSED));
}
});
Subscribing a LoginListener
In your JFrame:
final LoginDialog dialog = new LoginDialog();
dialog.addLoginListener(new LoginListener() {
#Override
public void handleLoginEvent(LoginEvent evt) {
if(evt.getId() == LoginEvent.LOGIN_SUCCEEDED {
dialog.close();
//continue execution
return;
}
if(evt.getId() == LoginEvent.LOGIN_FAILED) {
JOptionPane.showMessageDialog(null, "Login failed!");
return;
}
if(evt.getId() == LoginEvent.CLOSE_LOGIN_DIALOG) {
dialog.close();
// do something when this dialog is closed
}
}
};
dialog.show();
while(notValidLogIn && LoginPrompt.isNotCancelled()){
LoginPrompt.getDetails(); //a static method that creates an instance of login JDialog()
}
If this loop is inside another thread other than the EDT(event dispatch thread), then you can use SwingUtilities.invokeAndWait(new Runnable()) function: invokeAndWait() blocks the current thread until the EDT is done executing the task given by it. This option is particularly used while we want to await an execution of a thread for taking confirmation from user or other input using JDialogue/JFileChooser etc
while(notValidLogIn && LoginPrompt.isNotCancelled()){
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
LoginPrompt.getDetails() ;
}
});
}
Note: re-stating for emphasizing: you should ensure that this loop is executing inside another Thread: such as using an extended class of Runnable, or by means of anonymous class:
new Thread()
{
// other code of your context
public void run()
{
while(notValidLogIn && LoginPrompt.isNotCancelled()){
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
LoginPrompt.getDetails() ;
}
});
}
}
}.start();

JList content not updated

I have a jList object named jListCustSearchResults which holds a number of CustomerEntity objects and shows them as a list for a user to to select a customer.
The first method below is an actionperformed mthod of a JButton which triggers the update sequence of this JList when clicked. It invokes another function below named fillCustomerList to refill the JList object with the new Customers retrieved from the database.
The problem here is that so mentioned jList object is not updated in the gui. Instead it's completely empty. As an alternative solution, I placed the method refillCustomerList into a SwingWorker object within its doBackground method in order that the update does not happen in EDT. However, the jLIst is still not updated with the new content on the GUI. Why do you think it's not updated?
At the bottom of this message I placed the SwingWorker variant of my implementation. The jList is still not updated in the gui (I also invoked repaint()).
private void jTextFieldCustomerSearchWordActionPerformed(ActionEvent evt) {
int filterType = jComboBoxSearchType.getSelectedIndex() + 1;
String filterWord = jTextFieldCustomerSearchWord.getText();
try {
controller.repopulateCustomerListByFilterCriteria(filterType, filterWord);
} catch (ApplicationException ex) {
Helper.processExceptionLog(ex, true);
}
refillCustomerList();
}
private void refillCustomerList() {
if (jListCustSearchResults.getModel().getSize() != 0) {
jListCustSearchResults.removeAll();
}
jListCustSearchResults.setModel(new javax.swing.AbstractListModel() {
List<CustomerEntity> customerList = controller.getCustomerList();
#Override
public int getSize() {
return customerList.size();
}
#Override
public Object getElementAt(int i) {
return customerList.get(i);
}
});
jListCustSearchResults.setSelectedIndex(0);
}
==========================
WITH SWING WORKER variant:
private void jTextFieldCustomerSearchWordActionPerformed(ActionEvent evt) {
SwingWorker worker = new SwingWorker<Void, Void>() {
#Override
public void done() {
repaint();
}
#Override
protected Void doInBackground() throws Exception {
int filterType = jComboBoxSearchType.getSelectedIndex() + 1;
String filterWord = jTextFieldCustomerSearchWord.getText();
try {
controller.repopulateCustomerListByFilterCriteria(filterType, filterWord);
} catch (ApplicationException ex) {
Helper.processExceptionLog(ex, true);
}
refillCustomerList();
return null;
}
};
worker.execute();
}

updating a JProgressBar while processing

I know the subject has already been seen on many Questions and has been answered, but still, I can't get trough it.
I just want to update a progressBar while extracting some stuff of a large xml file.
I thought it was enough to have the time-consuming loop in a different thread but ?..
All I managed to get is the progressBar either not showed at all, or updated at the end, just before it's closed.
Instanced somewhere near the launch of the application, I have:
public class SomeClass {
private SomeClass () {
myXMLParser reader = new myXMLParser();
CoolStuff fromXml = reader.readTheXml();
}
}
while showing and updating a JDialog with a JProgressBar:
public class LoadingDialog extends JDialog {
private JProgressBar progressBar;
/* ... */
public void progress() {
progressBar.setValue(progressBar.getValue() + 1);
}
}
So I have this myXMLParser:
public class myXMLParser {
private LoadingDialog loadingDialog = new LoadingDialog();
public CoolStuff readTheXml() {
CoolStuff fromXml = new CoolStuff();
while(manyIterations) {
loadingDialog.progress();
fromXml.add(some() + xml() + reading());
}
return fromXml;
}
}
I have seen many things with SwingWorker and using PropertyChange events update the progressBar, but examples are always given all-in-one, with the processing and the progressbar within the same class, and with classes within classes, and since I begin in Java, I wasn't able to translate that to my situation.
Any help ?.. Any (not too obvious) advices ?
Edit: So thanks to btantlinger it worked like that:
public class SomeClass {
private SomeClass () {
myXMLParser reader = new myXMLParser();
new Thread(new Runnable() {
#Override
public void run() {
CoolStuff fromXml = reader.readTheXml();
}
}).start();
}
}
public class LoadingDialog extends JDialog {
private JProgressBar progressBar;
/* ... */
public void progress() {
progressBar.setValue(progressBar.getValue() + 1);
}
}
public class myXMLParser {
private LoadingDialog loadingDialog = new LoadingDialog();
public CoolStuff readTheXml() {
CoolStuff fromXml = new CoolStuff();
while(manyIterations) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
loadingDialog.progress();
}
});
fromXml.add(some() + xml() + reading());
}
return fromXml;
}
}
You MUST update the JProgress bar on the Swing Event Dispatch Thread. You cannot modify Swing components on any other thread.
Your only other alternative would be to set the JProgress bar "indeterminate" before you start your thread where the progress bar will just go back and forth.
E.g
progBar.setIndeterminate(true);
See the SwingWorker javadoc:
http://docs.oracle.com/javase/6/docs/api/javax/swing/SwingWorker.html
If you don't want to use the SwingWorker, another option is the SwingUtilities.invokeLater method
//inside your long running thread when you want to update a Swing component
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//This will be called on the EDT
progressBar.setValue(progressBar.getValue() + 1);
}
});
In addition to the code provided by #btantlinger, I found after testing that it required an additional line of code in order to update the progress bar on the UI thread while processing. See below.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
progressBar.setValue((int)percentage);
//below code to update progress bar while running on thread
progressBar.update(progressBar.getGraphics());
}
});

Categories

Resources