Created
March 6, 2024 17:49
-
-
Save nhtzr/36582062a2f8f301b51a1b26c2dd7cbf to your computer and use it in GitHub Desktop.
Example for a wizard made using cardLayout in java swing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
import javax.swing.event.AncestorEvent; | |
import javax.swing.event.AncestorListener; | |
import java.awt.*; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
public class Wizard { | |
private final CardLayout wizardCardLayout; | |
private final JPanel wizardContentPanel; | |
private final JButton nextButton; | |
private final JButton previousButton; | |
private final JPanel mainPanel; | |
public Wizard() { | |
mainPanel = new JPanel(); | |
mainPanel.setLayout(new BorderLayout(0, 0)); | |
wizardContentPanel = new JPanel(); | |
wizardCardLayout = new CardLayout(0, 0); | |
wizardContentPanel.setLayout(wizardCardLayout); | |
mainPanel.add(wizardContentPanel, BorderLayout.CENTER); | |
JPanel wizardControlPanel = new JPanel(); | |
wizardControlPanel.setLayout(new BorderLayout(0, 0)); | |
mainPanel.add(wizardControlPanel, BorderLayout.SOUTH); | |
previousButton = new JButton(); | |
previousButton.setEnabled(true); | |
previousButton.setText("Previous"); | |
wizardControlPanel.add(previousButton, BorderLayout.WEST); | |
nextButton = new JButton(); | |
nextButton.setText("Next"); | |
wizardControlPanel.add(nextButton, BorderLayout.EAST); | |
nextButton.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
wizardCardLayout.next(wizardContentPanel); | |
Wizard.this.onCardChange(); | |
} | |
}); | |
previousButton.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
wizardCardLayout.previous(wizardContentPanel); | |
Wizard.this.onCardChange(); | |
} | |
}); | |
wizardContentPanel.addAncestorListener(new AncestorListener() { | |
@Override | |
public void ancestorAdded(AncestorEvent event) { | |
onCardChange(); | |
} | |
@Override | |
public void ancestorRemoved(AncestorEvent event) { | |
} | |
@Override | |
public void ancestorMoved(AncestorEvent event) { | |
} | |
}); | |
} | |
public static void main(String[] args) { | |
Wizard wizard = new Wizard(); | |
// Use your panels | |
wizard.addComponent(new Hello().getPanel1(), "Card 1"); | |
wizard.addComponent(new Howareyou().getPanel1(), "Card 2"); | |
wizard.addComponent(new FineThankyou().getPanel1(), "Card 3"); | |
JFrame frame = new JFrame("Wizard"); | |
frame.setContentPane(wizard.mainPanel); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.pack(); | |
frame.setVisible(true); | |
} | |
public void addComponent(Component component, String cardName) { | |
this.wizardContentPanel.add(component, cardName); | |
} | |
public void onCardChange() { | |
Component first = wizardContentPanel.getComponent(0); | |
Component last = wizardContentPanel.getComponent(wizardContentPanel.getComponentCount() - 1); | |
previousButton.setEnabled(!first.isVisible()); | |
nextButton.setEnabled(!last.isVisible()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment