Hello there
I have been given an assignment at Uni to write program (obviously thus im here hehe)

The user (customer wishing to purchase a computer) should be able to login by typing his
user name and password and pressing the “Login” button. The layout of this first screen
of the program should look like the one displayed in Figure 1. Note, that the characters
in the password box should not be displayed while the user is typing them. Asterisks (*)
should be displayed instead.
When the application frame is resized the login screen should still look as in Figure 1, i.e.
relative positioning of components should be maintained.

2. When the user types the valid user name and password and presses the “login” button,
the background changes to green and the string “Welcome to ECSE501 Computers” is
displayed using a JLabel. For simplicity, the correct user name and password can be
hardcoded.
When the user types an incorrect user name or password, the background of the window
becomes red, the message “Incorrect login name/password” is displayed and the user is
given a chance to re-enter a username and password.

What i have so far is

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ecse501.object.oriented.development;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/**
 *
 * @author boss
 */
public class ECSE501OBJECTORIENTEDDEVELOPMENT {

    /**
     * @param args the command line arguments
     */
    private static String password = "pass";
    private static String user = "Maciek";
    
    public static void main(String[] args) {
       JFrame frame = new JFrame("Welcome to ECSE Computers");
       frame.setVisible(true);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setSize(300,300);
    //   frame.setResizable(false);// IF this option enabled nothing displays... You have to manually resize 
    //   window in order to show things (??)
       
       
       JLabel label = new JLabel("Password");
       JLabel label1 = new JLabel("Username");
       JPanel panel1 = new JPanel();
       JPanel panel = new JPanel();
       JButton label2 = new JButton("Log in");
       JButton panel2 = new JButton();
      
       frame.add(panel2);
       frame.add(panel);
       frame.add(panel1);
       
       JPasswordField pass = new JPasswordField(10);
       pass.setEchoChar('*');
       pass.addActionListener(new AL());
       JTextField uss = new JTextField(10);       
       uss.addActionListener(new AL());
       JButton log = new JButton();
       log.addActionListener(new AL());
       
       panel.add(label, BorderLayout.WEST);
       panel.add(pass, BorderLayout.WEST);
       panel1.add(uss, BorderLayout.WEST);
       panel1.add(label1, BorderLayout.WEST);
       panel2.add(label2, BorderLayout.WEST);
       panel2.add(log, BorderLayout.WEST);
    }
    static class AL implements ActionListener {
        @Override
        public void actionPerformed (ActionEvent e) {
            JPasswordField input = (JPasswordField) e.getSource();
            char[] passy = input.getPassword();
            String p = new String(passy);
            JTextField input1 = (JTextField) e.getSource();
            String usr = input1.getName();
            String u = usr;   
                        
            if(p.equals(password)){
                class frame extends JFrame
                {
                    frame()
                    {
                        getContentPane().setBackground(Color.green);
                    }
                }          
            }  
            else{
                JOptionPane.showMessageDialog(null, "Incorrect");
                class frame extends JFrame
                {
                    frame()
                    {
                        getContentPane().setBackground(Color.RED);
                    }
                }
            }
            if(u.equals(user)){
               class frame extends JFrame
                {
                    frame()
                    {
                        getContentPane().setBackground(Color.green);
                    }
                }  
            }
            else{
            JOptionPane.showMessageDialog(null, "Incorrect");
            class frame extends JFrame
                {
                    frame()
                    {
                        getContentPane().setBackground(Color.RED);
                    }
                }
            }
            
            
        }
        
        
        // rest to come
        
        
        
        }
        
    }

Not a lot but working on it all time.

And the problem is...
1.Content is not properly displayed, only one text field displayed instead of 2 (username and password)
2. Probably because the content is not properly displayed the background color does not change, but still that is the 2nd problem
3. It doesnt show any errors in NetBeans but when i compile it nothing happens and then bunch of unknown to me errors are coming up.

I have added attachment of how the display should roughly look like

thank you

ps. forgot to mention I am not allowed to use automatic generated code like the one you find in netbeans to create GUI therefore i have to create gui by my self (so it makes task bit harder)

Edited 5 Years Ago by infinitus: n/a

Attachments Untitled.png 8.72 KB

bunch of unknown to me errors are coming up.

Please copy and paste here the full text of your error messages.

Why so many inner classes named frame?

When using the BorderLayout only ONE component can be placed in each location.
You are placing more than one in them.
If you want more than one component in say the WEST, create a container like a JPanel and add the components to it and then add the JPanel to the WEST.

Edited 5 Years Ago by NormR1: n/a

thank you for your reply.
Errors are

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JTextField cannot be cast to javax.swing.JPasswordField
	at ecse501.object.oriented.development.ECSE501OBJECTORIENTEDDEVELOPMENT$AL.actionPerformed(ECSE501OBJECTORIENTEDDEVELOPMENT.java:70)
	at javax.swing.JTextField.fireActionPerformed(JTextField.java:508)
	at javax.swing.JTextField.postActionEvent(JTextField.java:721)
	at javax.swing.JTextField$NotifyAction.actionPerformed(JTextField.java:836)
	at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1661)
	at javax.swing.JComponent.processKeyBinding(JComponent.java:2879)
	at javax.swing.JComponent.processKeyBindings(JComponent.java:2926)
	at javax.swing.JComponent.processKeyEvent(JComponent.java:2842)
	at java.awt.Component.processEvent(Component.java:6281)
	at java.awt.Container.processEvent(Container.java:2229)
	at java.awt.Component.dispatchEventImpl(Component.java:4860)
	at java.awt.Container.dispatchEventImpl(Container.java:2287)
	at java.awt.Component.dispatchEvent(Component.java:4686)
	at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1908)
	at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:752)
	at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1017)
	at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:889)
	at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:717)
	at java.awt.Component.dispatchEventImpl(Component.java:4730)
	at java.awt.Container.dispatchEventImpl(Container.java:2287)
	at java.awt.Window.dispatchEventImpl(Window.java:2713)
	at java.awt.Component.dispatchEvent(Component.java:4686)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
	at java.awt.EventQueue.access$000(EventQueue.java:101)
	at java.awt.EventQueue$3.run(EventQueue.java:666)
	at java.awt.EventQueue$3.run(EventQueue.java:664)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
	at java.awt.EventQueue$4.run(EventQueue.java:680)
	at java.awt.EventQueue$4.run(EventQueue.java:678)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

Oh i see thats why i probably had only one item displaying... i will modify the code soon and see what i got.

ClassCastException: javax.swing.JTextField cannot be cast to javax.swing.JPasswordField
at OBJECTORIENTEDDEVELOPMENT.java:70)

At line 70 the code is casting an object to the wrong type.
Your listener needs to detect what kind of object it is before casting it.
How many different components use that same listener? Are they all the same type?

You call setVisible before you add any thing to the GUI.
You should call it AFTER everything has been added to the GUI.

Edited 5 Years Ago by NormR1: n/a

private static String password = "pass";
    private static String user = "Maciek";
    private static Component MyWindow;
    
    public static void main(String[] args) {
       JFrame frame = new JFrame("Welcome to ECSE Computers");       
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setSize(300,300);
    //   frame.setResizable(false);// IF this option enabled nothing displays... You have to manually resize 
    //   window in order to show things (??)
       JPanel panel = new JPanel();    
       Component content = MyWindow;       
       panel.add(content, BorderLayout.WEST);   
                      
       //panel.add(label, BorderLayout.WEST);
       //panel.add(pass, BorderLayout.WEST);
       //panel1.add(uss, BorderLayout.WEST);
       //panel1.add(label1, BorderLayout.WEST);
       //panel2.add(label2, BorderLayout.WEST);
       //panel2.add(log, BorderLayout.WEST);
       frame.setVisible(true);
    }
    class MyWindow extends JFrame{        
        MyWindow(){ //constructor
             JLabel label = new JLabel("Password");
             JLabel label1 = new JLabel("Username");
             JPanel panel1 = new JPanel();
             JPanel panel = new JPanel();
             JPasswordField pass = new JPasswordField(10);
             pass.setEchoChar('*');
             pass.addActionListener(new AL());
             JTextField uss = new JTextField(10);       
             uss.addActionListener(new AL());            
             
             JPanel content = new JPanel();// creates new content   
             content.add(pass);
             content.add(uss);
             content.add(label);
             content.add(label1);
             content.add(panel);
             content.add(panel1);
             setContentPane(content);        
       }
    }
.............. rest of code

I done the content panel as you suggested, but when i compile it it says

Exception in thread "main" java.lang.NullPointerException
	at java.awt.Container.addImpl(Container.java:1090)
	at java.awt.Container.add(Container.java:966)
	at ecse501.object.oriented.development.ECSE501OBJECTORIENTEDDEVELOPMENT.main(ECSE501OBJECTORIENTEDDEVELOPMENT.java:40)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

it seems like this bit

JPanel panel = new JPanel();    
       Component content = MyWindow;       
       panel.add(content, BorderLayout.WEST);

points to nothing but i dont understand why ...

but when i compile it it says

That is not a compiler error. You get a NullPointerException when you execute the code.

java.lang.NullPointerException
...
at
DEVELOPMENT.java:40)

Look at line 40 in the program and see what variable is null. Then check the code to see why that variable does not have a valid value.


What is the value of MyWindow?

Edited 5 Years Ago by NormR1: n/a

Whats the matter... Couldn't you come up with a longer class name than ECSE501OBJECTORIENTEDDEVELOPMENT???

Please start your own thread with your new question.

Use a layout manager to position your components.

it is regarding this question...

i have a similar code to this one which has been posted..

how ever i need put the text field and labels together

i dont want to show m code just incase some one takes it as im doing a college assignment... using the code posted before can u tell me how to get the txt field and labels together?

Then make an SSCCE and post it to show your problem.

Use a layout manager to position your components.

i havent been here for 2 days and it seems like someone tried to jack my thread and there is a argument going on here hmm .. .

Just ignore it.

Component content = MyWindow;
panel.add(content, BorderLayout.WEST);

I can't see any code to initialise MyWindow, so presumably its a null pointer, that gets copied to content, which leads to the NPE when you try to add it to your panel.

Welcome back to the party.

Whoops. Wrong thread. Your code is short enough.

What is the problem you are having now?

Edited 4 Years Ago by NormR1: removed request for SSCCE

hello sorry i have took a break then i forgot completely about my post... and in the mean time i have solved my problem, i just took different approach. :)

if you want i can paste my code in :)!

thx everyone for help anyway

This article has been dead for over six months. Start a new discussion instead.