Syed Irtaza Ali
asked on
create a login form in java
I require a login form created in java asking for a username and password. The password can either be hardcoded or coming from a text file.
If the user logins unsuccessfully it displays a msg and the user retries for 3 times after that the app closes.
If the user logins successfully it displays a new form displaying a label (You logged in successfully!) and a button to exit the app.
Iam using JCreator 2.5.
Now is there any code that i can look at to acheive this.
Iam a beginner in Java but have worked for 3 yrs in VB and Oracle/SQL Server.
Nomi.
If the user logins unsuccessfully it displays a msg and the user retries for 3 times after that the app closes.
If the user logins successfully it displays a new form displaying a label (You logged in successfully!) and a button to exit the app.
Iam using JCreator 2.5.
Now is there any code that i can look at to acheive this.
Iam a beginner in Java but have worked for 3 yrs in VB and Oracle/SQL Server.
Nomi.
is this an application or the user will log in though a website ? Either task is easy to perform.
I recommend using STRUTS http://jakarta.apache.org/struts/
down load the jar file. the jar file contains a demo. It's demo has a login page which does validation.
The big bonus is that you'll be using a very robust and stable framework. Since your starting out in Java, you might as well start by writing good code. STRUTS implements the MVC model.
Sounds difficult but since you have years of programming experience you shouldn't have too much difficulty.
- tekel
down load the jar file. the jar file contains a demo. It's demo has a login page which does validation.
The big bonus is that you'll be using a very robust and stable framework. Since your starting out in Java, you might as well start by writing good code. STRUTS implements the MVC model.
Sounds difficult but since you have years of programming experience you shouldn't have too much difficulty.
- tekel
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
rahul i copied the code and pasted it. Got a few errors which were fixed easily except these
D:\electric\JavaCodes\Java Programs\G etParam\Lo gin.java:1 7: cannot resolve symbol
symbol : class MySQLConn
location: class Login
private MySQLConn mysql;
^
D:\electric\JavaCodes\Java Programs\G etParam\Lo gin.java:2 5: cannot resolve symbol
symbol : class MySQLConn
location: class Login
mysql=new MySQLConn(config);
^
D:\electric\JavaCodes\Java Programs\G etParam\Lo gin.java:6 0: cannot resolve symbol
symbol : class VerticalFlowLayout
location: class Login
panelcontainer.setLayout(n ew VerticalFlowLayout());
^
D:\electric\JavaCodes\Java Programs\G etParam\Lo gin.java:1 08: cannot resolve symbol
symbol: variable Stars
Stars.closeLoginFrame();
^
4 errors
Please could u also send the code for the MySQL connection and other classes. I think they are missing.
D:\electric\JavaCodes\Java
symbol : class MySQLConn
location: class Login
private MySQLConn mysql;
^
D:\electric\JavaCodes\Java
symbol : class MySQLConn
location: class Login
mysql=new MySQLConn(config);
^
D:\electric\JavaCodes\Java
symbol : class VerticalFlowLayout
location: class Login
panelcontainer.setLayout(n
^
D:\electric\JavaCodes\Java
symbol: variable Stars
Stars.closeLoginFrame();
^
4 errors
Please could u also send the code for the MySQL connection and other classes. I think they are missing.
ASKER
rahul i copied the code and pasted it. Got a few errors which were fixed easily except these
D:\electric\JavaCodes\Java Programs\G etParam\Lo gin.java:1 7: cannot resolve symbol
symbol : class MySQLConn
location: class Login
private MySQLConn mysql;
^
D:\electric\JavaCodes\Java Programs\G etParam\Lo gin.java:2 5: cannot resolve symbol
symbol : class MySQLConn
location: class Login
mysql=new MySQLConn(config);
^
D:\electric\JavaCodes\Java Programs\G etParam\Lo gin.java:6 0: cannot resolve symbol
symbol : class VerticalFlowLayout
location: class Login
panelcontainer.setLayout(n ew VerticalFlowLayout());
^
D:\electric\JavaCodes\Java Programs\G etParam\Lo gin.java:1 08: cannot resolve symbol
symbol: variable Stars
Stars.closeLoginFrame();
^
4 errors
Please could u also send the code for the MySQL connection and other classes. I think they are missing.
D:\electric\JavaCodes\Java
symbol : class MySQLConn
location: class Login
private MySQLConn mysql;
^
D:\electric\JavaCodes\Java
symbol : class MySQLConn
location: class Login
mysql=new MySQLConn(config);
^
D:\electric\JavaCodes\Java
symbol : class VerticalFlowLayout
location: class Login
panelcontainer.setLayout(n
^
D:\electric\JavaCodes\Java
symbol: variable Stars
Stars.closeLoginFrame();
^
4 errors
Please could u also send the code for the MySQL connection and other classes. I think they are missing.
ASKER
rahul i copied the code and pasted it. Got a few errors which were fixed easily except these
D:\electric\JavaCodes\Java Programs\G etParam\Lo gin.java:1 7: cannot resolve symbol
symbol : class MySQLConn
location: class Login
private MySQLConn mysql;
^
D:\electric\JavaCodes\Java Programs\G etParam\Lo gin.java:2 5: cannot resolve symbol
symbol : class MySQLConn
location: class Login
mysql=new MySQLConn(config);
^
D:\electric\JavaCodes\Java Programs\G etParam\Lo gin.java:6 0: cannot resolve symbol
symbol : class VerticalFlowLayout
location: class Login
panelcontainer.setLayout(n ew VerticalFlowLayout());
^
D:\electric\JavaCodes\Java Programs\G etParam\Lo gin.java:1 08: cannot resolve symbol
symbol: variable Stars
Stars.closeLoginFrame();
^
4 errors
Please could u also send the code for the MySQL connection and other classes. I think they are missing.
D:\electric\JavaCodes\Java
symbol : class MySQLConn
location: class Login
private MySQLConn mysql;
^
D:\electric\JavaCodes\Java
symbol : class MySQLConn
location: class Login
mysql=new MySQLConn(config);
^
D:\electric\JavaCodes\Java
symbol : class VerticalFlowLayout
location: class Login
panelcontainer.setLayout(n
^
D:\electric\JavaCodes\Java
symbol: variable Stars
Stars.closeLoginFrame();
^
4 errors
Please could u also send the code for the MySQL connection and other classes. I think they are missing.
ASKER
rahul i copied the code and pasted it. Got a few errors which were fixed easily except these
D:\electric\JavaCodes\Java Programs\G etParam\Lo gin.java:1 7: cannot resolve symbol
symbol : class MySQLConn
location: class Login
private MySQLConn mysql;
^
D:\electric\JavaCodes\Java Programs\G etParam\Lo gin.java:2 5: cannot resolve symbol
symbol : class MySQLConn
location: class Login
mysql=new MySQLConn(config);
^
D:\electric\JavaCodes\Java Programs\G etParam\Lo gin.java:6 0: cannot resolve symbol
symbol : class VerticalFlowLayout
location: class Login
panelcontainer.setLayout(n ew VerticalFlowLayout());
^
D:\electric\JavaCodes\Java Programs\G etParam\Lo gin.java:1 08: cannot resolve symbol
symbol: variable Stars
Stars.closeLoginFrame();
^
4 errors
Please could u also send the code for the MySQL connection and other classes. I think they are missing.
D:\electric\JavaCodes\Java
symbol : class MySQLConn
location: class Login
private MySQLConn mysql;
^
D:\electric\JavaCodes\Java
symbol : class MySQLConn
location: class Login
mysql=new MySQLConn(config);
^
D:\electric\JavaCodes\Java
symbol : class VerticalFlowLayout
location: class Login
panelcontainer.setLayout(n
^
D:\electric\JavaCodes\Java
symbol: variable Stars
Stars.closeLoginFrame();
^
4 errors
Please could u also send the code for the MySQL connection and other classes. I think they are missing.
these are the errors to connect to the MySQL database
in the linux machine for that u need a mysql driver
since u r storing ur password in a file or something, remove MySQL statemnents.
instead add the file access statement.
in the linux machine for that u need a mysql driver
since u r storing ur password in a file or something, remove MySQL statemnents.
instead add the file access statement.
for verticalflow layout take this codeimport java.awt.*;
/**
* A vertical flow layout is used to lay out components in a panel.
* It will arrange the components from top to bottom until no more components fit in the same column.
* The columns can be aligned vertically.
*
* @version 1.0.2
*/
public class VerticalFlowLayout
implements LayoutManager
{
/**
* The top alignment variable.
*/
public static final int TOP = 1;
/**
* The centre alignment variable.
*/
public static final int CENTER = 0;
/**
* The bottom alignment variable.
*/
public static final int BOTTOM = 2;
/**
* Vertical alignment left
*/
public static final int LEFT = 3;
/**
* Vertical alignment right
*/
public static final int RIGHT = 4;
int align;
int vAlign; // Tim added horizontal alignment
int hgap;
int vgap;
boolean maximise;
/**
* Constructs a new layout with a centered alignment and a
* default 5-unit horizontal and vertical gap.
*/
public VerticalFlowLayout()
{
this(CENTER, 5, 5, false);
}
/**
* Constructs a new layout with the specified alignment and a
* default 5-unit horizontal and vertical gap.
* @param align the alignment value
*/
public VerticalFlowLayout(int align)
{
this(align, 5, 5, false);
}
/**
* Constructs a new layout with the specified alignment and gap
* values.
* @param align the alignment value
* @param hgap the horizontal gap variable
* @param vgap the vertical gap variable
*/
public VerticalFlowLayout(int align, int hgap, int vgap)
{
this(align, hgap, vgap, false);
}
/**
* Constructs a new layout with the specified alignment and gap
* values.
* @param align the alignment value
* @param hgap the horizontal gap variable
* @param vgap the vertical gap variable
*/
public VerticalFlowLayout(int align, int hgap, int vgap, boolean maximise)
{
if (align!=TOP && align!=CENTER && align!=BOTTOM)
throw new IllegalArgumentException(" illegal alignment value:"+align);
if (hgap<0)
throw new IllegalArgumentException(" horizontal gap is less than 0");
if (vgap<0)
throw new IllegalArgumentException(" vertical gap is less than 0");
this.align = align;
this.hgap = hgap;
this.vgap = vgap;
this.maximise = maximise;
}
/**
* Returns the alignment value for this layout (TOP, CENTER, or BOTTOM).
*/
public int getAlignment()
{
return align;
}
/**
* Sets the alignment value for this layout.
* @param align the alignment value (TOP, CENTER, or BOTTOM).
*/
public void setAlignment(int align)
{
if (align!=TOP && align!=CENTER && align!=BOTTOM)
throw new IllegalArgumentException(" illegal alignment value:"+align);
this.align = align;
}
/**
* Set the verical alignment value.
* @param aligns (LEFT, CENTER, or RIGHT)
*/
public void setHorizontalAlignment(int align)
{
if (align!=LEFT && align!=RIGHT && align!=CENTER)
throw new IllegalArgumentException(" illegal alignment value:"+align);
this.vAlign = align;
}
/**
* Returns the horizontal gap between components.
*/
public int getHgap()
{
return hgap;
}
/**
* Sets the horizontal gap between components.
* @param hgap the horizontal gap between components
*/
public void setHgap(int hgap)
{
if (hgap<0)
throw new IllegalArgumentException(" horizontal gap is less than 0");
this.hgap = hgap;
}
/**
* Returns the vertical gap between components.
*/
public int getVgap()
{
return vgap;
}
/**
* Sets the vertical gap between components.
* @param vgap the vertical gap between components
*/
public void setVgap(int vgap)
{
if (vgap<0)
throw new IllegalArgumentException(" vertical gap is less than 0");
this.vgap = vgap;
}
/**
* Adds the specified component to the layout. Not used by this class.
* @param name the name of the component
* @param comp the the component to be added
*/
public void addLayoutComponent(String name, Component comp)
{}
/**
* Removes the specified component from the layout. Not used by this class.
* @param comp the component to remove
*/
public void removeLayoutComponent(Comp onent comp) {
}
/**
* Returns the preferred dimensions for this layout given the components
* in the specified target container.
* @param target the component which needs to be laid out
* @see Container
* @see #minimumLayoutSize
*/
public Dimension preferredLayoutSize(Contai ner target)
{
Dimension dim = new Dimension(0, 0);
int nmembers = target.getComponentCount() ;
for (int i=0; i<nmembers; i++)
{
Component m = target.getComponent(i);
if (m.isVisible())
{
Dimension d = maximise ? m.getMaximumSize() : m.getPreferredSize();
dim.width = Math.max(dim.width, d.width);
if (i>1)
dim.height += vgap;
dim.height += d.height;
}
}
Insets insets = target.getInsets();
dim.width += insets.left + insets.right + hgap*2;
dim.height += insets.top + insets.bottom + vgap*2;
return dim;
}
/**
* Returns the minimum dimensions needed to layout the components
* contained in the specified target container.
* @param target the component which needs to be laid out
* @see #preferredLayoutSize
*/
public Dimension minimumLayoutSize(Containe r target)
{
Dimension dim = new Dimension(0, 0);
int nmembers = target.getComponentCount() ;
for (int i=0; i<nmembers; i++)
{
Component m = target.getComponent(i);
if (m.isVisible())
{
Dimension d = m.getMinimumSize();
dim.width = Math.max(dim.width, d.width);
if (i>0)
dim.height += vgap;
dim.height += d.height;
}
}
Insets insets = target.getInsets();
dim.width += insets.left + insets.right + hgap*2;
dim.height += insets.top + insets.bottom + vgap*2;
return dim;
}
/**
* Centers the elements in the specified column, if there is any slack.
* @param target the component which needs to be moved
* @param x the x coordinate
* @param y the y coordinate
* @param width the width dimensions
* @param height the height dimensions
* @param columnStart the beginning of the column
* @param columnEnd the the ending of the column
*/
private void moveComponents(Container target, int x, int y, int width, int height, int columnStart, int columnEnd)
{
switch (align)
{
case TOP:
break;
case CENTER:
y += height/2;
break;
case BOTTOM:
y += height;
break;
}
for (int i=columnStart; i<columnEnd; i++)
{
Component m = target.getComponent(i);
Rectangle mbounds = m.getBounds();
int X = x+(width-mbounds.width)/2; //center
if (m.isVisible())
{
if (vAlign == LEFT)
X = x;
else if (vAlign == RIGHT)
X = x+(width-mbounds.width);
m.setLocation(X, y);
y += vgap+mbounds.height;
}
}
}
/**
* Lays out the container. This method will actually reshape the
* components in the target in order to satisfy the constraints of
* the BorderLayout object.
* @param target the specified component being laid out.
* @see Container
*/
public void layoutContainer(Container target)
{
Insets insets = target.getInsets();
Rectangle bounds = target.getBounds();
int maxheight = bounds.height - (insets.top + insets.bottom + vgap*2);
int nmembers = target.getComponentCount() ;
int y = 0, x = insets.left + hgap;
int rowv = 0, start = 0;
for (int i=0; i<nmembers; i++)
{
Component m = target.getComponent(i);
if (m.isVisible())
{
Dimension d = maximise ? m.getPreferredSize() : m.getPreferredSize();
m.setSize(d.width, d.height);
if ((y==0) || ((y+d.height) <= maxheight))
{
if (y>0)
y += vgap;
y += d.height;
rowv = Math.max(rowv, d.width);
}
else
{
moveComponents(target, x, insets.top+vgap, rowv, maxheight-y, start, i);
x += hgap+rowv;
y = d.height;
rowv = d.width;
start = i;
}
}
}
moveComponents(target, x, insets.top+vgap, rowv, maxheight-y, start, nmembers);
}
/**
* Returns the string representation of this layout's values.
*/
public String toString()
{
StringBuffer buffer = new StringBuffer();
buffer.append(getClass().g etName());
buffer.append("[hgap=");
buffer.append(hgap);
buffer.append(",vgap=");
buffer.append(vgap);
switch (align) {
case TOP:
buffer.append(",align=top" );
break;
case CENTER:
buffer.append(",align=cent er");
break;
case BOTTOM:
buffer.append(",align=bott om");
break;
}
buffer.append("]");
return buffer.toString();
}
}
/**
* A vertical flow layout is used to lay out components in a panel.
* It will arrange the components from top to bottom until no more components fit in the same column.
* The columns can be aligned vertically.
*
* @version 1.0.2
*/
public class VerticalFlowLayout
implements LayoutManager
{
/**
* The top alignment variable.
*/
public static final int TOP = 1;
/**
* The centre alignment variable.
*/
public static final int CENTER = 0;
/**
* The bottom alignment variable.
*/
public static final int BOTTOM = 2;
/**
* Vertical alignment left
*/
public static final int LEFT = 3;
/**
* Vertical alignment right
*/
public static final int RIGHT = 4;
int align;
int vAlign; // Tim added horizontal alignment
int hgap;
int vgap;
boolean maximise;
/**
* Constructs a new layout with a centered alignment and a
* default 5-unit horizontal and vertical gap.
*/
public VerticalFlowLayout()
{
this(CENTER, 5, 5, false);
}
/**
* Constructs a new layout with the specified alignment and a
* default 5-unit horizontal and vertical gap.
* @param align the alignment value
*/
public VerticalFlowLayout(int align)
{
this(align, 5, 5, false);
}
/**
* Constructs a new layout with the specified alignment and gap
* values.
* @param align the alignment value
* @param hgap the horizontal gap variable
* @param vgap the vertical gap variable
*/
public VerticalFlowLayout(int align, int hgap, int vgap)
{
this(align, hgap, vgap, false);
}
/**
* Constructs a new layout with the specified alignment and gap
* values.
* @param align the alignment value
* @param hgap the horizontal gap variable
* @param vgap the vertical gap variable
*/
public VerticalFlowLayout(int align, int hgap, int vgap, boolean maximise)
{
if (align!=TOP && align!=CENTER && align!=BOTTOM)
throw new IllegalArgumentException("
if (hgap<0)
throw new IllegalArgumentException("
if (vgap<0)
throw new IllegalArgumentException("
this.align = align;
this.hgap = hgap;
this.vgap = vgap;
this.maximise = maximise;
}
/**
* Returns the alignment value for this layout (TOP, CENTER, or BOTTOM).
*/
public int getAlignment()
{
return align;
}
/**
* Sets the alignment value for this layout.
* @param align the alignment value (TOP, CENTER, or BOTTOM).
*/
public void setAlignment(int align)
{
if (align!=TOP && align!=CENTER && align!=BOTTOM)
throw new IllegalArgumentException("
this.align = align;
}
/**
* Set the verical alignment value.
* @param aligns (LEFT, CENTER, or RIGHT)
*/
public void setHorizontalAlignment(int
{
if (align!=LEFT && align!=RIGHT && align!=CENTER)
throw new IllegalArgumentException("
this.vAlign = align;
}
/**
* Returns the horizontal gap between components.
*/
public int getHgap()
{
return hgap;
}
/**
* Sets the horizontal gap between components.
* @param hgap the horizontal gap between components
*/
public void setHgap(int hgap)
{
if (hgap<0)
throw new IllegalArgumentException("
this.hgap = hgap;
}
/**
* Returns the vertical gap between components.
*/
public int getVgap()
{
return vgap;
}
/**
* Sets the vertical gap between components.
* @param vgap the vertical gap between components
*/
public void setVgap(int vgap)
{
if (vgap<0)
throw new IllegalArgumentException("
this.vgap = vgap;
}
/**
* Adds the specified component to the layout. Not used by this class.
* @param name the name of the component
* @param comp the the component to be added
*/
public void addLayoutComponent(String name, Component comp)
{}
/**
* Removes the specified component from the layout. Not used by this class.
* @param comp the component to remove
*/
public void removeLayoutComponent(Comp
}
/**
* Returns the preferred dimensions for this layout given the components
* in the specified target container.
* @param target the component which needs to be laid out
* @see Container
* @see #minimumLayoutSize
*/
public Dimension preferredLayoutSize(Contai
{
Dimension dim = new Dimension(0, 0);
int nmembers = target.getComponentCount()
for (int i=0; i<nmembers; i++)
{
Component m = target.getComponent(i);
if (m.isVisible())
{
Dimension d = maximise ? m.getMaximumSize() : m.getPreferredSize();
dim.width = Math.max(dim.width, d.width);
if (i>1)
dim.height += vgap;
dim.height += d.height;
}
}
Insets insets = target.getInsets();
dim.width += insets.left + insets.right + hgap*2;
dim.height += insets.top + insets.bottom + vgap*2;
return dim;
}
/**
* Returns the minimum dimensions needed to layout the components
* contained in the specified target container.
* @param target the component which needs to be laid out
* @see #preferredLayoutSize
*/
public Dimension minimumLayoutSize(Containe
{
Dimension dim = new Dimension(0, 0);
int nmembers = target.getComponentCount()
for (int i=0; i<nmembers; i++)
{
Component m = target.getComponent(i);
if (m.isVisible())
{
Dimension d = m.getMinimumSize();
dim.width = Math.max(dim.width, d.width);
if (i>0)
dim.height += vgap;
dim.height += d.height;
}
}
Insets insets = target.getInsets();
dim.width += insets.left + insets.right + hgap*2;
dim.height += insets.top + insets.bottom + vgap*2;
return dim;
}
/**
* Centers the elements in the specified column, if there is any slack.
* @param target the component which needs to be moved
* @param x the x coordinate
* @param y the y coordinate
* @param width the width dimensions
* @param height the height dimensions
* @param columnStart the beginning of the column
* @param columnEnd the the ending of the column
*/
private void moveComponents(Container target, int x, int y, int width, int height, int columnStart, int columnEnd)
{
switch (align)
{
case TOP:
break;
case CENTER:
y += height/2;
break;
case BOTTOM:
y += height;
break;
}
for (int i=columnStart; i<columnEnd; i++)
{
Component m = target.getComponent(i);
Rectangle mbounds = m.getBounds();
int X = x+(width-mbounds.width)/2;
if (m.isVisible())
{
if (vAlign == LEFT)
X = x;
else if (vAlign == RIGHT)
X = x+(width-mbounds.width);
m.setLocation(X, y);
y += vgap+mbounds.height;
}
}
}
/**
* Lays out the container. This method will actually reshape the
* components in the target in order to satisfy the constraints of
* the BorderLayout object.
* @param target the specified component being laid out.
* @see Container
*/
public void layoutContainer(Container target)
{
Insets insets = target.getInsets();
Rectangle bounds = target.getBounds();
int maxheight = bounds.height - (insets.top + insets.bottom + vgap*2);
int nmembers = target.getComponentCount()
int y = 0, x = insets.left + hgap;
int rowv = 0, start = 0;
for (int i=0; i<nmembers; i++)
{
Component m = target.getComponent(i);
if (m.isVisible())
{
Dimension d = maximise ? m.getPreferredSize() : m.getPreferredSize();
m.setSize(d.width, d.height);
if ((y==0) || ((y+d.height) <= maxheight))
{
if (y>0)
y += vgap;
y += d.height;
rowv = Math.max(rowv, d.width);
}
else
{
moveComponents(target, x, insets.top+vgap, rowv, maxheight-y, start, i);
x += hgap+rowv;
y = d.height;
rowv = d.width;
start = i;
}
}
}
moveComponents(target, x, insets.top+vgap, rowv, maxheight-y, start, nmembers);
}
/**
* Returns the string representation of this layout's values.
*/
public String toString()
{
StringBuffer buffer = new StringBuffer();
buffer.append(getClass().g
buffer.append("[hgap=");
buffer.append(hgap);
buffer.append(",vgap=");
buffer.append(vgap);
switch (align) {
case TOP:
buffer.append(",align=top"
break;
case CENTER:
buffer.append(",align=cent
break;
case BOTTOM:
buffer.append(",align=bott
break;
}
buffer.append("]");
return buffer.toString();
}
}
ASKER
rahul i have compiled the code successfully, now how do I run it.
I mean how do I use the Login constructor to open the Login form.
I mean how do I use the Login constructor to open the Login form.
ASKER
was having the problem running it using JCreator 2.5
but when ran from command prompt by supplying the ClassPath for tools.jar, it ran successfully. rahul can u explain y tools.jar was required in the classpath?
Also i noticed you did not add the
this.show();
line in the JPanel code to display the login form, is it that u missed it or are there other ways to display this form after the constructor is created?
Thanks.
Nomi.
but when ran from command prompt by supplying the ClassPath for tools.jar, it ran successfully. rahul can u explain y tools.jar was required in the classpath?
Also i noticed you did not add the
this.show();
line in the JPanel code to display the login form, is it that u missed it or are there other ways to display this form after the constructor is created?
Thanks.
Nomi.