I can load data from query result in JTable nicely.
SELECT * FROM 'customer info';
Make a result set and place to JTable is easy to handle.
But problem is, as my database table is so big and it takes so much time to load. My application totally freeze until current task complete. I know swing worker can perform a background task. I study this, but i find no any appropriate solution in my case. Because i already write a lot of function in basic way.
So finally what i need?
I need a Class, design with swing worker by which i can easily use it's object anywhere in my application. suppose i send my query string and JTable in this class constructor. Then it automatically starts a background thread to make result set and place it in JTable.
ok now its work in my case :) here is my own solution ....
public class UsableSwingWorkerThread extends SwingWorker<ResultSet, Object> {
JTable table;
String query;
public UsableSwingWorkerThread(JTable table, String query) {
this.table = table;
this.query = query;
}
#Override
protected ResultSet doInBackground() throws Exception {
return DatabaseFunctionClass.con.prepareStatement(query).executeQuery();
}
void loadTable(TableModel tb) {
new UsableDefaultLoadTable(tb, table);
}
#Override
protected void done() {
try {
loadTable(DbUtils.resultSetToTableModel(get()));
} catch (Exception ignore) {
StaticAccess.showMassageDialog(StaticAccess.mainFrame, "Fail to load. try again later..", ignore);
}
}
}
Related
I have lot of things implemented in ComponentAdapter of Java. Since it does loading data from database and displaying in JTable, I added it into another thread. I will show you one method which is being called by such ComponentAdapter
private class DisplayInitialRevenue_Thread implements Runnable
{
#Override
public void run()
{
displayInitialRevenue_Method();
}
}
private void displayInitialRevenue_Method()
{
//Get the dates from the combo
String selectedCouple = revenueYearCombo.getSelectedItem().toString();
if(selectedCouple.equals("Select Year"))
{
return;
}
String[] split = selectedCouple.split("/");
//Related to DB
double totalamountInvested;
//Get data from the database
dbConnector = new DBHandler();
dbConnector.makeConnection();
DefaultTableModel model = (DefaultTableModel) initialRevenueTable.getModel();
model.setRowCount(0);
ResultSet selectAllDetails = dbConnector.selectAllDetails("SQL CODE HERE ");
try
{
if(selectAllDetails.isBeforeFirst()==false)
{
JOptionPane.showMessageDialog(null,"This table is empty");
}
else
{
while(selectAllDetails.next())
{
String clientName = selectAllDetails.getString("Client Name");
String providerName = selectAllDetails.getString("Provider Name");
Double amountInvested = selectAllDetails.getDouble("Invest_Amount");
//Get Other Data
//Update the table
Object[]row = {dateS,clientName,providerName,amountInvested};
model.addRow(row);
//Get the total
amountInvested = amountInvested+amountInvested;
}
//Add the sum
Object[]blankRow = {null,null,null,null};
model.addRow(blankRow);
Object[]row = {dateS,clientName,providerName,amountInvested};
}
}
catch(SQLException sql)
{
JOptionPane.showMessageDialog(null,sql.getLocalizedMessage());
}
}
And, this above thread can be called in 3 ways. That is by ItemListener attached to a JComboBox, ActionListener attached to a JMenuand ComponentListener.
ComponentListener
private class DisplayInitialRevenue extends ComponentAdapter
{
public void componentShown(ComponentEvent e)
{
formMemorizer = FormMemorizer.Initial_Revenue;
//displayInitialRevenue_Method();
DisplayInitialRevenue_Thread t = new DisplayInitialRevenue_Thread();
t.run();
}
}
ItemListener
private class RevenueYearComboAction implements ItemListener
{
#Override
public void itemStateChanged(ItemEvent e)
{
if(e.getStateChange() == ItemEvent.SELECTED)
{
int selection = formMemorizer;
if(selection==-1)
{
return;
}
else if(selection==FormMemorizer.Initial_Revenue)
{
//displayInitialRevenue_Method();
DisplayInitialRevenue_Thread t = new DisplayInitialRevenue_Thread();
t.run();
}
}
}
I have lot of these kind of methods to get the data from the database and feed the JTables and take data from GUI and save in database.
Now my question is, all of these are freezing sometimes, whenever a database call occurred. I thought it is bcs of Thread issue so I made the above DisplayInitialRevenue_Thread to call displayInitialRevenue_Method() as a test. Then I only invoked the area related to the call this method but it still freezes sometimes! My other database methods are not in separate threads, but this is method is, so why even calling "only" this method lead this to freeze? It is in a thread!
For side note, I am in Java 8, using MySQL Server version: 5.6.16 - MySQL Community Server (GPL) which comes with XAMPP.
Call t.start() to start a new Thread, calling Thread#run does nothing more then calls the run method of the Thread within the same thread context...
Having said that, Swing is not thread safe, Swing requires that all updates to the UI are made from within the context of the Event Dispatching Thread. Instead of using a Thread, you should consider using a SwingWorker, which allows you to execute long running tasks in a background thread, but which provides easy to use publish/process methods and calls done when it completes, which are executed within the context of the EDT for you.
See Worker Threads and SwingWorker for more details
I have lot of things implemented in ComponentAdapter of Java. Since it does loading data from database and displaying in JTable, I added it into another thread. I will show you one method which is being called by such ComponentAdapter
private class DisplayInitialRevenue_Thread implements Runnable
{
#Override
public void run()
{
displayInitialRevenue_Method();
}
}
private void displayInitialRevenue_Method()
{
//Get the dates from the combo
String selectedCouple = revenueYearCombo.getSelectedItem().toString();
if(selectedCouple.equals("Select Year"))
{
return;
}
String[] split = selectedCouple.split("/");
//Related to DB
double totalamountInvested;
//Get data from the database
dbConnector = new DBHandler();
dbConnector.makeConnection();
DefaultTableModel model = (DefaultTableModel) initialRevenueTable.getModel();
model.setRowCount(0);
ResultSet selectAllDetails = dbConnector.selectAllDetails("SQL CODE HERE ");
try
{
if(selectAllDetails.isBeforeFirst()==false)
{
JOptionPane.showMessageDialog(null,"This table is empty");
}
else
{
while(selectAllDetails.next())
{
String clientName = selectAllDetails.getString("Client Name");
String providerName = selectAllDetails.getString("Provider Name");
Double amountInvested = selectAllDetails.getDouble("Invest_Amount");
//Get Other Data
//Update the table
Object[]row = {dateS,clientName,providerName,amountInvested};
model.addRow(row);
//Get the total
amountInvested = amountInvested+amountInvested;
}
//Add the sum
Object[]blankRow = {null,null,null,null};
model.addRow(blankRow);
Object[]row = {dateS,clientName,providerName,amountInvested};
}
}
catch(SQLException sql)
{
JOptionPane.showMessageDialog(null,sql.getLocalizedMessage());
}
}
And, this above thread can be called in 3 ways. That is by ItemListener attached to a JComboBox, ActionListener attached to a JMenuand ComponentListener.
ComponentListener
private class DisplayInitialRevenue extends ComponentAdapter
{
public void componentShown(ComponentEvent e)
{
formMemorizer = FormMemorizer.Initial_Revenue;
//displayInitialRevenue_Method();
DisplayInitialRevenue_Thread t = new DisplayInitialRevenue_Thread();
t.run();
}
}
ItemListener
private class RevenueYearComboAction implements ItemListener
{
#Override
public void itemStateChanged(ItemEvent e)
{
if(e.getStateChange() == ItemEvent.SELECTED)
{
int selection = formMemorizer;
if(selection==-1)
{
return;
}
else if(selection==FormMemorizer.Initial_Revenue)
{
//displayInitialRevenue_Method();
DisplayInitialRevenue_Thread t = new DisplayInitialRevenue_Thread();
t.run();
}
}
}
I have lot of these kind of methods to get the data from the database and feed the JTables and take data from GUI and save in database.
Now my question is, all of these are freezing sometimes, whenever a database call occurred. I thought it is bcs of Thread issue so I made the above DisplayInitialRevenue_Thread to call displayInitialRevenue_Method() as a test. Then I only invoked the area related to the call this method but it still freezes sometimes! My other database methods are not in separate threads, but this is method is, so why even calling "only" this method lead this to freeze? It is in a thread!
For side note, I am in Java 8, using MySQL Server version: 5.6.16 - MySQL Community Server (GPL) which comes with XAMPP.
Call t.start() to start a new Thread, calling Thread#run does nothing more then calls the run method of the Thread within the same thread context...
Having said that, Swing is not thread safe, Swing requires that all updates to the UI are made from within the context of the Event Dispatching Thread. Instead of using a Thread, you should consider using a SwingWorker, which allows you to execute long running tasks in a background thread, but which provides easy to use publish/process methods and calls done when it completes, which are executed within the context of the EDT for you.
See Worker Threads and SwingWorker for more details
In my application, I load a table with data.
In this tab, one column come from a webservice which can make some times to answer.
So I treat this one in pool thread to avoid to block the screen like this:
final ObservableList<StockListBean> list = FXCollections
.observableArrayList();
list.addAll(stocksListMService.getStocksListRunning());
stocksList.setItems(list);
final ExecutorService executor = Executors.newFixedThreadPool(4);
for (final StockListBean stockListBean : list) {
executor.execute(new Task<Float>() {
#Override
protected Float call() throws Exception {
logger.debug("In jfx task for {}", stockListBean.getCode());
((StockListRunningBean)stockListBean).setActualPrice(stocksListMService.getActualPrice(stockListBean.getCode()));
columnActualPrice.setVisible(false);
columnActualPrice.setVisible(true);
return 0f;
}
});
}
Threads are well execute and data are well set in beans but I don't reach to refresh the tableView.
I try code in the snapshot. I try many other ways found on the web but nothing to do, the column is desperately empty.
If I keep the thread loop but without execute the service and set a default value, the column is not empty.
It's such a real problem with screen refresh.
How can I refresh this ?
Thanks.
Assuming your StockListRunningBean uses JavaFX observable properties, so that the TableView sees the changes, you shouldn't need to do anything additional to update the table. One problem with your code is that you're making changes to the UI (via changes to the StockListRunningBean price property) from a thread that's not the FX Application Thread.
Try this refactoring:
for (final StockListBean stockListBean : list) {
final int code = stockListBean.getCode(); // assuming int, change as required
final Task<Float> task = new Task<Float>() {
#Override
protected Float call() throws Exception {
logger.debug("In jfx task for {}", code);
return stocksListMService.getActualPrice(code);
}
};
task.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
#Override
public void handle(WorkerStateEvent event) {
((StockListRunningBean)stockListBean).setActualPrice(task.getValue());
}
});
executor.execute(task);
}
Again, this assumes that your StockListRunnerBean has a
public FloatProperty actualPriceProperty() {...}
method and that the table column is properly bound to it.
So I have been trying to implement a progress indicator with no luck. I am not sure I understand managing threads with JavaFx very well, despite having read a bit about the Platform.RunLater and Tasks. So here is my use case.
My program allows users to connect to a database and look at some of the schemas and other objects in the database. Sometimes connecting to a large database and pulling up all its tables and info takes a while, so I would like to show a progress indicator. I am not trying to update the progress at all I would just like to make the progress indicator visible at a value of -1 while the process is running to pull everything from the database. Ideally I will have a progress indicator loaded in from an FXML file invisible. When I start the process of pulling info from the database I would like to make it visible.
When trying to make my progress visible it never showed up, so I decide to start out having it visible and making it invisible, just to see what happens. The progress indicator rotated nicely when I opened the program up, but as soon as I try to connect to the database it stopped rotating and just froze. I assume this is what happens when I try to make it visible too which is why it was never showing up.
The following is my current code, I would appreciate any detailed help with explanations so I can understand what is going on. Thanks
from the method that is doing most of the work.
//make progress indicator visible
pi.setVisible(true);
// separate non-FX thread
ExtractorThread t = new ExtractorThread();
t.setCp(cp);
t.start();
//Wait until the thread is done
try{
t.join();
}
catch(Exception e){
e.printStackTrace();
}
//Retrieve the dbextractor from the thread
DbExtractor dbe = t.getDbe();
//move on to the next page in the application
this.caster.goToDataSource(c, cp, dbe);
The ExtractorThread which does the work.
private class ExtractorThread extends Thread{
private ConnectionProperties cp;
private DbExtractor dbe;
public void run() {
dbe = new DbExtractor(cp);
try {
dbe.extract();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public DbExtractor getDbe() {
return dbe;
}
public void setCp(ConnectionProperties cp) {
this.cp = cp;
}
}
If I am supposed to use the Platform.RunLater I am not sure where to use it or why. Any help would be appreciated, thanks!
Use the javafx.concurrent API. Extend Task instead of Thread:
private class ExtractorThread extends Task<DbExtractor>{
private ConnectionProperties cp;
public DbExtractor call() throws Exception {
dbe = new DbExtractor(cp);
dbe.extract();
return dbe;
}
public void setCp(ConnectionProperties cp) {
this.cp = cp;
}
}
Then do:
//make progress indicator visible
pi.setVisible(true);
// separate non-FX thread
final ExtractorThread t = new ExtractorThread();
t.setCp(cp);
t.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
public void handle(WorkerStateEvent event) {
DbExtractor dbExtractor = t.getValue();
this.caster.goToDataSource(c, cp, dbe);
}
});
t.setOnFailed(...); // similarly, to handle exceptions
new Thread(t).start();
I don't code JavaFX, and so I can't give you chapter and verse, but this line:
t.join();
will block the calling code until the background thread is through. Don't do this. Instead use some type of listener to get notified when the background thread finishes. If this were Swing, I'd use a PropertyChangeListener added to a SwingWorker to notify me when the background thread was done. I think that you can still use a PropertyChangeListener to do a similar thing with with JavaFX, but I cannot tell you if this would represent the canonical solution.
Also, don't extend Thread but instead implement Runnable. This won't fix your problem but is basic Java common sense.
In my Java GUI app I have a JButton and when clicked it calls a function to connect to a database, then calls a function to clear a table in the DB, then calls a function that reads text from one file and loads variables, which calls a function that reads text from another file, compares the data from both and then calls a function to either update or insert data in the DB, all of that works fine.
However my question is related to the JButton, when its clicked I want to run a Indeterminate progress bar just so the user knows work is being done and then right before it leaves the the action listener setIndeterminate to false and set the value of the progress bar to 100(complete), but in my case when you click the button it stays in the clicked state and the progress bar freezes.
What should I implement to prevent this? threading possibly? but Im quite new to threading in java. here is my action listener:
private class buttonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if( e.getSource() == genButton )
{
progressBar.setIndeterminate(true);
progressBar.setString(null);
try
{
dbConnect(); //connects to DB
clearSchedules(); // deletes data in tables
readFile(); // reads first file and calls the other functions
dbClose();// closes the DB
progressBar.setIndeterminate(false);
progressBar.setValue(100);
}
catch (Exception e1){
System.err.println("Error: " + e1.getMessage());
}
}
}
}
On a side note, I would like to have the action bar actually move as the the program progresses but I wasnt sure how to monitor its progress.
Thanks, Beef.
UPDATE here is my example of SwingWorker and how I used it:
Declared globally
private functionWorker task;
private abstract class functionWorker extends SwingWorker {
public void execute() {
try {
dbConnect();
} catch (SQLException e) {
e.printStackTrace();
}
clearSchedules();
try {
readFile();
} catch (IOException e) {
e.printStackTrace();
}
dbClose();
}
}
Inside my actionPerformed method
if( e.getSource() == genButton )
{
progressBar.setIndeterminate(true);
progressBar.setString(null);
try
{
task.execute();
progressBar.setIndeterminate(false);
progressBar.setValue(100);
}
catch (Exception e1){
System.err.println("Error: " + e1.getMessage());
}
}
The problem is probably related to connecting to doing expensive operations in the UI thread (connecting to a database, reading from a file, calling other functions). Under no circumstances should you call code that uses excessive CPU time from the UI thread, as the entire interface can't proceed while it is executing your code, and it results in a 'dead' looking application, with components remaining in their state at the time before an expensive operation until completion. You should execute another thread, do the expensive work in that, and then use a SwingUtilities.invokeLater(Runnable doRun) with a passed runnable where you'd update the progress.
There may be synchronisation issues relating to the states of components, but you can fix these later.
Could I create the new thread when the action is performed and call the new functions in the thread, or should I do the threading within the actual function itself?
You can start a SwingWorker from your button's handler, as shown here. A related example implementing Runnable is seen here.
One method to handle progressbars are to extend SwingWorker in a class.
SwingWorker takes care of running background tasks for you and so you do not have to implement your own threading that can end up in unknown issues.
To begin with, your class that takes care of progress bar UI should implement PropertyChangeListener
And implement public void propertyChange(PropertyChangeEvent evt) { - to update the progressbar status based on a global variable.
The background task class should look like the following(this could be an inner class) :
class ProgressTask extends SwingWorker<Void, Void> {
#Override
public Void doInBackground() {
//handle your tasks here
//update global variable to indicate task status.
}
#Override
public void done() {
//re-enabled your button
}
}
on your button's event listener :
public void actionPerformed(ActionEvent evt) {
//disable your button
//Create new instance of "ProgressTask"
//make the task listen to progress changes by task.addPropertyChangeListener(this);
//calll task.execute();
}
I have tried to water down code example, you would have to read some tutorial to understand how all these pieces fit together. However, the main point is do not code your Threads, instead use SwingWorker
progressBar.setIndeterminate(true);
progressBar.setValue(0);
dbConnect(); //connects to DB
progressBar.setIndeterminate(true);
progressBar.setValue(10);
clearSchedules(); // deletes data in tables
progressBar.setIndeterminate(true);
progressBar.setValue(50);
readFile(); // reads first file and calls the other functions
progressBar.setIndeterminate(true);
progressBar.setValue(75);
dbClose();// closes the DB
progressBar.setIndeterminate(false);
progressBar.setValue(100);
You will need to tell the progress bar how much progress has been made because it does not know the percentage completed. Better yet, write a method that updates and repaints the progress bar rather than repeating the method calls here.
updateProgressBar(int progress, boolean isDeterminate, String msg){};
You will also need to make sure that your specific button is firing the action performed.
class IvjEventHandler implements java.awt.event.ActionListener {
public void actionPerformed(java.awt.event.ActionEvent e) {
if (e.getSource() == JMyPanel.this.getJButtonUpdate())
connEtoC1(e);
};
};
The connEtoC1(e); should execute a controller class or SwingWorker rather than firing from the GUI