Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

hello i would like to get helped with this exercice: Write a GUI program that im

ID: 3536419 • Letter: H

Question

hello i would like to get helped with this exercice:                            

Write a GUI program that implements a login window, which allows the user to enter the user name and password. When the log in button is clicked, the program will verify the login information. The reset button will reset (clear) both the user name and password.

If the login fails, the program will clear both user name and password field, and prompt the user to try again, the user will get three chances.

When the login is successful, hide the login window and open a new window for opening and saving files. The new window includes a text area, an open file button, save file button, and a close button. When the open file button is clicked, the user will be able to choose a file from computer and open it to the text area. The user may edit the opened file. After the user is done with the editing, he can click on the save button to save the content to a specified directory and file. The program will terminate when the close button is clicked,

To complete this project,

1.      You may need to check JPasswordField API for password field on login window. JPasswordField is a subclass of JTextField, which is for entering a password. Find an appropriate method from API so that each time the user types a letter, it shows a * character.

2.      The codes for selecting a file are provided. However, you may still want to study tutorials on how to user java file chooser. One of the tutorials can be found at http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html .

3.      Don%u2019t forget to handle IOException when dealing with File I/O.

The code segments for selecting a file are provided below.

/* Implement here the interface */

public voidactionPerformed(ActionEvent evt)
{
   
  
if(evt.getSource()==OpenFileButton)
    {
      
      JFileChooser chooser=
new JFileChooser();
     
int status=chooser.showOpenDialog(null);
     
if(status==JFileChooser.APPROVE_OPTION)
     {          
         File file=chooser.getSelectedFile();
          setTitle(chooser.getName(file));

           

            /*you%u2019ll continue to write codes to open a file and display it in the text area. Make sure to handle IOException.*/

         }

     }

     else if (evt.getSource()==saveFileButton)
    {
      
      JFileChooser chooser=
new JFileChooser();
     
int status=chooser.showSaveDialog(null);
     
if(status==JFileChooser.APPROVE_OPTION)
     {          
          File file=chooser.getSelectedFile();
        setTitle(chooser.getName(file));

           

            /*you%u2019ll continue to write codes to output the text from the text area to the file. Make sure to handle IOException.*/

         }

     }

   /* Implement here if close button fires the event */

}

Explanation / Answer

ANSWER class LoginDialog : public QDialog { /*! *Turns Login Dialog into a QObject */ Q_OBJECT private: /*! * A label for the username component. */ QLabel* labelUsername; /*! * A label for the password. */ QLabel* labelPassword; /*! * An editable combo box for allowing the user * to enter his username or select it from a list. */ QComboBox* comboUsername; /*! * A field to let the user enters his password. */ QLineEdit* editPassword; /*! * The standard dialog button box. */ QDialogButtonBox* buttons; /*! * A method to set up all dialog components and * initialize them. */ void setUpGUI(); public: explicit LoginDialog(QWidget *parent = 0); /*! * Sets the proposed username, that can come for instance * from a shared setting. *param username the string that represents the current username * to display */ void setUsername( QString& username ); /*! * Sets the current password to propose to the user for the login. * param password the password to fill into the dialog form */ void setPassword( QString& password ); /*! * Sets a list of allowed usernames from which the user * can pick one if he does not want to directly edit it. *param usernames a list of usernames */ void setUsernamesList( const QStringList& usernames ); signals: /*! * A signal emitted when the login is performed. * param username the username entered in the dialog * param password the password entered in the dialog * param index the number of the username selected in the combobox */ void acceptLogin( QString& username, QString& password, int& indexNumber ); public slots: /*! * A lot to adjust the emitting of the signal. */ void slotAcceptLogin(); }; #endif // LOGINDIALOG_H Implementation LoginDialog::LoginDialog(QWidget *parent) : QDialog(parent) { setUpGUI(); setWindowTitle( tr("User Login") ); setModal( true ); } void LoginDialog::setUpGUI(){ // set up the layout QGridLayout* formGridLayout = new QGridLayout( this ); // initialize the username combo box so that it is editable comboUsername = new QComboBox( this ); comboUsername->setEditable( true ); // initialize the password field so that it does not echo // characters editPassword = new QLineEdit( this ); editPassword->setEchoMode( QLineEdit::Password ); // initialize the labels labelUsername = new QLabel( this ); labelPassword = new QLabel( this ); labelUsername->setText( tr( "Username" ) ); labelUsername->setBuddy( comboUsername ); labelPassword->setText( tr( "Password" ) ); labelPassword->setBuddy( editPassword ); // initialize buttons buttons = new QDialogButtonBox( this ); buttons->addButton( QDialogButtonBox::Ok ); buttons->addButton( QDialogButtonBox::Cancel ); buttons->button( QDialogButtonBox::Ok )->setText( tr("Login") ); buttons->button( QDialogButtonBox::Cancel )->setText( tr("Abort") ); // connects slots connect( buttons->button( QDialogButtonBox::Cancel ), SIGNAL(clicked()), this, SLOT(close()) ); connect( buttons->button( QDialogButtonBox::Ok ), SIGNAL(clicked()), this, SLOT(slotAcceptLogin()) ); // place components into the dialog formGridLayout->addWidget( labelUsername, 0, 0 ); formGridLayout->addWidget( comboUsername, 0, 1 ); formGridLayout->addWidget( labelPassword, 1, 0 ); formGridLayout->addWidget( editPassword, 1, 1 ); formGridLayout->addWidget( buttons, 2, 0, 1, 2 ); setLayout( formGridLayout ); } void LoginDialog::setUsername(QString &username){ bool found = false; for( int i = 0; i count() && ! found ; i++ ) if( comboUsername->itemText( i ) == username ){ comboUsername->setCurrentIndex( i ); found = true; } if( ! found ){ int index = comboUsername->count(); qDebug() setFocus(); } void LoginDialog::setPassword(QString &password){ editPassword->setText( password ); } void LoginDialog::slotAcceptLogin(){ QString username = comboUsername->currentText(); QString password = editPassword->text(); int index = comboUsername->currentIndex(); emit acceptLogin( username, // current username password, // current password index // index in the username list ); // close this dialog close(); } void LoginDialog::setUsernamesList(const QStringList &usernames){ comboUsername->addItems( usernames ); }