java desktop "ajax" exists?

Asked

Viewed 168 times

5

I wonder if there is any way to return the value to my java desktop application every time a value is added to a table. Something like ajax for browser.

For example. We have the requested table and each record that is added to it my application screen is updated displaying this value (without needing to manually update the screen or having to close it and open it again).

Do you understand my doubt? don’t know how to search for it? someone has already gone through it and can give me a light?

  • Yes, it is possible. Simply update your jtable’s tablemodel as soon as the insertion is done, this way, every time you delete, edit, or add an item in the table, the tablemodel will update as well. Behold this example, try to edit the names.

  • This when my application sends the value, but when the value is entered into the bank by a third party (not me)... there is some way?

  • Stay "listening" to the table and so q is added some value me return that value?

  • Third? Desktop application usually has its own basis together with the application, its basis is remote?

  • Yes, the value will be entered by android in the bank and my application should display the value

  • It will be like this, the app on android sends an order with products to the database and my desktop application will be the counter, which should display the order as soon as it is registered in the database.

  • It’s a desktop app and an android app sharing a remote base?

  • Well, in this case I don’t know any listerner that monitors without having to run manually. Maybe using some routine, but it’s best to wait for someone with more expertise to help you.

  • blz, thanks @Diegofelipe

  • If you know some English, see this problem very similar to your Soen

Show 5 more comments

2 answers

7

There are several ways to solve synchronization between multiple clients from one data source. Some are simple and inefficient, others efficient but complex. I will cite two approaches.

Periodic check

Marcos' answer uses this technique. In it you create a Timer or an object that runs at regular intervals, where you then update the values from the server.

This is the simplest and least efficient method. If there is too much data, the system will get blocked frequently and annoy the user. Also, it will transfer a lot of data unnecessarily if they have not changed between one execution and another. This technique is unviable if you need updates in "real time".

One way to improve this would be to run a lighter query to check if there has been any change. For example:

  • If only the number of records matters, you can make a count table and only update the values if the result is different from the previous one
  • Add a field with the creation or modification date of the records and only refer to the records added after the last table update
  • Make pagination, just bring the last ones n records.

Standard Observer

A more complex scenario would be the implementation of a modification listening mechanism.

For this, no external system should modify the database directly. The Android app and other customers should always update the base via some API, such as a Restful Web Service.

This web service would allow customers to connect to it and receive event updates. So whenever a record was added, all the listeners or observers would be notified.

The desktop application, when started, would remotely connect to the service and keep that connection open, listening for events.

Note that this makes the application much more complex as it needs to deal with several different implementations, a public REST API, and possible communication gaps between client/server.

However, it may be the only way out in a more complex scenario, such as where there is need for instant updates or there are many customers who would overload the database with queries.

Be careful not to break user flow (usability)

Be careful when updating the screen automatically. If the user is looking at the records, or even editing one of them, and everything disappears from the screen, this will generate negative consequences for someone.

If the data is mutable, an idea is to warn the user that there are updates, but only update the table when they click on a button or link.

If the data is immutable, there is not so much problem. Just try to keep the user focus on the same record between updates. For example, if the user selected the record written "banana" and suddenly the focus is on "orange", it will get lost every time the screen changes.

Note on the home screen here from Stack Overflow in English how questions are updated to get an idea about this.

3


Use a Timer to redo queries and/or connection and/or your Jtables;

Something like

import javax.swing.Timer;
// (...)

Timer timer = new Timer(0, new ActionListener() {

   @Override
   public void actionRefresh(ActionEvent e) {
      updateRecords();
   }
});

timer.setDelay(30000); // refresh a cada 30 segundos
timer.start();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.