Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Override a frames next/prev buttons using cardLayout
- public class MainFrame {
- private static void createAndShowGUI() {
- JFrame frame = new JFrame("Dimensions helper");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLocationRelativeTo(null);
- final JPanel contentPane = new JPanel();
- contentPane.setLayout(new CardLayout(200, 200));
- Window1 win1 = new Window1();
- contentPane.add(win1);
- Window2 win2 = new Window2();
- contentPane.add(win2);
- Window3 win3 = new Window3();
- contentPane.add(win3);
- JPanel buttonPanel = new JPanel();
- final JButton previousButton = new JButton("< PREVIOUS");
- final JButton nextButton = new JButton("NEXT >");
- final JButton cancelButton = new JButton("CANCEL");
- buttonPanel.add(cancelButton);
- buttonPanel.add(previousButton);
- buttonPanel.add(nextButton);
- previousButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent ae) {
- Verifiable verifiable = null;
- Component[] contents = contentPane.getComponents();
- for(Component component : contents) {
- if(component.isVisible() && component instanceof Verifiable) {
- verifiable = (Verifiable)component;
- }
- }
- if(verifiable != null && verifiable.isDataValid()) {
- CardLayout cardLayout = (CardLayout) contentPane.getLayout();
- cardLayout.previous(contentPane);
- }
- }
- });
- nextButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent ae) {
- Verifiable verifiable = null;
- Component[] contents = contentPane.getComponents();
- for(Component component : contents) {
- if(component.isVisible() && component instanceof Verifiable) {
- verifiable = (Verifiable)component;
- }
- }
- if(verifiable != null && verifiable.isDataValid()) {
- CardLayout cardLayout = (CardLayout) contentPane.getLayout();
- cardLayout.next(contentPane);
- }
- }
- });
- cancelButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent ae) {
- System.exit(0);
- }
- });
- frame.add(contentPane);
- frame.add(buttonPanel, BorderLayout.PAGE_END);
- frame.pack();
- frame.setVisible(true);
- }
- public static void main(String[] args) {
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- createAndShowGUI();
- }
- });
- } }
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.JPanel;
- public class Window1 extends JPanel implements Verifiable {
- JTextField txtUsername = new JTextField();
- JPasswordField txtPassword = new JPasswordField();
- public Window1() {
- init();
- }
- private void init() {
- setLayout(new GridLayout(2, 2));
- JLabel lblUsername = new JLabel("Username:", JLabel.CENTER);
- txtUsername = new JTextField();
- JLabel lblPassword = new JLabel("Password:", JLabel.CENTER);
- txtPassword = new JPasswordField();
- add(lblUsername);
- add(txtUsername);
- add(lblPassword);
- add(txtPassword);
- String title = "Log in";
- setBorder(BorderFactory.createTitledBorder(title));
- }
- @Override
- public boolean isDataValid() {
- if(txtUsername.getText().equals("foo") &&
- java.util.Arrays.equals(txtPassword.getPassword(), "bar".toCharArray())) {
- return true;
- } else {
- JOptionPane.showMessageDialog(this, "Fel användarnamn och/eller lösenord",
- "Error", JOptionPane.ERROR_MESSAGE);
- return false;
- }
- }
- }
- interface Verifiable {
- boolean isDataValid();
- }
- class Window1 extends JPanel implements Verifiable {
- JTextField txtUsername = new JTextField();
- JPasswordField txtPassword = new JPasswordField();
- public Window1() {
- init();
- }
- private void init() {
- setLayout(new GridLayout(2, 2));
- JLabel lblUsername = new JLabel("Username:", JLabel.CENTER);
- JLabel lblPassword = new JLabel("Password:", JLabel.CENTER);
- add(lblUsername);
- add(txtUsername);
- add(lblPassword);
- add(txtPassword);
- String title = "Use "foo" and "bar"";
- setBorder(BorderFactory.createTitledBorder(title ));
- }
- @Override
- public boolean isDataValid() {
- return txtUsername.getText().equals("foo") &&
- java.util.Arrays.equals(txtPassword.getPassword(), "bar".toCharArray());
- }
- }
- nextButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent ae) {
- Verifiable verifiable = null;
- Component[] contents = contentPane.getComponents();
- for (Component component : contents) {
- if (component.isVisible() && component instanceof Verifiable) {
- verifiable = (Verifiable) component;
- }
- }
- if (verifiable != null && verifiable.isDataValid()) {
- CardLayout cardLayout = (CardLayout) contentPane.getLayout();
- cardLayout.next(contentPane);
- }
- }
- });
Add Comment
Please, Sign In to add comment