11 replies to this topic
#1
Posted 23 March 2011 - 02:57 PM
Hello guyz. Im tring to create a GUI that represent a mobile phone. It has a textfield for tel number, a textfield for the message and keypad. My question is how i will make the program when mouse click the tel number field, keypad write just numbers and when click message field write characters indepents how many clicks u made (one for 'a', two for 'b'....etc.) Any idea??
|
|
|
#2
Posted 23 March 2011 - 07:13 PM
Since you can't rely on control focus (since focus is taken away from the fields once you start clicking buttons), I would declare a class variable called "fieldFocus" or something like that. You can make it whatever data type you want. Basically, you're going to use that to keep track of which text field the user most recently clicked. Then, I would create a focus listener for each text field. In each listener, you set your fieldFocus variable to whichever field had the focus. Say for instance, if you make fieldFocus an integer variable, you could set it to 0 for the telephone number field, and 1 for the message field.
Then, in each of the action listeners for your buttons, you'd have one big if statement surrounding everything that looks like this:
As for making different letters based on how many times you click a button, you'll have to add another class variable called "buttonCount" or something like that, as well as a "lastButtonPressed" variable. If the last button pressed is equal to the current button being pressed, you can add one to buttonCount, erase the last character of the message field, and add a new character to the message field according to where the buttonCount is at. You need to keep track of "wrap around" if the user presses it more than 3 times (or 4 or 5, depending on which button it is.) If the last button pressed is different from the current one being pressed, reset buttonCount, and insert the first letter of that button to the field.
If you want to add timing to it as well, so that if your user waits for a moment and then presses the button again, and your program starts a new letter, you'll have to use java.lang.System.currentTimeMillis() to get the current time in milliseconds each time the button is pressed, and store that in yet another class variable. Each time your button is pressed, subtract the previous time from the current time to see if the time has gone beyond some threshold that you decide upon, and if so, reset buttonCount and start a new letter.
Does that answer everything?
Then, in each of the action listeners for your buttons, you'd have one big if statement surrounding everything that looks like this:
if (fieldFocus == 0) {
// Add a character to the telephone number field
} else {
// Add a character to the message field
}
As for making different letters based on how many times you click a button, you'll have to add another class variable called "buttonCount" or something like that, as well as a "lastButtonPressed" variable. If the last button pressed is equal to the current button being pressed, you can add one to buttonCount, erase the last character of the message field, and add a new character to the message field according to where the buttonCount is at. You need to keep track of "wrap around" if the user presses it more than 3 times (or 4 or 5, depending on which button it is.) If the last button pressed is different from the current one being pressed, reset buttonCount, and insert the first letter of that button to the field.
If you want to add timing to it as well, so that if your user waits for a moment and then presses the button again, and your program starts a new letter, you'll have to use java.lang.System.currentTimeMillis() to get the current time in milliseconds each time the button is pressed, and store that in yet another class variable. Each time your button is pressed, subtract the previous time from the current time to see if the time has gone beyond some threshold that you decide upon, and if so, reset buttonCount and start a new letter.
Does that answer everything?
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
#3
Posted 24 March 2011 - 02:44 AM
Hello,
I change my program lil bit. Removed the mouseListener and now when program starts only message field is appearing, so inside actionListener im telling,
and when user, click 'done' button, then number field becoming visible and message invisible. Is working fine but only with keys who have just two characters (0 or +). I thought create other classes for button more than two characters. For example:
How is sounds???
P.S. java.lang.System.currentTimeMillis() looks like very useful thank you, i think i will try it. Do you have any link for more info?
thank you
I change my program lil bit. Removed the mouseListener and now when program starts only message field is appearing, so inside actionListener im telling,
if(e.getSource()==Bt_0){
if(num.isVisible()==true){
num.append("0");
}
else{
scr.append("+");
}
}
and when user, click 'done' button, then number field becoming visible and message invisible. Is working fine but only with keys who have just two characters (0 or +). I thought create other classes for button more than two characters. For example:
if(e.getSource()==Bt_2){
if(num.isVisible()==true){
num.append("2");
}
else{ GO to the corrisponding class - calculate what is the letter - and return the letter.
}
}
How is sounds???
P.S. java.lang.System.currentTimeMillis() looks like very useful thank you, i think i will try it. Do you have any link for more info?
thank you
#4
Posted 24 March 2011 - 05:42 AM
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
#5
Posted 24 March 2011 - 05:50 AM
how my idea for my program look like to you? Btw any example for currentTimeMillis() to fit with what i want to do? I mean when the '2' key will write 'a' if clicked ones, when 'b' when 'c' etc..
Thank you
Thank you
#6
Posted 24 March 2011 - 05:59 AM
In your class variables section, declare a variable to hold the last time the user clicked a button:
Hope that helps.
private long lastButtonPressTime = 0;You'll also need a constant declared for your threshold. I'm using 1 second here:
private const long timeThreshold = 1000;In your action listener for your button, you'll need something like this code:
long currentTime = java.util.System.currentTimeMillis();
...
// your code for determining which button was pressed goes here
...
// Here's the timing code:
if (lastButtonPressTime == 0 || (currentTime - lastButtonPressTime) > timeThreshold) {
// This is a new letter. Reset buttonCount and start a new letter.
} else {
// This is a continuation of the previous letter. Remove the letter, increment buttonCount, and add the updated letter.
}
// Don't forget to update the last time the button was pressed
lastButtonPressTime = currentTime;
Hope that helps.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
#7
Posted 24 March 2011 - 06:22 AM
oww, nevermind :s
Edited by wim DC, 24 March 2011 - 07:54 AM.
#8
Posted 25 March 2011 - 05:31 AM
Guyz, thanks for your help, im working this way, but something going wrong. If user doesn't write in number field, then calling the method from other class and passing as parameters, button's letters. Here is the other class:
Problems:
public class keyButton {
private long lastBtPress = 0;
private long currentTime;
int count = 0;
char currLetter;
String letter;
char[] letterArray;
public String findLetter(String letters){
letterArray = letters.toCharArray();
currentTime = System.currentTimeMillis();
if((currentTime - lastBtPress)>800){
currLetter = letterArray[count];
}
else{
if(count<3){
currLetter = letterArray[count++];
}
else{
count=0;
currLetter = letterArray[count];
}
}
letter = Character.toString(currLetter);
lastBtPress = currentTime;
return letter;
}
}
Problems:
- If i click 2, print out 'a' if i do it again (in time less than 800) again prints out 'a'
- How to replace the previous letter with new one?
- I have exception, when reach the last letter
#9
Posted 25 March 2011 - 06:25 AM
You need to increment the value of count each time, regardless of whether the button press is a repeat or a new letter. Try this:
public class keyButton {
private long lastBtPress = 0;
private long currentTime;
int count = 0;
char currLetter;
String letter;
char[] letterArray;
public String findLetter(String letters){
letterArray = letters.toCharArray();
currentTime = System.currentTimeMillis();
if((currentTime - lastBtPress)>800){
// Time has expired. This is a new letter.
count = 0; // Need to reset here
currLetter = letterArray[count];
}
else{
if(count<3){
// Count is less than 3, we haven't wrapped around yet
currLetter = letterArray[count]; // No need to increment here
}
else{
// Count is 3 or greater, we need to wrap around
count=0;
currLetter = letterArray[count];
}
}
letter = Character.toString(currLetter);
lastBtPress = currentTime;
// Increment count before returning
count++;
return letter;
}
}
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
#10
Posted 25 March 2011 - 06:34 AM
Wow..respect to you gregwarner...fixed the 2/3 problems.. Doesn't overwrite the 'a'...just write a and then b
#11
Posted 25 March 2011 - 07:17 AM
As far as replacing the last letter with the new one, how are you storing the message in progress?
For example, if it's a text box, you could replace the last letter with a new one like this:
For example, if it's a text box, you could replace the last letter with a new one like this:
// Remove the last character TextBox1.Text = TextBox1.Text.Substring(0, TextBox1.TextLength - 1); // Add the new character TextBox1.Text = TextBox1.Text + letter;
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
#12
Posted 25 March 2011 - 08:20 AM
You are the best ;) ... big problems solved...now i have to find solutions for smaller probs :P
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









