Skip to content
Advertisement

Java Swing Application Window – Second Form showing empty

The first class or JFrame that displays just fine

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.Window;
import java.awt.Color;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;

public class ATM_Display extends JFrame{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JFrame frame;
    private JPasswordField jpass_passfield;
    char[] password = new char[5]; // field for our password
    private StringBuilder tempPin = new StringBuilder(); // temporary pin placeholder
    private static List<Account> listOfAccounts = new ArrayList<Account>(); // list of the banks accounts
    private static List<JButton> btnList = new ArrayList<JButton>();
    private static OptionsMenu frame2;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ATM_Display window = new ATM_Display();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public ATM_Display() {
        initialize();
        frame2 = new OptionsMenu();
        InstantiateAccList(listOfAccounts); // instantiate our list of accounts including clients names/pin/balance
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 508, 481);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        
        jpass_passfield = new JPasswordField();
        jpass_passfield.setHorizontalAlignment(SwingConstants.CENTER);
        jpass_passfield.setFont(new Font("Tahoma", Font.PLAIN, 30));
        jpass_passfield.setToolTipText("please enter a valid 4 digit pin number using the numeric buttons below");
        jpass_passfield.setEditable(false);
        jpass_passfield.setBounds(114, 113, 233, 41);
        frame.getContentPane().add(jpass_passfield);
        
        //button 1
        JButton btn_1 = new JButton("1");
        btn_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                AddToPassword("1"); // add to temporary placeholder
                jpass_passfield.setText(GetPin()); // set the actual text of the password field
                if(CheckPinLength()) {              // check the length of the pin SB if >= 5
                    DisableKeypad(btnList); //disable buttons
                }
            }
        });
        
        
        btn_1.setFont(new Font("Tahoma", Font.BOLD, 16));
        btn_1.setBounds(22, 256, 89, 23);
        frame.getContentPane().add(btn_1);
        
        //button 2
        JButton btn_2 = new JButton("2");
        btn_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                AddToPassword("2");
                jpass_passfield.setText(GetPin());
                CheckPinLength(); // check the length of the pin SB
                if(CheckPinLength()) {              // check the length of the pin SB if >= 5
                    DisableKeypad(btnList); //disable buttons
                }
            }
        });
        btn_2.setFont(new Font("Tahoma", Font.BOLD, 16));
        btn_2.setBounds(128, 256, 89, 23);
        frame.getContentPane().add(btn_2);
        
        //button 3
        JButton btn_3 = new JButton("3");
        btn_3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                AddToPassword("3");
                jpass_passfield.setText(GetPin());
                CheckPinLength(); // check the length of the pin SB
                if(CheckPinLength()) {              // check the length of the pin SB if >= 5
                    DisableKeypad(btnList); //disable buttons
                }
            }
        });
        btn_3.setFont(new Font("Tahoma", Font.BOLD, 16));
        btn_3.setBounds(229, 256, 89, 23);
        frame.getContentPane().add(btn_3);
        
        //button 4
        JButton btn_4 = new JButton("4");
        btn_4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                AddToPassword("4");
                jpass_passfield.setText(GetPin());
                CheckPinLength(); // check the length of the pin SB
                if(CheckPinLength()) {              // check the length of the pin SB if >= 5
                    DisableKeypad(btnList); //disable buttons
                }
            }
        });
        btn_4.setFont(new Font("Tahoma", Font.BOLD, 16));
        btn_4.setBounds(22, 301, 89, 23);
        frame.getContentPane().add(btn_4);
        
        //button 5
        JButton btn_5 = new JButton("5");
        btn_5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                AddToPassword("5");
                jpass_passfield.setText(GetPin());
                CheckPinLength(); // check the length of the pin SB
                if(CheckPinLength()) {              // check the length of the pin SB if >= 5
                    DisableKeypad(btnList); //disable buttons
                }
            }
        });
        btn_5.setFont(new Font("Tahoma", Font.BOLD, 16));
        btn_5.setBounds(128, 301, 89, 23);
        frame.getContentPane().add(btn_5);
        
        //button 6
        JButton btn_6 = new JButton("6");
        btn_6.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                AddToPassword("6");
                jpass_passfield.setText(GetPin());
                CheckPinLength(); // check the length of the pin SB
                if(CheckPinLength()) {              // check the length of the pin SB if >= 5
                    DisableKeypad(btnList); //disable buttons
                }
            }
        });
        btn_6.setFont(new Font("Tahoma", Font.BOLD, 16));
        btn_6.setBounds(229, 301, 89, 23);
        frame.getContentPane().add(btn_6);
        
        //button 7
        JButton btn_7 = new JButton("7");
        btn_7.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                AddToPassword("7");
                jpass_passfield.setText(GetPin());
                CheckPinLength(); // check the length of the pin SB
                if(CheckPinLength()) {              // check the length of the pin SB if >= 5
                    DisableKeypad(btnList); //disable buttons
                }
            }
        });
        btn_7.setFont(new Font("Tahoma", Font.BOLD, 16));
        btn_7.setBounds(22, 347, 89, 23);
        frame.getContentPane().add(btn_7);
        
        //button 8
        JButton btn_8 = new JButton("8");
        btn_8.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                AddToPassword("8");
                jpass_passfield.setText(GetPin());
                CheckPinLength(); // check the length of the pin SB
                if(CheckPinLength()) {              // check the length of the pin SB if >= 5
                    DisableKeypad(btnList); //disable buttons
                }
            }
        });
        btn_8.setFont(new Font("Tahoma", Font.BOLD, 16));
        btn_8.setBounds(128, 347, 89, 23);
        frame.getContentPane().add(btn_8);
        
        //button 9
        JButton btn_9 = new JButton("9");
        btn_9.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                AddToPassword("9");
                jpass_passfield.setText(GetPin());
                CheckPinLength(); // check the length of the pin SB
                if(CheckPinLength()) {              // check the length of the pin SB if >= 5
                    DisableKeypad(btnList); //disable buttons
                }
            }
        });
        btn_9.setFont(new Font("Tahoma", Font.BOLD, 16));
        btn_9.setBounds(229, 347, 89, 23);
        frame.getContentPane().add(btn_9);
        
        //button 0
        JButton btn_0 = new JButton("0");
        btn_0.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                AddToPassword("1");
                jpass_passfield.setText(GetPin());
                CheckPinLength(); // check the length of the pin SB
                if(CheckPinLength()) {              // check the length of the pin SB if >= 5
                    DisableKeypad(btnList); //disable buttons
                }
            }
        });
        
        btn_0.setFont(new Font("Tahoma", Font.BOLD, 16));
        btn_0.setBounds(128, 389, 89, 23);
        frame.getContentPane().add(btn_0);
        
        JButton btn_cancel = new JButton("CANCEL");
        btn_cancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
            }
        });
        
        btn_cancel.setBackground(new Color(255, 0, 0));
        btn_cancel.setFont(new Font("Tahoma", Font.BOLD, 16));
        btn_cancel.setBounds(328, 256, 127, 23);
        frame.getContentPane().add(btn_cancel);
        
        JButton btn_clear = new JButton("CLEAR");
        btn_clear.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ClearTempPin(); // clear our temporary pin
                jpass_passfield.setText(""); // clear the password field
                EnableKeypad(btnList);
            }
        });
        btn_clear.setBackground(Color.YELLOW);
        btn_clear.setForeground(Color.BLACK);
        btn_clear.setFont(new Font("Tahoma", Font.BOLD, 16));
        btn_clear.setBounds(328, 301, 127, 23);
        frame.getContentPane().add(btn_clear);
        
        JButton btn_enter = new JButton("ENTER");
        btn_enter.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(isPinValid()) {
                    frame.setVisible(false);
                    frame2.setVisible(true);
                }
            }
        });
        btn_enter.setBackground(Color.GREEN);
        btn_enter.setFont(new Font("Tahoma", Font.BOLD, 16));
        btn_enter.setBounds(328, 347, 127, 23);
        frame.getContentPane().add(btn_enter);      
        
        JLabel lblNewLabel = new JLabel("Enter Pin");
        lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 21));
        lblNewLabel.setBounds(179, 79, 114, 23);
        frame.getContentPane().add(lblNewLabel);
        
        JLabel lblWelcomePlease = new JLabel("INVESCO ATM");
        lblWelcomePlease.setForeground(new Color(0, 128, 0));
        lblWelcomePlease.setFont(new Font("Tahoma", Font.BOLD, 25));
        lblWelcomePlease.setBounds(144, 11, 189, 23);
        frame.getContentPane().add(lblWelcomePlease);
        
        //add buttons to our list
        AddBtnsToList(btn_1, btn_2, btn_3, btn_4, btn_5, btn_6, btn_7, btn_8, btn_9, btn_0);
    }
    
    // method for accessing private jpass_passfield
    /*
     * public void AddValToPassField(int i) { this.jpass_passfield. }
     */
    
    // method to instantiate list of accounts
    public static void InstantiateAccList(List<Account> x) {
        final Account a = new Account("Henry", "Ford", 10000, 12345);
        final Account b = new Account("Martha", "Stewart", 3454453, 22345);
        final Account c = new Account("Cletus", "Shines", 199, 32345);
        final Account d = new Account("Warren", "Scruffet", 28488, 42345);
        
        x.add(a);
        x.add(b);
        x.add(c);
        x.add(d);
        
    }
    
    //method to validate client PIN
    // if pin is correct return true
    // otherwise return false
    
    public boolean isPinValid() {
        for(Account y : listOfAccounts) { 
            if(y.getPin() == Integer.parseInt(GetPin())) {
                return true;
            }
        } 
        return false;
    }
    
    // Method for adding buttons to the list of buttons
    public static void AddBtnsToList(JButton a, JButton b, JButton c, JButton d, JButton e,
            JButton f, JButton g, JButton h, JButton i, JButton j) {
        btnList.add(a);
        btnList.add(b);
        btnList.add(c);
        btnList.add(d);
        btnList.add(e);
        btnList.add(f);
        btnList.add(g);
        btnList.add(h);
        btnList.add(i);
        btnList.add(j);
    }
    
    // method for adding string to the temporary pin string we will be comparing to
    
    public void AddToPassword(String x) {
        StringBuilder s = new StringBuilder(x);
        this.tempPin.append(s);
    }
    // gets the current pin string
    public String GetPin() {
        return this.tempPin.toString();
    }
    // clears the temporary pin placeholder
    public void ClearTempPin() {
        this.tempPin.setLength(0);
    }
    
    // checks to see if pin is at 5 characters
    public boolean CheckPinLength() {
        String temp = this.tempPin.toString();
        if (temp.length() == 5) {
            return true;
        }
        else { return false;}
    }
    
    //method to disable keypad if length is at 5
    public void DisableKeypad(List<JButton> x) {
        for(JButton y : x) {
            y.setEnabled(false);
        }
    }
        
        //method to Enable under a particular condition
        public void EnableKeypad(List<JButton> x) {
            for(JButton y : x) {
                y.setEnabled(true);
            }
    }

}

And now my second class.. I’m sorry I’m a complete noob, when I run the debugger it looks like Options Menu is constructing correctly, but then it just displays as the minimize, full screen and exit button and nothing else. It will also run perfectly fine independently if I run it on it’s own page. I’m really confused at this point and would appreciate any help. I have seen a lot of examples on here where the JFrame isn’t set to isVisible or instantiated, but that isn’t the case here. Thank you so much for any help!

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Font;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class OptionsMenu extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JFrame frame;
    private JTextField tb_deposit;

    /**
     * Launch the application.
     */
    /*
     * public static void main(String[] args) { EventQueue.invokeLater(new
     * Runnable() { public void run() { try { OptionsMenu window = new
     * OptionsMenu(); window.frame.setVisible(true); } catch (Exception e) {
     * e.printStackTrace(); } } }); }
     */

    /**
     * Create the application.
     */
    public OptionsMenu() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 490, 461);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        
        JButton btn_balance = new JButton("1");
        btn_balance.setFont(new Font("Tahoma", Font.BOLD, 25));
        btn_balance.setBounds(207, 267, 64, 31);
        frame.getContentPane().add(btn_balance);
        
        JButton btn_Withdrawal = new JButton("2");
        btn_Withdrawal.setFont(new Font("Tahoma", Font.BOLD, 25));
        btn_Withdrawal.setBounds(207, 301, 64, 31);
        frame.getContentPane().add(btn_Withdrawal);
        
        JButton btn_deposit = new JButton("3");
        btn_deposit.setFont(new Font("Tahoma", Font.BOLD, 25));
        btn_deposit.setBounds(207, 335, 64, 31);
        frame.getContentPane().add(btn_deposit);
        
        /// exit button
        JButton btn_exit = new JButton("4");
        btn_exit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int result = JOptionPane.showConfirmDialog(frame,
                        "Do you want to Exit ?", "Exit Confirmation : ",
                        JOptionPane.YES_NO_OPTION);
                    if (result == JOptionPane.YES_OPTION)
                      frame.dispose();
                    else if (result == JOptionPane.NO_OPTION)
                      frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
            }
        });
        btn_exit.setFont(new Font("Tahoma", Font.BOLD, 25));
        btn_exit.setBounds(207, 369, 64, 31);
        frame.getContentPane().add(btn_exit);
        
        JLabel lblNewLabel = new JLabel("BALANCE INQUIRY:");
        lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 15));
        lblNewLabel.setBounds(37, 267, 160, 31);
        frame.getContentPane().add(lblNewLabel);
        
        JLabel lblWithdrawal = new JLabel("WITHDRAWAL:");
        lblWithdrawal.setFont(new Font("Tahoma", Font.BOLD, 15));
        lblWithdrawal.setBounds(72, 309, 116, 19);
        frame.getContentPane().add(lblWithdrawal);
        
        JLabel lblDeposit = new JLabel("DEPOSIT:");
        lblDeposit.setFont(new Font("Tahoma", Font.BOLD, 15));
        lblDeposit.setBounds(114, 339, 74, 19);
        frame.getContentPane().add(lblDeposit);
        
        JLabel lblExit = new JLabel("EXIT:");
        lblExit.setFont(new Font("Tahoma", Font.BOLD, 15));
        lblExit.setBounds(140, 369, 48, 14);
        frame.getContentPane().add(lblExit);
        
        JButton btn_get20 = new JButton("$20");
        btn_get20.setBounds(10, 34, 89, 23);
        frame.getContentPane().add(btn_get20);
        
        JButton btn_get40 = new JButton("$40");
        btn_get40.setBounds(10, 80, 89, 23);
        frame.getContentPane().add(btn_get40);
        
        JButton btn_get60 = new JButton("$60");
        btn_get60.setBounds(10, 127, 89, 23);
        frame.getContentPane().add(btn_get60);
        
        JButton btn_get100 = new JButton("$100");
        btn_get100.setBounds(338, 34, 89, 23);
        frame.getContentPane().add(btn_get100);
        
        JButton btn_get200 = new JButton("$200");
        btn_get200.setBounds(338, 80, 89, 23);
        frame.getContentPane().add(btn_get200);
        
        JButton btn_cancel = new JButton("CANCEL");
        btn_cancel.setBounds(338, 127, 89, 23);
        frame.getContentPane().add(btn_cancel);
        
        tb_deposit = new JTextField();
        tb_deposit.setBounds(157, 128, 96, 20);
        frame.getContentPane().add(tb_deposit);
        tb_deposit.setColumns(10);
        
        JLabel lblNewLabel_1 = new JLabel("Enter Deposit Amount");
        lblNewLabel_1.setBounds(157, 103, 114, 14);
        frame.getContentPane().add(lblNewLabel_1);
    }
    
    public JFrame GetFrame2() {
        return this.frame;
    }
}

Advertisement

Answer

The problem is that your classes (ATM_Display and OptionsMenu) extend JFrame but are never really used as JFrames (you create a separate JFrame instance in each).

That means that when you make the OptionsMenu visible there is nothing to show (because you add all elements to the OptionsMenu.frame).

You should drop the extends clause:

public class ATM_Display {
    // ...
}

and

public class OptionsMenu {
    // ...
}

Now you will have a compile error in ATM_Display on the following line because the OptionsMenu doesn’t have a setVisible() method:

    frame2.setVisible(true);

To make the code work again the OptionsMenu needs this setVisible() method to make its frame visible:

public class OptionsMenu {
    // ...

    public void setVisible(boolean b) {
        frame.setVisible(b);
    }
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement