CardLayout showing two panels, flashing

Here’s a problem: final Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { Don’t use a java.util.Timer in a Swing program as you will have threading problems. Instead use a Swing Timer. Also, you’re making Swing calls in background threads and using Graphics object obtained by calling getGraphics() on a component, two no-nos for Swing programs. … Read more

How to effectively use cardlayout in java in order to switch from panel using buttons inside various panel constructors

The short answer is don’t. The reasons for this is you’ll end having to expose the parent container and CardLayout to ALL your sub components, which not only exposes portions of your application to potential mistreatment, it tightly couples the navigation making it difficult to add/remove steps in the future… A better solution would be … Read more

how to change UI depending on combo box selection

CardLayout works well for this, as suggested below. import java.awt.BorderLayout; import java.awt.CardLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; /** @see http://stackoverflow.com/questions/6432170 */ public class CardPanel extends JPanel { private static final Random random = new Random(); private static final JPanel cards … Read more