Permalink
Browse files

Welcome Card substantially complete...

Added a couple twists I've been wanting to find out how to implement,
including clearing text fields when clicked on (the first time only)
and adding guards to only enable "enter" button when username and
password are filled in.
  • Loading branch information...
jasonshaffner committed Mar 2, 2012
1 parent f02325d commit dcb442bf12be1f22e6908e16e427483c543e39a9
Showing with 61 additions and 27 deletions.
  1. +1 −0 src/modules/applet_framework/ETApplet.java
  2. +60 −27 src/modules/applet_framework/WelcomeCard.java
@@ -27,6 +27,7 @@
static String selectedTonic;
static int selectedInstrument;
+ static String user;
public void init() {
System.out.println("Initializing...");
@@ -1,35 +1,38 @@
// Ear training Applet Framework -- Activity Cards
-// Jacob Peck
+// Jacob Peck/Jason Shaffner
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
+import javax.swing.event.*;
+import javax.swing.text.*;
-public class WelcomeCard extends ActivityCard implements ActionListener {
+public class WelcomeCard extends ActivityCard implements ActionListener, FocusListener {
- JLabel header = new JLabel("<html><center><h1>Welcome to the Ear Training Suite</h1></center></html>");
- JLabel prompter = new JLabel("<html><center><h3>Sign in to get started</h3></center></html>");
- JTextField userField = new JTextField("Enter your username",15);
- JPasswordField passwordField = new JPasswordField("Enter your password",15);
+ JLabel header = new JLabel("<html><center><h1>Welcome to the Ear Training Suite<br></h1><h3>Sign in to get started<br></h3></center></html>");
+ JTextField userField = new JTextField("username",15);
+ JPasswordField passwordField = new JPasswordField("password",15);
JButton enterButton = new JButton("Enter");
+ JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JPanel centerPanel = new JPanel(new BorderLayout());
JPanel fieldPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
- String user;
char[] password;
+ boolean userPrev;
+ boolean passwordPrev;
//layout not set in stone... i'll play with it if i have time
public void initializeThisCard() {
System.out.println("Initializing pane...");
- add(header,BorderLayout.NORTH);
- fieldPanel.add(prompter);
+ topPanel.add(header);
+ add(topPanel,BorderLayout.NORTH);
fieldPanel.add(userField);
fieldPanel.add(passwordField);
centerPanel.add(fieldPanel,BorderLayout.CENTER);
add(centerPanel,BorderLayout.CENTER);
buttonPanel.add(enterButton);
add(buttonPanel,BorderLayout.SOUTH);
- // enterButton.setEnabled(false);
+ enterButton.setEnabled(false);
addActionListeners();
}
@@ -39,28 +42,58 @@ public WelcomeCard() {
}
public void addActionListeners() {
- // userField.addActionListener(this);
- // passwordField.addActionListener(this);
enterButton.addActionListener(this);
+ enterButton.addFocusListener(this);
+ enterButton.requestFocus();
+ userField.addFocusListener(this);
+ passwordField.addFocusListener(this);
+ JButtonStateController enterButtonStateController = new JButtonStateController(userField.getDocument(),passwordField.getDocument());
+ userField.getDocument().addDocumentListener(enterButtonStateController);
+ passwordField.getDocument().addDocumentListener(enterButtonStateController);
}
- //will need to implement this guard correctly at some point...
- //right now only works if you press enter while in each field
- //so i just commented it out until i figure it out
public void actionPerformed(ActionEvent e) {
- /* if (e.getSource() == userField) {
- user = userField.getText();
- if (user != null && password != null) enterButton.setEnabled(true);
- } else if (e.getSource() == passwordField) {
- password = passwordField.getPassword();
- if (user != null && password != null) enterButton.setEnabled(true);
- } else if (e.getSource() == enterButton) {
- if (user == null || password == null) prompter.setText("<html><h3><center>You must enter a username and password</center></h3></html>");
- else {
- */ user = userField.getText();
+ if (e.getSource() == enterButton) {
+ ETApplet.user = userField.getText();
password = passwordField.getPassword();
ETApplet.switchToCard("options");
- // }
- // }
+ }
}
+
+ //clears preset text from username and password fields when cursor enters said field
+ public void focusGained(FocusEvent e) {
+ if (userField.isFocusOwner() && !userPrev) {
+ userField.setText("");
+ userPrev = true;
+ } else if (passwordField.isFocusOwner() && !passwordPrev) {
+ passwordField.setText("");
+ passwordPrev = true;
+ }
+ }
+ //not used
+ public void focusLost(FocusEvent e) {}
+
+ //controls state of enter button, enabling only when both username and password are filled out
+ class JButtonStateController implements DocumentListener {
+ boolean userFilled;
+ boolean passwordFilled;
+ Document userDoc;
+ Document passwordDoc;
+
+ JButtonStateController(Document userDoc, Document passwordDoc) {
+ this.userDoc = userDoc;
+ this.passwordDoc = passwordDoc;
+ }
+
+ public void disableIfEmpty(DocumentEvent e) {
+ if (e.getDocument() == userDoc) userFilled = e.getDocument().getLength() > 0;
+ if (e.getDocument() == passwordDoc) passwordFilled = e.getDocument().getLength() > 0;
+ enterButton.setEnabled(userFilled && passwordFilled);
+ }
+
+ public void changedUpdate(DocumentEvent e) { disableIfEmpty(e); }
+ public void insertUpdate(DocumentEvent e) { disableIfEmpty(e); }
+ public void removeUpdate(DocumentEvent e) { disableIfEmpty(e); }
+
+ }
}

0 comments on commit dcb442b

Please sign in to comment.