Java Swing: JPasswordField::setBounds(int, int,int,int); not working

CodeX

2[H]4U
Joined
Mar 21, 2006
Messages
2,879
I am trying to reposition a password field inside my JFrame using the method setBounds but it doesn't seem to be doing anything. Here is the code, I am completely new to this stuff so I have no idea if the order of any of this matters or what...

Code:
package atm;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;

public class ATM_Main
{
	static Customer customer;
	static JFormattedTextField pinField;
	static int pinEntered;
	
	public static void main(String[] args) 
	{
		pinEntered = 0;
		customer = new Customer();
		
       	welcomeScreen();
	}
	
	
	public static void welcomeScreen()
	{
		 
		JFrame frame = new JFrame("welcome");
		Container content = frame.getContentPane();
		content.setLayout(new FlowLayout()); 
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        frame.setResizable(false);
        frame.setBounds(0, 0, 640, 480);
        content.setBackground(Color.black);
        
        Font font = new Font("Arial", Font.PLAIN, 100);
        content.setFont(font);
        String labelText1 = "<html><FONT COLOR=WHITE SIZE=6>Welcome to Group 8 Bank</FONT></html>";
        String labelText2 = "Please Swipe Your Card to Begin";
        //JLabel label1 = new JLabel(labelText1, JLabel.CENTER);
        JLabel label1 = new JLabel(labelText1);
        label1.setForeground(Color.white);
        label1.setBounds(300, 0, 400, 20);
        content.add(label1);
        
        JPasswordField pinField = new JPasswordField(4);
        pinField.setBounds(170, 100, 200, 20);
        pinField.setBackground(Color.white);
        pinField.setEchoChar('*');
        pinField.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
                JPasswordField input = (JPasswordField)e.getSource();
                char[] password = input.getPassword();
                if(checkPin(password)) 
                {
                    //JOptionPane.showMessageDialog(frame, "Correct  password.");
                	//Continue to next screen
                } 
                
                else 
                {
                    //JOptionPane.showMessageDialog(frame, "Incorrect PIN.", "Error Message", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        content.add(pinField);
        frame.validate();
	}
	
	private static boolean checkPin(char[] inputPassword) 
    {
        char[] actualPassword = {'1', '1', '1', '1'};
        if(inputPassword.length != actualPassword.length)
        {
            return false;		//Return false if lengths are unequal
        }
        
        for (int i = 0;  i < inputPassword.length; i ++)
        {
            if (inputPassword[i] != actualPassword[i])
            {
                return false;
            }
        }
        
        return true;
    }
}
 
Follow up question:

Is there any way in Swing to just NOT use a STUPID layout type? I just want to position things manually and resizing the window is not a concern since I disable that functionality. Having to choose between the ridiculous layout types is making what I need to do very difficult.
 
If that's really what you want to do:

Code:
content.setLayout(null)

In the case of FlowLayout, if you set the component's preferred size, it should be honored:

Code:
pinField.setPreferredSize(new Dimension(200, 20))
 
just as a sidenote, for a simple quick application, positioning elements may be faster, but for large complicated programs i'd kill myself without layout managers ;)
 
Back
Top