JPanel with ComponentListener

I have a main JPanel, and its layout is installed on CardLayout.

The main JPanel has 4 cards: Card1JPanel, Card2JPanel, Card3JPanel, Card4JPanel.

I also have a SwingWorker class called "MySwingy" that does something forever and the loop flag is true.

When Card1JPanel VISIDLE / SHOWN I want to do work with MySwingy.

When Card1JPanel IS INVISIBLE / HIDE I want to stop the employee MySwingy;

Below is the code I have, and I am wondering if there is a better / cleaner way to solve the problem above. In the following code example, you will see that I use the ComponentListener for Card1JPanel to determine if it is displayed or hidden, but what if I have a lot of cards, each with its own ComponentListener event, do these event listeners slow down my application?

Thank you very much

public class Card1JPanel extends JPanel{ private MySwingy mySwingy; private JTable tableDatabase; public Card1JPanel(){ initComponents();//tableDatabase is drawn here among other things this.addComponentListener(new myListener()); } class myListener implements ComponentListener { @Override public void componentHidden(ComponentEvent e) { try{ mySwingy.stopExecuting();//A flag inside of the worker gets set to false that terminates the while(true) loop. }catch(NullPointerException ex){ } } @Override public void componentMoved(ComponentEvent e) { } @Override public void componentResized(ComponentEvent e) { } @Override public void componentShown(ComponentEvent e) { mySwingy = new MySwingy(tableDatabase); mySwingy.execute(); } } } 

EDIT: shows what mySwingy does:

MySwingy is used to analyze data from an SQL database and to update a table database when data changes in a database. The tableDatabase is located on Card1JPanel, and it is updated on the EDT from MySwingy using SwingUtilities.InvokeLater. In my application, I have many maps (JPanels) with JTables and SwingWorkers that update their JTables in EDT. Now I'm sure the GUI will freeze if all of these JTables are constantly updated on EDT with SwingWorkers. Therefore, how do I stop SwingWorker from updating JTable when its JPanel is not showing? This is my question.

 public class MySwingy extends SwingWorker<Void,Void>{ private JTable tableDatabase; private boolean isStopExecuting; private boolean isDatabaseDataChanged; public MySwingy(JTable tableDatabase){ this.tableDatabase = tableDatabase isStopExecuting = false; isDatabaseDataChanged = false; } public void stopExecuting(){ isStopExecuting = true; } @Override public Void doInBackground(){ while(isStopExecuting == false){ //Here is the code that parses some data from an SQL database and if the data in the SQL database //has changed the isDatabaseDataChanged boolean flag is set to true, //else isDatabaseDataChanged boolean flag is set to false; if(isDatabaseDataChanged == true){ isDatabaseDataChanged = false; SwingUtilities.InvokeLater(new Runnable(){ @Override public void run(){ //Update tableDatabase rows with new data } }); } } return null; } } 
+4
source share
1 answer

Do not switch the worker; let it work. Instead, conditionally update a view only when it isVisible() , either directly through process() , or indirectly through a property change.

Extras: some useful comments deserve a closer look.

  • As @Andrew notes, a lot depends on the job. It would be possible to develop a simulation model in the background thread, but any rendering should take place in the event dispatch thread .

  • I'm no less confused than @mKorbel; You asked to critically evaluate the solution without saying what problem it should solve.

  • @kleopatra notes that SwingWorker is designed to be run only once . Any attempt to control the doInBackground() loop from another thread will be inconvenient for synchronization. Also consider the expected delay of your task mentioned here . An alternative would be an instance of javax.swing.Timer , which provides the start() and stop() methods.

  • Updated question : if your employee needs to interrogate a variable delay data source, use java.util.Timer in the background thread to execute a trigger request at a steady pace.

+4
source

Source: https://habr.com/ru/post/1433083/


All Articles