Experts, need some help.
I have created JFrame in say class 'A' and have added JTabbedPane using NetBeans IDE, also have added a first JPanel to this JTabbedPane. On this JPanel, I have JCheckbox that adds and removes new tab (instance of JPanel) based on the checked/unchecked event. The panel being added & removed is defined in another say class 'B' that extends JPanel. This JPanel has a timer task which runs in a specific interval, get some data from REST resource and update the contents in the JPanel's body as shown below:
private void refreshAgentUtilizationData() {
TimerTask updateAgentDetailsTask = new TimerTask() {
@Override
public void run() {
agentObj.updateData();
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
memChart.repaint();
System.out.println("This is from Agent monitor timer task...");
}
});
}
};
agentMonTimer.scheduleAtFixedRate(updateAgentDetailsTask, 0, master_pollingInterval);
}
JCheckBox action performed (in class 'A') looks below,
Some details first:
AgentMon_ChartsUI
= Class that extends JPanel and being added to JTabbedPane i.e. class B
agentMonTabs
= JTabbedPane that resides in say class A
private void agentMonSwitchActionPerformed(java.awt.event.ActionEvent evt) {
if (agentMonSwitch.isSelected()) {
AgentMon_ChartsUI agentChartPane = new AgentMon_ChartsUI();
Icon agentIcon = new javax.swing.ImageIcon(getClass().getResource("/resources/abc.png"));
agentMonTabs.addTab("Agent runtime monitor", agentIcon, agentChartPane);
agentMonTabs.setSelectedIndex(agentMonTabs.indexOfTab("Agent runtime monitor"));
} else {
agentMonTabs.remove(agentMonTabs.indexOfTab("Agent runtime monitor"));
}
}
The problem is: I am not able to find graceful way to dispose the instance of JPanel which is removed on checkbox's uncheck event. When I uncheck it, I can see the tab is being successfully removed and it looks like the panel is now gone, but I can see that the System.out.println...
is still being executed in timer's job. This means that Jcheckbox > Uncheck simply removes the tab but does not dispose it.
I checked other questions on StackOverflow (this & this) and it is confirmed that once the references are set to null, the GC will take care of it. I am not sure in this case, how should I set the reference to null, as I am simply removing the panel from JTabbedPane. I monitored my application for good amount of time and I didn't see GC clearing it out. Am I looking at it correctly? What is correct & recommended way to dispose/nullify the panel which was removed from JTabbledPane?