size of JTextField and JPasswordField

I have a dialog for user login. I added the JTextField and JPasswordField to the dialog in exactly the same way, but the JPasswordField appears to be stretched vertically. Does anyone know what could cause this?
[218 byte] By [JNooreza] at [2007-10-3 4:05:48]
# 1

We have no idea what LayoutManager you are using or how you added the components to the GUI, so no we don't know whats happening.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

camickra at 2007-7-14 22:05:10 > top of Java-index,Desktop,Core GUI APIs...
# 2

here is the class that was giving me trouble. i've made it execuatable for this example. I found the problem: I was using the UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() ) and i'm guessing it was causing the components to missalign. Can someone help fix it?

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class LoginFrame

{

private static final long serialVersionUID = 1L;

private String userName;

private char[] password;

public String getUserName(){

return userName;

}

public char[] getPassword(){

return password;

}

private final int textFieldSize = 18;

public boolean okButtonClicked;

public JDialog loginDialogWindow;

JTextField inUserName;

JPasswordField inPwdField;

public LoginFrame()

{

loginDialogWindow = new JDialog((JFrame)null, "Login");

Container c = loginDialogWindow.getContentPane();

c.setLayout( new BoxLayout(c, BoxLayout.Y_AXIS) );

JPanel inputPanel = new JPanel();

inputPanel.setLayout( new FlowLayout() );

inUserName = new JTextField(textFieldSize);

inputPanel.add( new JLabel("User Name") );

inputPanel.add( inUserName );

c.add( inputPanel );

inputPanel = new JPanel();

inputPanel.setLayout( new FlowLayout() );

inputPanel.add( new JLabel("Password") );

inPwdField = new JPasswordField(textFieldSize);

inputPanel.add( inPwdField );

c.add( inputPanel );

inputPanel = new JPanel();

inputPanel.setLayout( new FlowLayout() );

JButton okButton = new JButton("OK");

inputPanel.add(okButton);

JButton cancelButton = new JButton("CANCEL");

inputPanel.add(cancelButton);

c.add(inputPanel);

loginDialogWindow.setDefaultCloseOperation( JDialog.DO_NOTHING_ON_CLOSE );

//loginDialogWindow.setResizable(false);

loginDialogWindow.setSize(250,165);

}

public void setDialogLocation( int parentWidth, int parentHeight, int parentX, int parentY )

{

loginDialogWindow.setLocation(

( parentWidth-loginDialogWindow.getWidth() )/2+parentX,

( parentHeight-loginDialogWindow.getHeight() )/2+parentY );

}

public void showLoginDialog()

{

loginDialogWindow.setVisible(true);

}

public void setUserNamePassword( ActionEvent e )

{

userName = inUserName.getText();

password = inPwdField.getPassword();

okButtonClicked = true;

loginDialogWindow.setVisible(false);

}

public void onCancelButtonClick( ActionEvent e )

{

userName = null;

password = null;

okButtonClicked = false;

loginDialogWindow.setVisible(false);

}

public static void main( String[] args )

{

new LoginFrame().showLoginDialog();

}

}

JNooreza at 2007-7-14 22:05:10 > top of Java-index,Desktop,Core GUI APIs...
# 3
oppss...add this in the main method before constructor invokation:try{UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );}catch( Excepion e ){}
JNooreza at 2007-7-14 22:05:10 > top of Java-index,Desktop,Core GUI APIs...
# 4
Read reply 1.
camickra at 2007-7-14 22:05:10 > top of Java-index,Desktop,Core GUI APIs...
# 5

OK...i'm using a BoxLayout on Y_AXIS to add JPanels to a Container. Each JPanel is set to FlowLayout. When I use

UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() ); and get the native look and feel..you'll notice that the JPasswordField is wider then the JTextField. I don't know what could be causing this. Below is an executable program. If you are using windows xp...you should see the problem. If I don't sepecifiy the LookAndFeel...both the JTextField and JPasswordField look the same. Here is the program

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class LoginFrame

{

private final int textFieldSize = 18;

public JDialog loginDialogWindow;

JTextField inUserName;

JPasswordField inPwdField;

public LoginFrame()

{

loginDialogWindow = new JDialog((JFrame)null, "Login");

Container c = loginDialogWindow.getContentPane();

c.setLayout( new BoxLayout(c, BoxLayout.Y_AXIS) );

JPanel inputPanel = new JPanel();

inputPanel.setLayout( new FlowLayout() );

inUserName = new JTextField(textFieldSize);

inputPanel.add( new JLabel("User Name") );

inputPanel.add( inUserName );

c.add( inputPanel );

inputPanel = new JPanel();

inputPanel.setLayout( new FlowLayout() );

inputPanel.add( new JLabel("Password") );

inPwdField = new JPasswordField(textFieldSize);

inputPanel.add( inPwdField );

c.add( inputPanel );

inputPanel = new JPanel();

inputPanel.setLayout( new FlowLayout() );

JButton okButton = new JButton("OK");

inputPanel.add(okButton);

JButton cancelButton = new JButton("CANCEL");

inputPanel.add(cancelButton);

c.add(inputPanel);

loginDialogWindow.setDefaultCloseOperation( JDialog.EXIT_ON_CLOSE );

loginDialogWindow.setResizable(false);

loginDialogWindow.setSize(250,165);

}

public void showLoginDialog()

{

loginDialogWindow.setVisible(true);

}

public static void main( String[] args )

{

try

{

UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );

}

catch( Exception e )

{

e.printStackTrace();

}

new LoginFrame().showLoginDialog();

}

}

JNooreza at 2007-7-14 22:05:10 > top of Java-index,Desktop,Core GUI APIs...
# 6
The code isn't formatted. I'm sure you don't code that way and I'm not going to read your code that way.
camickra at 2007-7-14 22:05:10 > top of Java-index,Desktop,Core GUI APIs...
# 7
My code does have proper formatting...i think its just the way this forum is doing it..because it was formatted when i was posting the message. If you have another place where I can send the source then i'll be happy to send it.
JNooreza at 2007-7-14 22:05:10 > top of Java-index,Desktop,Core GUI APIs...
# 8
For the third time .... read my first reply completely....
camickra at 2007-7-14 22:05:10 > top of Java-index,Desktop,Core GUI APIs...
# 9

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class LoginFrame

{

private final int textFieldSize = 18;

public JDialog loginDialogWindow;

JTextField inUserName;

JPasswordField inPwdField;

public LoginFrame()

{

loginDialogWindow = new JDialog((JFrame)null, "Login");

Container c = loginDialogWindow.getContentPane();

c.setLayout( new BoxLayout(c, BoxLayout.Y_AXIS) );

JPanel inputPanel = new JPanel();

inputPanel.setLayout( new FlowLayout() );

inUserName = new JTextField(textFieldSize);

inputPanel.add( new JLabel("User Name") );

inputPanel.add( inUserName );

c.add( inputPanel );

inputPanel = new JPanel();

inputPanel.setLayout( new FlowLayout() );

inputPanel.add( new JLabel("Password") );

inPwdField = new JPasswordField(textFieldSize);

inputPanel.add( inPwdField );

c.add( inputPanel );

inputPanel = new JPanel();

inputPanel.setLayout( new FlowLayout() );

JButton okButton = new JButton("OK");

inputPanel.add(okButton);

JButton cancelButton = new JButton("CANCEL");

inputPanel.add(cancelButton);

c.add(inputPanel);

loginDialogWindow.setDefaultCloseOperation(

JDialog.EXIT_ON_CLOSE );

loginDialogWindow.setResizable(false);

loginDialogWindow.setSize(250,165);

}

public void showLoginDialog()

{

loginDialogWindow.setVisible(true);

}

public static void main( String[] args )

{

try

{

UIManager.setLookAndFeel(

UIManager.getSystemLookAndFeelClassName() );

}

catch( Exception e )

{

e.printStackTrace();

}

new LoginFrame().showLoginDialog();

}

}

JNooreza at 2007-7-14 22:05:10 > top of Java-index,Desktop,Core GUI APIs...
# 10
inPwdField = new JPasswordField(textFieldSize);inPwdField.setPreferredSize(inUserName.getPreferredSize());//<inputPanel.add( inPwdField );
Michael_Dunna at 2007-7-14 22:05:10 > top of Java-index,Desktop,Core GUI APIs...