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();
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){