adding user privilege to application

i would like to add user privilege to my swing applicationafter login to the application the user sould get an privilege numberwhat is the best why to disable/hide/showbuttons/menus according to the user privilegethanks
[254 byte] By [shay_tea] at [2007-10-2 20:38:39]
# 1

Actually what you want it not posible to write a whole program for you but here is a example below I guess it would be help full to get the idea how to make add user gui & how to do it.

/**

* @author MD WAHID HOSSAIN

* @version 0.1

*/

import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

import java.io.*;

/**

* Class IncorrectPasswordException is new class extends Exception class,

* here the exception are catched.

*/

class IncorrectPasswordException extends Exception {

/**IncorrectPasswordException() is a constructor which showing the exception

* in a

*JOptionPane.ERROR_MESSAGE*/

IncorrectPasswordException( ){

JOptionPane.showMessageDialog(null,

"Username/Password detction problem please try again. ", "Exception",

JOptionPane.ERROR_MESSAGE);

}}

/** Loging is the main class which extends JFrame

* This class create a GUI that Takes Username & Password from a user

* then check the user name & password from a external .txt file.

* If the user type wrong username or password 3 times in ANY ORDER then the GUI is

* p[ermentaly blocked. if the user try to type the username & password again then he/she

* must restart the program. */

public class Login extends JFrame {

/** Those Labels are used to give instruction to user*/

private JLabel Instruction, UserName, Password, StatusBar;

/**This text field used to take the Username from user */

private JTextField NameField;

/**This password field used to take the password from user */

private JPasswordField Pass;

/** Cancel button is used to terminate the program & Ok button used to cheking the Username & Password */

private JButton Okbtn, Cancelbtn;

/**Declearing four panel to set the GUI */

private JPanel pnl[] = new JPanel[4];

/** str is String object to set the string for satus bar*/

private String str;

/**Integers to indexing */

private Integer count, index1, index2;

/** userdata & passdata are .txt file where the username & password are saved */

private RandomAccessFile userdata, passdata;

/**array to cheking the username & password */

private String name[], password[];

/** String for cheking one by one */

private String strname , strpass;

/**Loging class constructor which set the GUI */

public Login(){

/**To set the title */

super("Login");

count = 1;

/**set the User information */

Instruction = new JLabel("Enter your username qnd password");

/** changing the color of the user information message*/

Instruction.setForeground(Color.BLUE);

/** set the front type, style & size for user information*/

Instruction.setFont( new Font( "Serif", Font.BOLD + Font.ITALIC, 12 ) );

pnl[0] = new JPanel();

pnl[0].add(Instruction);

pnl[1] = new JPanel();

/** set the label in where the user write the username*/

UserName = new JLabel("Username: ");

/** changing the color of the label*/

UserName.setForeground(Color.BLUE);

/** set the label in where the user write the password*/

Password = new JLabel("Password: ");

/** changing the color of the label*/

Password.setForeground(Color.BLUE);

/** instantiating textfield with 10 character*/

NameField = new JTextField( 10);

/** instantiating passwordfield with 10 character*/

Pass = new JPasswordField( 10 );

/** labeling the Ok button*/

Okbtn = new JButton("OK");

/** changing the color of the Ok button label*/

Okbtn.setForeground(Color.BLUE);

/** Set the keyboard interface O for Ok button*/

Okbtn.setMnemonic( 'O' );

/** labeling the Cancel button*/

Cancelbtn = new JButton("Cancel");

/** Set the keyboard interface C for Cancel button*/

Cancelbtn.setMnemonic( 'C' );

/** changing the color of the Cancel button label*/

Cancelbtn.setForeground(Color.RED);

pnl[1] = new JPanel();

/** Change the panel lay out to GridLayout with 3 column, 2 row & space gap is set to 5

* pixel*/

pnl[1].setLayout(new GridLayout( 3, 2, 5, 5));

pnl[1].add(UserName);

pnl[1].add(NameField);

pnl[1].add(Password);

pnl[1].add(Pass);

pnl[1].add(Cancelbtn);

pnl[1].add(Okbtn);

str = " ";

/** instantiating satusbar with label*/

StatusBar = new JLabel(str);

/** set the colour for the satusbar*/

StatusBar.setForeground(Color.RED);

/** set the front type, style & size for statusbar*/

StatusBar.setFont( new Font( "Serif", Font.BOLD + Font.ITALIC, 11 ) );

pnl[2] = new JPanel();

pnl[2].add(StatusBar);

pnl[3] = new JPanel();

pnl[3].setLayout( new BorderLayout());

pnl[3].add(pnl[0],BorderLayout.NORTH);

pnl[3].add(pnl[1],BorderLayout.CENTER);

pnl[3].add(pnl[2],BorderLayout.SOUTH);

/** calling the getinfo() funbction*/

getinfo();

/** Creating the inner class for the Cancel button actionlistener*/

Cancelbtn.addActionListener( new ActionListener() {

public void actionPerformed( ActionEvent t ) {

System.exit(0);

}

}

);

/** Creating the inner class for the Ok button actionlistener*/

Okbtn.addActionListener( new ActionListener() {

public void actionPerformed( ActionEvent t ) {

try{

/** calling the call() function*/

call();

}

catch(Exception e){

JOptionPane.showMessageDialog(null,

"" + e, "Exception",

JOptionPane.ERROR_MESSAGE);

}

}

}

);

/** Assigning the enter button for Password field*/

Pass.addActionListener( new ActionListener() {

public void actionPerformed( ActionEvent t ) {

/** calling the call() function*/

call();

}

}

);

this.getContentPane().add(pnl[3]);

this.pack();

this.setVisible( true );

}

/** Defining the getinfo() function. return type is void*/

private void getinfo()

{

/** instantiating name[] with 100 obj*/

name = new String[100];

/** instantiating password[] with 100 obj*/

password = new String[100];

try{

/** instantiating RandomAccessFile with User.txt as readmode*/

userdata = new RandomAccessFile( "User.txt","r");

/** instantiating RandomAccessFile with Pass.txt as readmode*/

passdata = new RandomAccessFile( "Pass.txt","r");

index1 = index2 = 0;

/** Geting the Username from the User.txt file in name[]*/

while ((name[index1++] = userdata.readLine())!=null && index1<100);

/** Geting the Password from the Pass.txt file in password[]*/

while ((password[index2++] = passdata.readLine())!=null && index2<100);

}

/** catching the FileNotFoundException & showing in a JOptionPane.ERROR_MESSAGE*/

catch( FileNotFoundException e)

{

JOptionPane.showMessageDialog(null,

" not found in local directory", "File Not Found",

JOptionPane.ERROR_MESSAGE);

}

/** catching the IOException & showing in a JOptionPane.ERROR_MESSAGE*/

catch (IOException io )

{

JOptionPane.showMessageDialog(null,

" Input or output problem", "Input output Problem",

JOptionPane.ERROR_MESSAGE);

}

}

/** Defining the call() function, return type is void.

* which cheking the user name & password is correct or incorrect

*if the user type the wright username & password the its shows the user login

* successful

* otherwise its shows a error message that the password or user name is incorrect

* & is the user type wrong password or user name in any order its shows a error

* message

* that "You have tryed to use some incorrect username/password combination 3 or

* times'

* & after 3 time if the user trying to put the wrong pass or name then he/she is

* bound to close the program.

* & in every time in the wrong username or password the satus bar shows waring

* whith

* the counter.

*/

private void call()

{

{ /** instantiating strname with the user inputed Username*/

strname = new String(NameField.getText());

/** instantiating strpass with the user inputed Password*/

strpass = new String(Pass.getPassword());

int g =0;

if( name.length > password.length)

g = name.length;

else

g = password.length;

for( int i=0; i<g; i++)

{

if( strname.equals(name[i]) && strpass.equals(password[i]))

{

str = "";

StatusBar.setText(str);

JOptionPane.showMessageDialog(null,

" You have successfully logged into the system", "Login Successful",

JOptionPane.INFORMATION_MESSAGE);

NameField.setText("");

Pass.setText("");

return;

}

}

if ( count >< 3 )

{

str = "Incorrect Username/Password" + "(" + count + ")";

StatusBar.setText(str);

NameField.setText("");

Pass.setText("");

count++;

}

else if( count >= 3)

{

str = "Incorrect Username/Password" + "(" + count + ")";

StatusBar.setText( str );

JOptionPane.showMessageDialog(null,

"You have tryed to use some incorrect username/password combination 3 or times ",

"Login Failed", JOptionPane.ERROR_MESSAGE);

NameField.setEnabled( false );

Pass.setEnabled( false );

Okbtn.setEnabled( false );

}

}

}

/**

* The main function contains only a call to instantiate an object of the Login class.

* @param arg array of Strings to get command line arguments

*/

public static void main(String arg[]){

Login tw = new Login();

tw.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

}

}

you need two .txt file one name is Pass.txt & other is User.txt

in Pass.txt you can add any pass & accroding to the pass you have to add a User name in the User.txt file the jusr run this fully complied code

black_codera at 2007-7-13 23:21:54 > top of Java-index,Desktop,Core GUI APIs...
# 2
ok... it's an login dialog...the question is what doing after login and getting the priviledge levelcan i make a general way to apply tous privileges to the gui.could be 5 privilege levels..watcheditadddeleteadministrator
shay_tea at 2007-7-13 23:21:54 > top of Java-index,Desktop,Core GUI APIs...
# 3

ya thats a nice question. but you know one thing to be a good programer you need to use your brain. other wise its good for nothing to copy other code.

what ever I just can give you the idea coz I don't have enough time or free energy do your work.

ok here is it:

for watch "watch" you can use a JList or make monitor with JTextArea for fully monitor the user

Make a reusable gui maintaining all the info of user & then through JOptionpane use to

edit

add

delete

administrator

with there appropiate wants.

ya that much I can do for you

bye now

black_codera at 2007-7-13 23:21:54 > top of Java-index,Desktop,Core GUI APIs...
# 4

not looking to copy others code

and dont want any example of code.

i can understand a concept.

and this is what i am looking for ,

a concept.

an idea.

the code i can write my self

you can just say..

when your privilege are applying to a GUI it's hard to make it general

AND IT'S OK TO to apply the privilege to evrey screen in the gui

example

int privilege = AppUtil.getInstance().getUserPrivilege();

if(privilege < AppUtil.PRIVILEGE_SHOW){

btnAdd.setEnable(fale);

}

is that approach OK?

case the way that i know is not that genegral

is it correct to do it that why?

giving me an example code could help as much as words

thanks shay

shay_tea at 2007-7-13 23:21:54 > top of Java-index,Desktop,Core GUI APIs...
# 5

Actually to be a good coder you don't need to show any one that you know that. Don't missunderstant me what I am trying to say that is you have belive that you can do it. belive that only think needed to be a best coder(my sir always say that to me).

actually what you write its good but some time & most of the time its really critical do that way.

What I said to you to make a resuable gui.

if you wanna make a reusable gui the best way to do it by extending the JPanel.

JPanel is really cool & pretty advanced to that kinda work.

In JPanel set all the input (Labels, Textraea, TextField......what you need in your application) then make it a different class with every value can be accesses my methid. now when ever you need.........like adduser...Show the JPanel with JOptionPane & add the values to your application ......in the same way when you need to edit a user then call the JPanel with set the user info on the panel & change the edited value accroding to the JPanel by the user......in the same way addminis.....& go on & on.

black_codera at 2007-7-13 23:21:54 > top of Java-index,Desktop,Core GUI APIs...
# 6
what about more complex thing'snot just JOptionPane what about disable enable buttons?
shay_tea at 2007-7-13 23:21:54 > top of Java-index,Desktop,Core GUI APIs...
# 7

actually enable & disable button is not complex....

you can do by calling a simple method

button.setEnable(true/false);//only this method can do it.

but if you wanna do some thing real kinda complex.

then do the reusable gui by the Window.

first extends the Window

& then set all the desire component(Better all the component you can costumize then use those) & on the perticular point show the window.

ya thats all now do it.

& don't forget to send me the code I will need it.

:)

black_codera at 2007-7-13 23:21:54 > top of Java-index,Desktop,Core GUI APIs...