Welcome to Dream.In.Code
Become a Java Expert!

Join 149,520 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,395 people online right now. Registration is fast and FREE... Join Now!




easy one here: Need o display number of users in GUI

 
Reply to this topicStart new topic

easy one here: Need o display number of users in GUI

dbrine
18 May, 2007 - 04:42 PM
Post #1

New D.I.C Head
*

Joined: 25 Apr, 2007
Posts: 48


My Contributions
I wrotr code that will track user logon/logoffs. But I need to display that number in the Number Of users box in the GUI. I cannot figure it for the life of me.

CODE

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class logonTracking extends JFrame implements ActionListener
{
        //Declaring variables
       String users;
       boolean success;

       //Construct variables
       String userArray[] = {"dbrine", "bbrine", "mbrine", "kbrine"};

       //construct a panel for the fields and buttons
    JPanel userPanel = new JPanel();
    JPanel buttonPanel = new JPanel();
    JPanel numberOfUsersPanel = new JPanel();

    //construct labels and text boxes
    JLabel userLabel = new JLabel("User:  ");
        JTextField user = new JTextField(10);
    JLabel numOfUsersLabel = new JLabel("Number of Users:  ");
        JTextField NumoUs = new JTextField(4);

    //construct buttons
    JButton logonButton = new JButton("Logon");
    JButton logoffButton = new JButton("Logoff");



    public static void main(String[] args)
    {
        logonTracking f = new logonTracking();
        f.setSize(200,125);
        f.setTitle("User Tracking");
        f.setResizable(false);
        f.setLocation(200,200);
        f.setVisible(true);
    }

    public logonTracking()
    {
        //add user panel to content panel
        JPanel userPanel= new JPanel(new BorderLayout());
        userPanel.add(userLabel,BorderLayout.WEST);
        userPanel.add(user,BorderLayout.EAST);

        //add button panel to content panel
        JPanel buttonPanel= new JPanel(new FlowLayout());
        buttonPanel.add(logonButton);
        buttonPanel.add(logoffButton);

        //add number of users panel
        JPanel numberOfUsersPanel= new JPanel(new BorderLayout());
        numberOfUsersPanel.add(numOfUsersLabel,BorderLayout.WEST);
        numberOfUsersPanel.add(NumoUs,BorderLayout.EAST);

        //add content panel to frame
        JPanel contentPanel= new JPanel(new FlowLayout());
        contentPanel.add(userPanel);
        contentPanel.add(buttonPanel);
        contentPanel.add(numberOfUsersPanel);
        setContentPane(contentPanel);

        logonButton.addActionListener(this);
        logoffButton.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e)
    {
        int NumoUs;
        NumoUs = 0;

        users = user.getText();//users id name
                  success = false; //assumes failed login

                  //Sequential search
                  for (int i = 0; i < userArray.length; i++)

                  if((userArray[i].compareTo(users)==0))
                  success = true;
                   {
                          if(success == true)
                          {
                           NumoUs++;

                           }
                           else if(success == false)
                           {
                               NumoUs--;
                               JOptionPane.showMessageDialog(this,"UserName not Found",                                                                   "Invalid username. Try again.", JOptionPane.ERROR_MESSAGE);

                           clearFields();
                           }
                }
   }

                //clears the login and password if incorrect
                   public void clearFields()
                           {
                               user.setText("");
                               user.requestFocus();
                }

}

User is offlineProfile CardPM
+Quote Post

keems21
RE: Easy One Here: Need O Display Number Of Users In GUI
18 May, 2007 - 08:34 PM
Post #2

D.I.C Head
Group Icon

Joined: 3 Feb, 2007
Posts: 183



Thanked: 2 times
Dream Kudos: 25
My Contributions
Try using and Integer instead of an int.
Change:
CODE
int NumoUs;

to
CODE
Integer NumoUs = new Integer(0);


Then you can send it to the text field by just calling the toString() method
CODE
user.setText(NumoUs.toString());


And all you need to do to get the value back from that same text field is parse it as an Integer
CODE
NumoUs = Integer.getInteger(user.getText());


Hope that this helps.
User is offlineProfile CardPM
+Quote Post

dbrine
RE: Easy One Here: Need O Display Number Of Users In GUI
19 May, 2007 - 12:11 PM
Post #3

New D.I.C Head
*

Joined: 25 Apr, 2007
Posts: 48


My Contributions
thanks. I tried that but it changes the username to a number. I need to display the number of logged on users in the textbox, then I will have to make it uneditable by the user.


QUOTE(keems21 @ 18 May, 2007 - 09:34 PM) *

Try using and Integer instead of an int.
Change:
CODE
int NumoUs;

to
CODE
Integer NumoUs = new Integer(0);


Then you can send it to the text field by just calling the toString() method
CODE
user.setText(NumoUs.toString());


And all you need to do to get the value back from that same text field is parse it as an Integer
CODE
NumoUs = Integer.getInteger(user.getText());


Hope that this helps.


User is offlineProfile CardPM
+Quote Post

keems21
RE: Easy One Here: Need O Display Number Of Users In GUI
19 May, 2007 - 03:10 PM
Post #4

D.I.C Head
Group Icon

Joined: 3 Feb, 2007
Posts: 183



Thanked: 2 times
Dream Kudos: 25
My Contributions
QUOTE(dbrine @ 19 May, 2007 - 01:11 PM) *

thanks. I tried that but it changes the username to a number. I need to display the number of logged on users in the textbox, then I will have to make it uneditable by the user.


By making the int to an Integer though, you're making it into an Object. That way you can change it from a number to a String just by using the toString() method on it.

As for making the textField uneditable, use this method:
textFieldName.setEditable(false);

That should do the trick.
User is offlineProfile CardPM
+Quote Post

dbrine
RE: Easy One Here: Need O Display Number Of Users In GUI
19 May, 2007 - 05:06 PM
Post #5

New D.I.C Head
*

Joined: 25 Apr, 2007
Posts: 48


My Contributions
I tried the code as you have it below. But instead of diaplying the number in the textbox next to the number of users it replaces the username.

CODE

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class logonTracking extends JFrame implements ActionListener
{
        //Declaring variables
       String users;
       boolean success;

       //Construct variables
       String userArray[] = {"dbrine", "bbrine", "mbrine", "kbrine"};

       //construct a panel for the fields and buttons
    JPanel userPanel = new JPanel();
    JPanel buttonPanel = new JPanel();
    JPanel numberOfUsersPanel = new JPanel();

    //construct labels and text boxes
    JLabel userLabel = new JLabel("User:  ");
        JTextField user = new JTextField(10);
    JLabel numOfUsersLabel = new JLabel("Number of Users:  ");
        JTextField NumoUs = new JTextField(4);

    //construct buttons
    JButton logonButton = new JButton("Logon");
    JButton logoffButton = new JButton("Logoff");



    public static void main(String[] args)
    {
        logonTracking f = new logonTracking();
        f.setSize(200,125);
        f.setTitle("User Tracking");
        f.setResizable(false);
        f.setLocation(200,200);
        f.setVisible(true);
    }

    public logonTracking()
    {
        //add user panel to content panel
        JPanel userPanel= new JPanel(new BorderLayout());
        userPanel.add(userLabel,BorderLayout.WEST);
        userPanel.add(user,BorderLayout.EAST);

        //add button panel to content panel
        JPanel buttonPanel= new JPanel(new FlowLayout());
        buttonPanel.add(logonButton);
        buttonPanel.add(logoffButton);

        //add number of users panel
        JPanel numberOfUsersPanel= new JPanel(new BorderLayout());
        numberOfUsersPanel.add(numOfUsersLabel,BorderLayout.WEST);
        numberOfUsersPanel.add(NumoUs,BorderLayout.EAST);

        //add content panel to frame
        JPanel contentPanel= new JPanel(new FlowLayout());
        contentPanel.add(userPanel);
        contentPanel.add(buttonPanel);
        contentPanel.add(numberOfUsersPanel);
        setContentPane(contentPanel);

        logonButton.addActionListener(this);
        logoffButton.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e)
    {
        Integer NumoUs = new Integer(0);

        users = user.getText();//users id name
                  success = false; //assumes failed login

                  //Sequential search
                  for (int i = 0; i < userArray.length; i++)

                  if((userArray[i].compareTo(users)==0))
                  success = true;
                   {
                          if(success == true)
                          {
                           NumoUs++;
                        user.setText(NumoUs.toString());
                        NumoUs = Integer.getInteger(user.getText());
                           }
                           else if(success == false)
                           {
                               NumoUs--;
                               JOptionPane.showMessageDialog(this,"UserName not Found",                                                                   "Invalid username. Try again.", JOptionPane.ERROR_MESSAGE);


                           clearFields();
                           }
                }
   }

                //clears the login and password if incorrect
                   public void clearFields()
                           {
                               user.setText("");
                               user.requestFocus();
                }

}



QUOTE(keems21 @ 19 May, 2007 - 04:10 PM) *

QUOTE(dbrine @ 19 May, 2007 - 01:11 PM) *

thanks. I tried that but it changes the username to a number. I need to display the number of logged on users in the textbox, then I will have to make it uneditable by the user.


By making the int to an Integer though, you're making it into an Object. That way you can change it from a number to a String just by using the toString() method on it.

As for making the textField uneditable, use this method:
textFieldName.setEditable(false);

That should do the trick.


User is offlineProfile CardPM
+Quote Post

keems21
RE: Easy One Here: Need O Display Number Of Users In GUI
19 May, 2007 - 06:24 PM
Post #6

D.I.C Head
Group Icon

Joined: 3 Feb, 2007
Posts: 183



Thanked: 2 times
Dream Kudos: 25
My Contributions
I didn't look at your code closely enough. I thought that user was the textField that kept track of the number of users logged in.

But you should look at the code that I gave you. It's doing exactly what you tell it to, it's taking the input and putting it in the user textField. Fixing it should be trivial, all you need to do is switch the name of the textField that you want it to write to.

CODE

NumoUs.setText(NumoUs.toString());
NumoUs = Integer.getInteger(NumoUs.getText());  //you shouldn't even need this line of code


Edit:
OK, now I think that you've got a problem. You've the name for one of your textFields and your Integer the same name. It may compile correctly, but regardless, you should never have two names representing two different things. It gets really confusing to someone trying to read your code. Hopefully you can figure this one out on your own.

This post has been edited by keems21: 19 May, 2007 - 06:26 PM
User is offlineProfile CardPM
+Quote Post

dbrine
RE: Easy One Here: Need O Display Number Of Users In GUI
19 May, 2007 - 06:32 PM
Post #7

New D.I.C Head
*

Joined: 25 Apr, 2007
Posts: 48


My Contributions
I get an error

logonTracking.java:86: cannot find symbol
symbol : method setText(java.lang.String)
location: class java.lang.Integer
NumoUs.setText(NumoUs.toString());



QUOTE(keems21 @ 19 May, 2007 - 07:24 PM) *

I didn't look at your code closely enough. I thought that user was the textField that kept track of the number of users logged in.

But you should look at the code that I gave you. It's doing exactly what you tell it to, it's taking the input and putting it in the user textField. Fixing it should be trivial, all you need to do is switch the name of the textField that you want it to write to.

CODE

NumoUs.setText(NumoUs.toString());
NumoUs = Integer.getInteger(NumoUs.getText());  //you shouldn't even need this line of code


Edit:
OK, now I think that you've got a problem. You've the name for one of your textFields and your Integer the same name. It may compile correctly, but regardless, you should never have two names representing two different things. It gets really confusing to someone trying to read your code. Hopefully you can figure this one out on your own.



WWWWEEE..



Got it finally. Thanks!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1



QUOTE(dbrine @ 19 May, 2007 - 07:30 PM) *

I get an error

logonTracking.java:86: cannot find symbol
symbol : method setText(java.lang.String)
location: class java.lang.Integer
NumoUs.setText(NumoUs.toString());



QUOTE(keems21 @ 19 May, 2007 - 07:24 PM) *

I didn't look at your code closely enough. I thought that user was the textField that kept track of the number of users logged in.

But you should look at the code that I gave you. It's doing exactly what you tell it to, it's taking the input and putting it in the user textField. Fixing it should be trivial, all you need to do is switch the name of the textField that you want it to write to.

CODE

NumoUs.setText(NumoUs.toString());
NumoUs = Integer.getInteger(NumoUs.getText());  //you shouldn't even need this line of code


Edit:
OK, now I think that you've got a problem. You've the name for one of your textFields and your Integer the same name. It may compile correctly, but regardless, you should never have two names representing two different things. It gets really confusing to someone trying to read your code. Hopefully you can figure this one out on your own.



User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 08:24PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month