Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One approach would be to add an <code>AncestorListener</code> to each tab's content. Let the listener trigger the desired effect as the tab is added to or removed from visibility.</p> <p><img src="https://i.stack.imgur.com/8yWT3.png" alt="image"></p> <pre><code>import java.awt.EventQueue; import java.util.Date; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTabbedPane; import javax.swing.event.AncestorEvent; import javax.swing.event.AncestorListener; /** * @see http://stackoverflow.com/a/17993449/230513 */ public class Test { private void display() { JFrame f = new JFrame("Test"); final JTabbedPane jtp = new JTabbedPane(); jtp.add("One", createPanel()); jtp.add("Two", createPanel()); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(jtp); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } private JPanel createPanel() { JPanel panel = new JPanel(); final JLabel label = new JLabel(new Date().toString()); panel.add(label); panel.addAncestorListener(new AncestorListener() { @Override public void ancestorAdded(AncestorEvent event) { // start animation label.setText(new Date().toString()); } @Override public void ancestorRemoved(AncestorEvent event) { // stop animation } @Override public void ancestorMoved(AncestorEvent event) { } }); return panel; } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new Test().display(); } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload