First Swing application..

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 10043 Views - Last Post: 29 December 2010 - 10:32 PM Rate Topic: -----

#1 Guest_mandy*


Reputation:

First Swing application..

Posted 20 December 2010 - 03:37 PM

Hello,
i have to create a login form in Java Swing....i have the form and added all the GUI components needed on it but i haven't the foggiest as to what to write in the code in this part:

private void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt)      {                                                
      //TODO add your handling code here: //i have no idea how to implement this password?
    }                                               

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
    {  
      //TODO add your handling code here: //If password correct then message appears "Welcome to Online Computers" and form turns green. If incorrect form turns red and another message appears to say "Login Incorrect"
    }

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) 
   {
        // TODO add your handling code here:
    }




The entire code.

/*
 * CompJFrame.java
 */

package ComputersOnline;

import java.awt.*;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventListener;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
/**
 *
 * @author mandy
 */
public class CompJFrame extends javax.swing.JFrame
{
    private ActionListener CompJFrame;
    private boolean jPasswordField;
    private Object message;
    private Object password;
    

    /** Creates new form CompJFrame */
    public CompJFrame()
    {
        initComponents();
    }

   /** 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() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jPasswordField1 = new javax.swing.JPasswordField();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Computers Online");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setText("Username:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel2.setText("Password:");

        jPasswordField1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jPasswordField1.setBorder(new   javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        jPasswordField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPasswordField1ActionPerformed(evt);
            }
        });

        jTextField1.setFont(new java.awt.Font("Tahoma", 0, 12));
        jTextField1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        jTextField1.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jButton1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jButton1.setText("Login");
        jButton1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jPasswordField1, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE))))
                .addGap(62, 62, 62))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(72, 72, 72)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(37, 37, 37)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(35, 35, 35)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(41, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(28, Short.MAX_VALUE))
        );

        pack();
    }// END of Generated CODE



    private void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt)      {                                                
      //TODO add your handling code here:
    }                                               

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
    {  
      //TODO add your handling code here:
    }

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) 
   {
        // TODO add your handling code here:
    }
    

/**
* @param args the command line arguments
*/
public static void main(String args[])
{

new CompJFrame().setVisible(true);

}

// Variables declaration - do not modify
javax.swing.JButton jButton1;
javax.swing.JLabel jLabel1;
javax.swing.JLabel jLabel2;
javax.swing.JPanel jPanel1;
javax.swing.JPasswordField jPasswordField1;
javax.swing.JTextField jTextField1;
// End of variables declaration




}




[/code]

Is This A Good Question/Topic? 0

Replies To: First Swing application..

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,244
  • Joined: 27-December 08

Re: First Swing application..

Posted 20 December 2010 - 03:39 PM

We are happy to help you with code you wrote. However, the NetBeans GUI Generator produces code that isn't readable nor maintainable for humans. Swing is so easy to use that you should write all your GUIs by hand. In fact, there are a nubmer of Swing tutorials in the Java Tutorials section.
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: First Swing application..

Posted 20 December 2010 - 04:25 PM

What will you do at your next interview when they will ask you to show your Java knowledge by writting a simple GUI with Notepad as editor ?
Was This Post Helpful? 0
  • +
  • -

#4 Guest_mandy*


Reputation:

Re: First Swing application..

Posted 20 December 2010 - 05:50 PM

Hi, firstly this is for a course and has nothing to do with my work....im a nurse!!Lol

Yes i do realise the Netbeans interface generates a lot of code but i can't do anything about that really...i have to use netbeans (course teacher says so!) but the generated code i am pretty sure is just for all the gui components presentation(eg fonts, form layout and size,and all the other graphical "prettiness". I just dragged and dropped each component onto the form.Will check out the tutorials then.
Was This Post Helpful? 0

#5 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: First Swing application..

Posted 20 December 2010 - 07:33 PM

Well before I can help you, I guess my first question would be: how much do you actually know? Do you understand the basic concepts of programming at least?

Now, let's take a look at what you're trying to do:

private void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt)      {                                                
      //TODO add your handling code here: //i have no idea how to implement this password?
    }                                               

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
    {  
      //TODO add your handling code here: //If password correct then message appears "Welcome to Online Computers" and form turns green. If incorrect form turns red and another message appears to say "Login Incorrect"
    }

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) 
   {
        // TODO add your handling code here:
    }




Right now I'm a little confused what the method jPasswordField1ActionPerformed is for. Obviously it is what is handling what happens when the user interacts with the password field, but what needs to be done exactly?

The next method, jButton1ActionPerformed, is a little more clear. So I'm assuming there is a Login button of some kind. When the button is pressed, this will get called. So firstly, how do we know if a password is correct? I'm assuming it's most likely just stored in a variable. So you are going to want to check to see if the text in the textfield is the same as the password string. That would be something like:
if(passField.getText().equals(passwordText))
{
//do something
}



Now you say depending on if it's the correct password or not, you want to change the color of the form. Do you mean you want to change the background color?

You could do this by setting the background color of the panel. Something like:
if(passText.getText().equals(passwordText))
{
panel.setBackground(color1);
// here color1 is a variable of type Color
}



Well this will hopefully get you off on the right foot.

By the way, you can find an excellent Swing tutorial here:
http://zetcode.com/t...aswingtutorial/

This post has been edited by eZACKe: 20 December 2010 - 07:44 PM

Was This Post Helpful? 0
  • +
  • -

#6 Guest_mandy*


Reputation:

Re: First Swing application..

Posted 21 December 2010 - 04:55 AM

Thankyou!I am a java beginner i know the basic concepts of Java, i got confused when it comes to Swing and how to amalgamate ordinary Java with the swing concepts and components. I think it is getting started with it and will try your suggestions below.

"Right now I'm a little confused what the method jPasswordField1ActionPerformed is for. Obviously it is what is handling what happens when the user interacts with the password field, but what needs to be done exactly?" //This piece of code may not be needed.


Our specification for this is:

"Your task is to create a graphical application using Java Swing which simulates an online shop
selling computers with di fferent characteristics (e.g. laptop, desktop, CPU, speed, hard drive,
graphics card, make).
Customers should be able to choose the characteristics of the computer
that they would like to purchase using appropriate buttons, radio buttons, checkboxes, etc.
The details for the implementation of the system are given in the steps below:

1. 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 rst 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 (*)

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 Online 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.



3. After the user logins, he is presented with various choices about buying a computer. Design
and implement one or more appropriate screens in your Swing application which allow the
user to select whether he wishes to buy a laptop or desktop and subsequently what kind of
"appropriate" components he would like to have in his laptop or desktop (e.g. CPU type
and speed, hard disk, graphics card, etc.).
The various choices should be presented using radio buttons, checkboxes and other Swing
JComponents."
Was This Post Helpful? 0

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: First Swing application..

Posted 21 December 2010 - 05:39 AM

Quote

Yes i do realise the Netbeans interface generates a lot of code but i can't do anything about that really...i have to use netbeans (course teacher says so!)


Does the instructor require you to use the NetBeans GUI Builder? IMO, the value of this assignment is in learning to write the Swing code yourself, and it's not that hard.
Was This Post Helpful? 0
  • +
  • -

#8 Guest_mandy*


Reputation:

Re: First Swing application..

Posted 21 December 2010 - 07:32 AM

Quote

Does the instructor require you to use the NetBeans GUI Builder? IMO, the value of this assignment is in learning to write the Swing code yourself, and it's not that hard.


Yes we are using the netbeans GUI bulider!!We were told to use the netbeans java desktop application.

And yes Obviously it is about learning to write the code....i figured that part out for myself!
Was This Post Helpful? 0

#9 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: First Swing application..

Posted 21 December 2010 - 01:11 PM

Quote

And yes Obviously it is about learning to write the code....i figured that part out for myself!


I didn't assume that you took the short bus to school, and I'd appreciate the same courtesy.

When I suggested that learning to code the Swing code was the value of the exercise, I was referring to the graphical (or GUI) part of the code generated by the NetBeans GUI Builder, the part that (1) you don't understand, (2) that you won't understand after you complete the assignment, and (3) that few humans can read and help you with.

But the assignment (or your interpretation of it) is what it is. Good luck with it!
Was This Post Helpful? 1
  • +
  • -

#10 Guest_mandy*


Reputation:

Re: First Swing application..

Posted 21 December 2010 - 02:43 PM

I was just responding to your original statement which was patronising......."the point of the exercise is to learn the code".

Sorry if it came across as being discourteous.....


I do know what most of the gui code is for, if you read my previous posts......Each component on the JFrame firstly has to be declared,then the generated code for each includes layouts,the position and placement of each component, size,colour, shape etc.Each "Event" that occurs has an Interface, an event listener, and an action performed.

My blockage at present is to make it do what i want it to do....i know what it has to do is simplistic which is probably why i have gotten stuck (trying too hard and looking for something more complex than is needed) i also missed the class on Java Swing.

Thankyou for your input
Was This Post Helpful? 0

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: First Swing application..

Posted 21 December 2010 - 07:04 PM

So here is a start
import java.awt.event.*;
import java.awt.*;

import javax.swing.*;

public class Login extends JFrame implements ActionListener {

	JTextField usernameT, passwordT;
	JButton button;
	
	Login() {
		super("Login form");
		
		// make a JPanel 3 X 2
		JPanel p = new JPanel(new GridLayout(3,2));
		
		// first row a label a a textfield
		p.add(new JLabel("Username:"));
		usernameT = new JTextField(20);
		p.add(usernameT);
		// second row a label a a textfield
		p.add(new JLabel("Password:"));
		passwordT = new JTextField(20);
		p.add(passwordT);
		// the button
		button = new JButton("Login");
		// call back the actionPerformed()
		button.addActionListener(this);
		p.add(button);
		add(p);
		
		// standard JFrame operations
		setVisible(true);
		pack();
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);	
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		System.out.println("The button was hit");
		System.out.println("Content of usernameT: " + usernameT.getText());
		System.out.println("Content of passwordT: " + passwordT.getText());
		
	}
	
	// to launch the application
	public static void main(String[] args) {
		new Login();
	}
}


Was This Post Helpful? 0
  • +
  • -

#12 Guest_mandy*


Reputation:

Re: First Swing application..

Posted 22 December 2010 - 05:32 PM

Thankyou very much for this, it is appreciated. i also found a fantastic video about event handling on YouTube, worth a look.....i'm understanding more now.

http://www.youtube.c...feature=channel
Was This Post Helpful? 0

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,244
  • Joined: 27-December 08

Re: First Swing application..

Posted 22 December 2010 - 06:55 PM

I also have a tutorial on Event-Driven Programming that you may find helpful. :)
Was This Post Helpful? 0
  • +
  • -

#14 Guest_mandy*


Reputation:

Re: First Swing application..

Posted 23 December 2010 - 05:31 AM

View Postmacosxnerd101, on 22 December 2010 - 05:55 PM, said:

I also have a tutorial on Event-Driven Programming that you may find helpful. :)


Y&s it is thankyou for the link!Lots of "Ah i see" moments :-) especially about game programming that makes more sense also. I do have a question, where is the "main method" included in GUI programming or does it not matter if it is a seperate class( with its own seperate file(outer class)

Is there a rule of thumb for it at all?
Was This Post Helpful? 0

#15 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,244
  • Joined: 27-December 08

Re: First Swing application..

Posted 23 December 2010 - 03:19 PM

Generally, the Main class is its own separate class, and each class you design has a main() method for unit testing. :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2