Does anyone know how to go about creating field that would perform telephone number format masking, like here (___) ___-____:
http://www.smartclient.com/smartgwt/showcase/#form_masking
A better approach would be to let the user type whatever they want: "789-555-1234" or "(789) 555-1234" or "7895551234" and then when the field loses focus decide if what they typed can be a phone number. If so you can reformat it as "(789) 555-1234". There are several related questions about how to do that sort of thing with regular expressions; just be sure your regex accepts the format you're changing the user's input to, otherwise it will be really annoying to edit.
As an example, look what happens when you type ".5" into the left margin field in Microsoft's standard page setup dialog: when you tab out it changes it to "0.5".
UPDATE: Here's sample code in GWT to illustrate. For the sake of this example, assume there's an element called "phoneContainer" to put the text box in. GWT doesn't give you the full java.util.regex package, but it gives enough to do this:
private void reformatPhone(TextBox phoneField) {
String text = phoneField.getText();
text = text.replaceAll("\\D+", "");
if (text.length() == 10) {
phoneField.setText("(" + text.substring(0, 3) + ") " + text.substring(3, 6) + "-" + text.substring(6, 10));
}
}
public void onModuleLoad() {
final TextBox phoneField = new TextBox();
RootPanel.get("phoneContainer").add(phoneField);
phoneField.addBlurHandler(new BlurHandler(){
public void onBlur(BlurEvent event) {
reformatPhone(phoneField);
}
});
}
It looks like you'd want to create your own widget that extends the GWT input box and has a default value set to the mask you want. Then you handle the onKeypress event and update the field as needed (making sure to set the cursor position to the correct location).
Related
In my app, I need for values from buttons (the value is taken from an SQLite database) to be displayed in one of 13 JTextFields, and they can be in any order. How do I make it possible for the value to be displayed in the next available JTextField, if say the first one is empty?
The only thing I could think of was
if (textField.getText().isEmpty())
{
String text = String.valueOf(num);
textField.setText(textField.getText() + text);
}
What should I do next? How should I go around the else statement? Should I even use it?
Thanks in advance!
If I understand your question correctly,this should be the idea
JTextField[] fields = new JTextField[13];
firstText.setName("First Text") ;
.......
field[0] = firstText;
field[1] = SecondText;
//then add remaining textfields
for(JTextField txtField : fields) {
if(txtField.getText().equals("") ) {
// do whatever you want
}else{
// do whatever you want
}
}
For a (NetBeans) GUI in Java language below,
I want keyboard input language mode be automatically changed -
to Korean when the "K Box" gets focus (i.e., cursor is in "K Box"),
to English When the "E Box" gets focus (i.e., cursor is in "E Box")
(FYI, keyboard input language mode is changed by hitting right-ALT key).
If that is not going to be possible, I want to detect whether it is in English input mode or in Korean input mode. Then I'll make a label telling current input language mode to the users so they don't need to make mistake typing Korean in English mode.
I defined following method and using it to change keyboard language for a specific component. In the code 'ControlEnums.Languages' is my own enum type, so you may ignore it for the moment.
public static void setKeyboardLanguage(Component comp,
ControlEnums.Languages language)
{
try {
InputContext inCtx = comp.getInputContext();
Character.Subset[] subset = new Character.Subset[1];
if (language == KOREAN) {
// for Korean input mode
subset[0] = Character.UnicodeBlock.HANGUL_SYLLABLES;
} else {
// for English input mode
subset = null;
}
inCtx.setCharacterSubsets(subset);
} catch(Exception e) {
}
}
I found the above method 'setKeyboardLanguage' works pretty well. Does anyone have a better idea on my research?
Let's say we desire to have a non-ASCII character, for example, U+2082 (subscript 2).
Normally, we can display this in a swing component, such as JFrame, as Character.toString('\u2082').
Now, my issue is that I can't determine the the exact Unicode code, since the exact code is determined by the String supplied in the parameter. The parameter will always be a polyatomic ion - e.g. PO3. What my goal is to find the "3", turn that into a subscript 3 (U+2083), but also have the algorithm/method be abstracted enough that it will apply for any polyatomic ion (not just PO3, but also PO4 as well), and have it display correctly on a JFrame. I supply my method below.
private static String processName(String original)
{
char[] or = original.toCharArray();
int returned = -1;
for(int i = 0; i < or.length; i++)
{
if(Character.isDigit(or[i]))
{
returned = Integer.parseInt(Character.toString(or[i]));
or[i] = (char) (returned + 2080);
returned = -1;
}
}
return new String(or);
}
You probably are thinking, well, the code looks clean and should display correctly. However, the part (char) (returned+2080) doesn't display the symbol - it displays a blank box. I tried to fix it by setting a compatible font (GNU Unifont), but that didn't do anything. Any ideas?
2083 is a hex value, not decimal. See the unicode page for details about this character. I think the value you want is 0x2083, or 8323
I have a simple scale with a range of 0 - 100. I bound that to a label via WindowsBuilder. I want the text to display the numerical value on the scale and a % following that. How would I go about doing this?
The source code was generated by Windows Builder
protected DataBindingContext initDataBindings() {
DataBindingContext bindingContext = new DataBindingContext();
//
IObservableValue observeTextLblPercentObserveWidget = WidgetProperties.text().observe(lblPercent);
IObservableValue observeSelectionScaleObserveWidget = WidgetProperties.selection().observe(scale);
bindingContext.bindValue(observeTextLblPercentObserveWidget, observeSelectionScaleObserveWidget, null, null);
//
return bindingContext;
}
I don't know how you may do it in WindowsBuilder, but you need to add org.eclipse.core.databinding.conversion.IConverter.
Actually I think you'll need two converters (from model to text that appends "%" and one for reverse convertion to parse user input) and a validator.
I'm trying to learn something about GUI, using NetBeans6.8, starting with the GUI section in The java tutorial.
There is a simple exercise for a Celsius-Fahrenheit converter. I want that to have two TextFields, one for Celsius and one for Fahrenheit temperature; if the user types in the celsius text field he got the result "printed" in the fahrenheit text filed. and vice versa.
So, i put on both the textfields one KeyTyped event, here's the code:
private void celsiusTextKeyTyped(java.awt.event.KeyEvent evt) {
int cels = Integer.parseInt(celsiusText.getText());
int fahr = (int)(cels * 1.8 + 32);
fahrText.setText(fahr + "");
}
private void fahrTextKeyTyped(java.awt.event.KeyEvent evt) {
int fahr = Integer.parseInt(fahrText.getText());
int cels = (int)(fahr / 1.8 - 32);
celsiusText.setText(cels + "");
}
It doesn't work. If i type something in a textfield i got this exception: java.lang.NumberFormatException: For input string: ""
The code that attach the listeners:
celsiusText.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
celsiusTextKeyTyped(evt);
}
});
fahrText.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
fahrTextKeyTyped(evt);
}
});
[However, i can't modify it, it's autogenerated.]
Method .getText() returns a string not a number, if that string contains non-numeric characters (i.e. a letter, a space, nothing at all) then parseInt will throw a NumberFormatException. Since your using KeyEvent, as soon as you press say "7", the event is fired before 7 is entered into the text box. Thus the text box still only contains "", which is where the error comes from. You may wish to also listen to the keyUp event instead.
You need to enclose your code in a try catch block.
private void fahrTextKeyTyped(java.awt.event.KeyEvent evt)
{
try
{
int fahr = Integer.parseInt(fahrText.getText());
int cels = (int)(fahr / 1.8 - 32);
celsiusText.setText(cels + "");
}
catch(NumberFormatException ex)
{
//Error handling code here, i.e. informative message to the user
}
}
An alternative is you could filter out non-numbers on keydown event, see example here - http://www.javacoffeebreak.com/java107/java107.html (Creating a custom component - NumberTextField)
I suspect that what's happened is that you added these handlers with something like celsiusText.addKeyListener, yes?
The thing is, that'll give you not just the KEY_TYPED events you wanted, but also KEY_DOWN and KEY_UP. The KEY_DOWN event will happen before the text is really entered into the field, so your code firing on that will see the field as blank still. Trying to convert the empty string to a number gives you a format exception.
The easiest way to fix this is the try/catch construct other people have been posting.
You probably set action to keyDown, this mean that even occur before the key value is "added" to textbox, while You retrieve the value from it is still empty "".
There is a simple exercise for a
Celsius-Fahrenheit converter
That is a really old example. The better approach is to use a DocumentListener, not a KeyListener.