Results 1 to 5 of 5
  1. #1
    cromie11 is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default swing passwordField to change jPanel background colour

    new to java as the title suggests i am attempting to create a swing application with great difficulty but feel im very close, i want to create a door security system where when you enter the password into a passwordField it changes a panel from black to red indicating that the door is open also i want if the user hits 000 they can change the password i am just new to java and would hugely appreciate any help thanks in advancehttp://www.java-forums.org/images/smilies/skype/(happy).gif

    CODE

    Java Code:
    import java.awt.Color;
    import java.util.Arrays;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /*
     * NewJFrame.java
     *
     * Created on 28-May-2011, 15:11:44
     */
    
    /**
     *
     * @author cromie09
     */
    public class NewJFrame extends javax.swing.JFrame {
    
        /** Creates new form NewJFrame */
        public NewJFrame() {
            initComponents();
    
        }
    
    ;
    
    
    
        @Override
        public void setBackground(Color red) {
            super.setBackground(red);
        }
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
    
    
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            btn1 = new javax.swing.JButton();
            btn2 = new javax.swing.JButton();
            btn3 = new javax.swing.JButton();
            btn4 = new javax.swing.JButton();
            btn5 = new javax.swing.JButton();
            btn6 = new javax.swing.JButton();
            btn7 = new javax.swing.JButton();
            btn8 = new javax.swing.JButton();
            btn9 = new javax.swing.JButton();
            btn0 = new javax.swing.JButton();
            OK = new javax.swing.JButton();
            btnClear = new javax.swing.JButton();
            jP1 = new javax.swing.JPanel();
            txtPassword2 = new javax.swing.JPasswordField();
            jLabel1 = new javax.swing.JLabel();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("DOOR KEYPAD");
            setFont(new java.awt.Font("BatangChe", 1, 10)); // NOI18N
    
            btn1.setText("1");
            btn1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btn1ActionPerformed(evt);
                }
            });
    
            btn2.setText("2");
            btn2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btn2ActionPerformed(evt);
                }
            });
    
            btn3.setText("3");
            btn3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btn3ActionPerformed(evt);
                }
            });
    
            btn4.setText("4");
            btn4.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btn4ActionPerformed(evt);
                }
            });
    
            btn5.setText("5");
            btn5.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btn5ActionPerformed(evt);
                }
            });
    
            btn6.setText("6");
            btn6.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btn6ActionPerformed(evt);
                }
            });
    
            btn7.setText("7");
            btn7.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btn7ActionPerformed(evt);
                }
            });
    
            btn8.setText("8");
            btn8.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btn8ActionPerformed(evt);
                }
            });
    
            btn9.setText("9");
            btn9.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btn9ActionPerformed(evt);
                }
            });
    
            btn0.setText("0");
            btn0.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btn0ActionPerformed(evt);
                }
            });
    
            OK.setText("OK");
            OK.setToolTipText("Submit");
            OK.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    OKActionPerformed(evt);
                }
            });
    
            btnClear.setText("CLEAR");
            btnClear.setToolTipText("Clear");
            btnClear.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnClearActionPerformed(evt);
                }
            });
    
            jP1.setBackground(new java.awt.Color(0, 51, 51));
    
            javax.swing.GroupLayout jP1Layout = new javax.swing.GroupLayout(jP1);
            jP1.setLayout(jP1Layout);
            jP1Layout.setHorizontalGroup(
                jP1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 100, Short.MAX_VALUE)
            );
            jP1Layout.setVerticalGroup(
                jP1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 100, Short.MAX_VALUE)
            );
    
            txtPassword2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    txtPassword2ActionPerformed(evt);
                }
            });
    
            jLabel1.setText("1234");
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(94, 94, 94)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(btn1)
                        .addComponent(btn4)
                        .addComponent(btn7))
                    .addGap(44, 44, 44)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(btn0, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                        .addComponent(btn8, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                        .addComponent(btn2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btn5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(37, 37, 37)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(btn3, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 302, Short.MAX_VALUE)
                                    .addComponent(OK))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(btn9)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 259, Short.MAX_VALUE)
                                    .addComponent(txtPassword2, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(btn6)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 284, Short.MAX_VALUE)
                                    .addComponent(btnClear)))
                            .addGap(6, 6, 6)
                            .addComponent(jP1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(184, 184, 184)
                            .addComponent(jLabel1)))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(31, 31, 31)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jP1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(65, 65, 65)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(btn1)
                                .addComponent(btn2)
                                .addComponent(btn3)
                                .addComponent(OK))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(28, 28, 28)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(btn4)
                                        .addComponent(btn5)
                                        .addComponent(btn6))
                                    .addGap(31, 31, 31)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(btn7)
                                        .addComponent(btn8)
                                        .addComponent(btn9))
                                    .addGap(33, 33, 33)
                                    .addComponent(btn0))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(40, 40, 40)
                                    .addComponent(btnClear)
                                    .addGap(37, 37, 37)
                                    .addComponent(txtPassword2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jLabel1)))))
                    .addContainerGap(20, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>
    
        private void btn1ActionPerformed(java.awt.event.ActionEvent evt) {                                     
       String btn1Text = txtPassword2.getText() + btn1.getText( );
    txtPassword2.setText(btn1Text);
    
    
        }                                    
    
        private void btn2ActionPerformed(java.awt.event.ActionEvent evt) {                                     
             String btn2Text = txtPassword2.getText() + btn2.getText( );
    txtPassword2.setText(btn2Text);
        }                                    
    
        private void btn3ActionPerformed(java.awt.event.ActionEvent evt) {                                     
             String btn3Text = txtPassword2.getText() + btn3.getText( );
    txtPassword2.setText(btn3Text);
        }                                    
    
        private void btn4ActionPerformed(java.awt.event.ActionEvent evt) {                                     
             String btn4Text = txtPassword2.getText() + btn4.getText( );
    txtPassword2.setText(btn4Text);
        }                                    
    
        private void btn5ActionPerformed(java.awt.event.ActionEvent evt) {                                     
                  String btn5Text = txtPassword2.getText() + btn5.getText( );
    txtPassword2.setText(btn5Text);
        }                                    
    
        private void btn6ActionPerformed(java.awt.event.ActionEvent evt) {                                     
              String btn6Text = txtPassword2.getText() + btn6.getText( );
    txtPassword2.setText(btn6Text);
        }                                    
    
        private void btn7ActionPerformed(java.awt.event.ActionEvent evt) {                                     
                  String btn7Text = txtPassword2.getText() + btn7.getText( );
    txtPassword2.setText(btn7Text);
        }                                    
    
        private void btn8ActionPerformed(java.awt.event.ActionEvent evt) {                                     
                  String btn8Text = txtPassword2.getText() + btn8.getText( );
    txtPassword2.setText(btn8Text);
        }                                    
    
        private void btn9ActionPerformed(java.awt.event.ActionEvent evt) {                                     
                  String btn9Text = txtPassword2.getText() + btn9.getText( );
    txtPassword2.setText(btn9Text);
    
        }                                    
    
        private void btn0ActionPerformed(java.awt.event.ActionEvent evt) {                                     
                   String btn0Text = txtPassword2.getText() + btn0.getText( );
    txtPassword2.setText(btn0Text);
        }                                    
      private static boolean isPasswordCorrect(char[] input) {
            boolean isCorrect = true;
            char[] correctPassword = { '1', '2', '3', '4' };
    
            if (input.length != correctPassword.length) {
                isCorrect = false;
            } else {
                isCorrect = Arrays.equals (input, correctPassword);
            }
    
            //Zero out the password.
            Arrays.fill(correctPassword,'0');
    
            return isCorrect;
        }
    
    
    
        private void OKActionPerformed(java.awt.event.ActionEvent evt) {                                          
     
    
    
    
        }                                         
    
        private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {                                         
             txtPassword2.setText("");
        }                                        
    
        public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();
    
        if (OK.equals(cmd)) { //Process the password.
            char[] input = txtPassword2.getPassword();
            if (isPasswordCorrect(input)) {
             jP1.setBackground(Color.red);
    
            }
     else {
                jP1.setBackground(Color.black);
     }
    
            //Zero out the possible password, for security.
            Arrays.fill(input, '000');
    
           txtPassword2.selectAll();
    
        }
        }
    
    
    
    
    
        
    
    
        private void txtPassword2ActionPerformed(java.awt.event.ActionEvent evt) {
    
    
        }
    
    
       
    
    
    
    
    
    
                             // create components
    
    
    
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new NewJFrame().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify
        private javax.swing.JButton OK;
        private javax.swing.JButton btn0;
        private javax.swing.JButton btn1;
        private javax.swing.JButton btn2;
        private javax.swing.JButton btn3;
        private javax.swing.JButton btn4;
        private javax.swing.JButton btn5;
        private javax.swing.JButton btn6;
        private javax.swing.JButton btn7;
        private javax.swing.JButton btn8;
        private javax.swing.JButton btn9;
        private javax.swing.JButton btnClear;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JPanel jP1;
        private javax.swing.JPasswordField txtPassword2;
        // End of variables declaration
    
    }
    Last edited by Fubarable; 05-30-2011 at 03:39 AM. Reason: code tags added

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Here's a few tips:

    Do not dump a gazillion lines of code and expect us to fix it. Create a SSCCE instead (google it).

    If you get any errors, copy and paste the exact error message(s).

    Ask a specific question. All you have done is state a bunch of requirements. What do you expect a mechanic to say if you said "I want to build a car"?

    When posting code use code tags. Place [ code ] before and [ /code ] after the code. (wihtout the spaces)

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Agree with Junky, and his suggestions aren't criticism but rather recommendations that will help us help you.

    You will want to let us know what your problems are as specifically as possible so we can better help you rather than post code and requirements.

    Code tags added.

    Much luck.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Also, this line doesn't compile:

    Java Code:
    Arrays.fill(input, '000');
    as input is a char[] and '000' isn't a valid char. You will want to fix this, perhaps by filling with spaces:

    Java Code:
    Arrays.fill(input, ' ');
    Last edited by Fubarable; 05-30-2011 at 03:51 AM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I assume that you want your code to react to the OK JButton, but it's easy to see that this button does nothing as evidenced by its action listener code:

    Java Code:
       private void OKActionPerformed(java.awt.event.ActionEvent evt) {
    
       }
    You'll want to fill this method with appropriate code.

    Luck!

Similar Threads

  1. Change colour of JOptionPane
    By chirag123 in forum AWT / Swing
    Replies: 1
    Last Post: 05-03-2011, 03:48 AM
  2. Change BufferedImage background color
    By Nosrettap in forum New To Java
    Replies: 0
    Last Post: 03-21-2011, 12:29 AM
  3. Replies: 4
    Last Post: 10-14-2010, 09:18 AM
  4. Change JPanel background after its been set once
    By mevets in forum AWT / Swing
    Replies: 4
    Last Post: 04-14-2010, 02:07 AM
  5. Replies: 0
    Last Post: 03-26-2009, 02:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •