I am creating a Form for restaurant management which has a JTable showing order details. The form is connected to mysql database 'Restaurant' in which 'orders' table is present.
Now when new data is added to table 'Orders' in 'Restaurant' database I want the form to update automatically.
How can I achieve this?
Easiest solution is to create a java.util.Timer or javax.swing.Timer and assign an ActionEvent to it to check for changes in set intervals.
One of the possible solutions is using Listener Pattern.
When customer changes something in database, CustomerJFrame can notify it's listeners, that data has changed. OrdersJFrame can listen to CustomerJFrame and fetch needed data from db on notify
Related
I am currently writing a program which retrieves data from a SQL database and uses the data to populate a pie-chart on a JavaFX scene. So far, this has been a success.
What I am wondering is if it is possible to repopulate and refresh the pie chart as new data is entered into the database. So far the only solution I have found is to close the program and reopen it, which is not really much of a solution.
I can provide my code if needed.
Grateful for any tips you can give, thanks!
What I am wondering is if it is possible to repopulate and refresh the pie chart as new data is entered into the database.
The beauty of graphs/Charts in FX is that they are all dynamic, and change.
if you add new data to your dataCollection, or set your entire database to PieChart.setData(data);
You would have to set up a listener to listen in on your DB and changes, or listen to the changes on whatever is changing your DB. Is it external or internal to your Application?
If it's Internal than you can set up the listener with a flag and do it then, if it's External then you will either
have to poll the database x seconds/minutes
somehow set up SQL code to update something in your application and
set up a listener there (such as How to implement a db listener in Java)
Let's say you have a database table name table1 with columns name and surname. Two different clients open the same view from the java application and get the data for same person at the same time.
1) First client changed the name and pressed save button to update database record.
2) Client2 still sees the old record on the screen and then pressed to save button to change the surname.
It actually overwrite the record by old name. I think to check and get the latest data before updating the database when I click button but I do not like this solution because of making a second query before update.
So how can we solve this problem by using Hibernate and without using Hibarnete. Any idea?
Of course if you do not want that something will be overridden, then you have to check the data before an update. But it will be not always a real query with a good caching strategy. You could also use a timestamp with last update to compare it easier. Another strategy would be to lock the entities when the first user will read them. But that is normally not a good design for web applications or you have to integrate a messaging service, which will all user inform for an update who actually have open that entity. But I think that is not so easy to implement and a more advanced feature.
In short, compare the timestamp of an entity and if already updated, then compare the changes and show them for the user who wanted update that entity.
Hello every one I m lost with this problem :
I have a Jtable and I fill data from mysql database ,
but I want the table to be refresh by a timer every
xx seconds. Really i don't know how to do ?
I would appreciate some direction/help.
Use a javax.swing.Timer, with setRepeats(true) so that it repeatedly fires an ActionEvent. In your ActionListener, run the query against the database. The easiest approach is to throw away your old table model, generate a new one from the new database results, and call setTableModel on your JTable. (A more sophisticated approach is to work out the differences to the previous query results and only update those table cells that have changed, but you may not need this refinement.)
So far, I talked about an ActionListener doing the work of regenerating the table model, but a more elegant solution might be to have a custom table model that knows how to update its own data from the database - and will do so, for example, on invocation of (say) an updateFromDatabase() method. So the TableModel has the responsibility of updating itself and the Timer/ActionListener has the responsibility of deciding when these updates should happen.
As already mentioned by Kalathoki, changes to the model layer are not seen in the JTable view component unless the model layer notifies the JTable that the model has changed. If you're calling setTableModel on JTable, then the JTable knows that the model has changed, but if you have a custom table model that (from the point of view of the JTable) 'secretly' refreshes itself from the database, then you need to make sure that the JTable is notified by calling the fireTableDataChanged() method.
Another consideration is how long the database refresh takes. You would not want your UI to freeze up for long periods while the JTable refreshes, so a further refinement would be to use a SwingWorker so that the database query is run in a background thread. In this case the Swing timer would be used to initiate the SwingWorker, and the SwingWorker would use its done() method to update the table model and fire an event to the JTable view component.
Use fireTableDataChanged() Method. Click Here fore more details
I have two radiobuttons(Say rbtn_Asia,rbtn_Europe)and one JTable. When I select rbtn_Asia, table must contains Asia's data. Similarly when I select rbtn_Europe, table must contains Europe's data. (Asia's data and Europe's data is in same database which will be updated periodically). I have implemented upto this.
My problem is like this: Consider the following case: I have selected rbtn_Asia and obviously table will contain Asia's data. Now let database has got two new tuples of Asia, how can I update the JTable dynamically without selecting the rbtn_Asia once again (because rbtn_Asia is already in selected state).
In your button handler, update your implementation of TableModel, which should then fire the appropriate event. A structure that supports clear() such as Map, shown here, is convenient. More examples may be found here.
Skimmed through the Eclipse API doc and found no mentioning of this event. Could I have missed something? I'm building something that lists all table columns in a given table, and it needs to be automatically updated if the set of columns in the Table changes, so I need to listen to the Table structure changes and update my list accordingly.
What you are looking for doesn't exist. You could add dispose listeners to the TableColumns, and that could tell you when one is removed, but there are no events for telling you when widgets are created. You might be able to "fake it" by tracking control events on the table columns with TableColumn#addControlListener, but this is iffy. If you have control of the tables you could do something more advanced by wrapping them in a dynamic proxy that could perform that function for you.