I'm trying to change current visible in card layout with slide effect. But I see a flick at the start of slide which I'm not able to debug/solve. How can I avoid that flick?
Here is sample code to reproduce error:
import java.awt.CardLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new CardLayout());
JLabel label1 = new JLabel("Harry Joy");
panel.add(label1, "1");
JLabel label2 = new JLabel("Harsh Raval");
panel.add(label2, "2");
frame.add(panel);
frame.pack();
frame.setVisible(true);
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(500L);
} catch (InterruptedException e) {
e.printStackTrace();
}
label2.setLocation(label1.getX() + 10 - label1.getWidth(), label1.getY());
label1.setLocation(label1.getX() + 10, label1.getY());
label2.setVisible(true);
}
label2.setVisible(false);
CardLayout cl = (CardLayout)panel.getLayout();
cl.next(panel);
}
}
Here at first label1
("Harry Joy") is displayed. Then I make label2
("Harsh Raval") visible and try to change location of both to provide a slide effect. But what happening here is first time both labels are displayed on top of each other and then it starts to slide. How can I stop that, I mean displaying both labels on top of each other? You can get better idea of what I mean if you run it once.