ActionEvent textfield checking - java

I have cashform with atttributes pin,sendername,receivername,senderphone,amount and another form accountfrom with attributes pin,sendername receivername,senderphone,amount,bankname,account number..
both form have send Command
Now, I want to check whether the textfields are empty when the user clicks send button...
I tried it in this way
if ( ae.getCommand() == send && ae.getSource()==cashpayform){
cashcheck();
}
if ( ae.getCommand() == send && ae.getSource()==accpayform){
acccheck();
}
but its not working can anyone help me
thanx

When a command triggers an event the source of the event is the Command not the button so you can't physically make a distinction between a command triggered from a button press and a command triggered from a menu.
I suggest you use two different commands if you need to make a distinction between the source of the commands, they can have the same name and even ID if you make pointer comparisons.

Don't compare strings using ==, instead use..
send.equals(ae.getCommand())

Related

How to check in java if user is clicking save button unnecessarily and there is nothing to save?

In my piece of code there is one input box so data will be fetched in that box on drop down selection. And if user do not want to insert/update/delete that data and click on save button unnecessarily then i have to show an error message that: "There is nothing to save..You pressed the save button unnecessary."
Currently , i am checking if the data in input box is equals to the database value then show that error but its not working.
if (inputVal1.equals(dbVal1.getValue()) && inputVal2.equals(dbVal2.getValue())) {
addPageError(T_NOTHING_TO_SAVE);
}
Please suggest how to handle this validation in java.
Store the data in a separate variable and simply compare it to the current values in the input box. If you do this, you could even detect if the values were changed, but then changed back to the original value. Apart from this you could also even deactivate the button and compare in an edit-event of the input box if the content has really changed and enable the button accordingly.

Deselect JToggleButton if file exists

as I mentioned in a post before, I'm porting my program to Java, to make it available for Mac OS and Linux users.
At the start of the program, I'd like to check if adb is installed to the system using this code:
private void checkADBExists()
// Checks if adb binaries exist and sets jTogglebutton1 correspondingly...
{
File adb = new File("/usr/bin/adb");
if (!adb.exists())
{
jToggleButton1.isSelected();
} else {
jToggleButton1.isSelected()= false;
}
}
Here's my problem:
If the file doesn't exist, the JToggleButton isn't selected, even though it should be and I get an error deselecting it.
Any help is much appreciated.
Thanks in advance,
Beats
Many of Swing's core components follow a simple getter/setter pattern.
That is, you can "get" a property value and "set" a property value (note, not all getters have a corresponding setter though).
In the case of a boolean property, the convention is to use "is" instead of "get", it just rolls off the tongue better.
So, in your case, all you are doing is getting the value if the selected property, not really what you want to do.
Instead use jToggleButton1.setSelected(true) or jToggleButton1.setSelected(false) based on your needs
You might like to take a look at How to Use Buttons, Check Boxes, and Radio Buttons for some more details
JToggleButton().isSelected() return a value not a variable. By JToggleButton().isSelected() = false, you are trying to assign a value to a value, it doesn't make sense, much like writing a statement 2 = 2;. use JToggleButton.setSelected(true) to set the toggle button as selected and JToggleButton.setSelected(false) to deselect.

How to bind properties of two UI objects in Java?

I have a simple Java application with textfields and buttons. I am looking for the best and quick way to bind the state of one JTextField to the state of one JButton. I'm using Eclipse, so I don't need any tricks of Netbeans IDE.
Suppose a user needs to input a value into a textfield in order to be able to send a request. The button should be enabled only if the value of the textfield is not empty and consists at least of 3 symbols. If the user deletes the input, the button becomes disabled.
I come from the Flex-world. Such a task can be solved there very easy. One should just write something like this:
<mx:Button enabled = "{myTextField.text.length >= 3}" />
Is there such an opportunity in Java? How is it called? I hope, I don't need to write event listeners for each pair of logicaly connected UI elements, do I?
I would do it with a DocumentListener on the JTextField. Every time the Document changes, you check the state of your button, like button.setEnabled(textField.getText().length > 3)

Override delete key on Android?

I mostly fixed the problem with these lines in dispatchKeyEvent:
byte[] cmdLeft = { (byte) 27, (byte) '[', (byte) 'D' };
byte[] cmdErase = { (byte) 27, (byte) '[', (byte) 'P' };
mSession.appendToEmulator(cmdLeft, 0, cmdLeft.length);
mSession.appendToEmulator(cmdErase, 0, cmdErase.length);
The only problem now is that if I select the editText and hit delete then one character is deleted but two appear to be on screen. so if I write enable and hit delete it will change to enab but what would actually be sent is enabl
I overrode dispatchKeyEvent, and it kind of works. If the editText is selected, the terminal deletes characters over serial now, so that is a good step. However the main problem still exists that if the terminal is selected itself, weird little boxes are written to the screen instead of deleting a character. Well one is written, and if I keep pressing delete it stays at that one box, but next time I type the amount of deletes I pressed comes up as boxes. It's very odd...
It's like it is just overridden for the edittext and not for the terminal.
Weird little boxes in all their glory:
public boolean dispatchKeyEvent(KeyEvent event) {
if (event != null && event.getAction() == KeyEvent.ACTION_UP) {
return false;
}
if (event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
try {
sendOverSerial("\b".getBytes("UTF-8"));
}
catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return super.dispatchKeyEvent(event);
};
I am connecting to a terminal emulator using a library in android, this connects to a serial device (a switch) and shows me sent/received data. I send data over the connection via a text box below the terminal or by typing in the terminal itself and hitting enter on the keyboard in both cases. It will only ever be a soft keyboard that is used. If I send an incorrect string I am in an unrecoverable state due to not having a delete key implementation. Backspace in my editTxt works fine, I just want it to work when the terminal is highlighted and I am writing in that.
At the moment if I press delete a little odd box character comes up and nothing else happens, I get an exception in the log some times(http://i.imgur.com/wMRaLPX.png). What I want to know is how to I go about changing the delete keys functionality so that when I press it I can send a delete character like this but also retain the ability to delete characters in the edittext box etc:
sendOverSerial("\b".getBytes("UTF-8"))
This sends a correct back space, I just need to incorporate it.
But the soft-keyboard doesn't seem to register key presses? i keep getting a keycode of 0 and only enter will work.
I am currently trying out https://stackoverflow.com/questions/4...62035_11377462, but any other suggestions would be great, as about 10 suggestions haven't worked so far. My backspace wouldn't be associated with an editText, but a terminal View. I can't even detect the delete key being pressed.
It looks like the terminal control you are using must be consuming the KEYCODE_DEL instead of letting it propagate to the window and it must be sending a different char to the remote end instead of \b. So when your edit text is focused your dispatchKeyEvent is handling the press - but you don't see it when the terminal has focus. Have you confirmed that the even handler is firing via debugger when the terminal has focus? You didn't say which library you are using for the terminal, but I'd look at that and see if you can set a key handler or something.
I don't have any experience with Android, and I'll also admit I've never tried to implement a delete/backspace key bind. However, if I were trying to do this, and I didn't know a good standard implementation I can think of a workaround that would probably function just fine. Make a key bind to delete with an associated action listener. Make the action listener getText() out of your text field and store it as a String. Substring that string to include everything but the last character. Then use setText() for the text field with the new string. Kind of a manual way of doing it, but it would definitely work.
I recommend capturing the full string and send it all at once, when the user presses Send, like a chat program.
The solution was to move the method that wrote to the screen to another class, then everything worked fine.

libGDX scene2d TextFieldListener does not receive DELETE key on Android

I cannot get the android "delete" key to register in my TextField (scene2d ui element in libgdx) listener. Here is my code to define the text field:
nameTextfield = new TextField("", skin);
nameTextfield.setMessageText("Some Text");
uiStage.addActor(nameTextfield);
I tried this listener just to decode the keycode for the DELETE key:
nameTextfield.setTextFieldListener(new TextFieldListener() {
public void keyTyped (TextField textField, char key) {
textField.setText(String.valueOf(Integer.valueOf(key)));
}
});
Although it gives code for almost for all buttons, it doesn't even react on DELETE button.
I tested this on a Nexus 7.
From the TextField.java source it looks like the "DELETE" (and "BACKSPACE", and "TAB" and a couple other keys ) are handled specially by the TextField. These keys are never forwarded to any listener.
The built-in handler should do "the right thing" (trimming characters off the string contents).
Is delete not behaving correctly for your case in some way that led you to try to decode it?
Well the DELETE button should be implemented differently.
I suggest trying to verify if the pressed key is the DELETE button. If it is, you just do textField.getText(), trim the last letter off it, and set the new text with setText.
I'm sure there's a much more elegant way to do this, but it's the only workaround I can think of. After all, DELETE isn't really a char which you can throw inside setText. Is it? :/
LATER EDIT:
Print the key variable inside your listener, put a breakpoint there, and see what value is assigned to it.
Then also print (or check the javadoc) KeyEvent.KEYCODE_DEL (documentation here) to see what value this one takes.
the best way to solve this is the following:
You will need to listen to inputs from another listner, for example the same screen.
MainMenuScreen implements Screen, InputProcessor
then you will need to create a multiplexer to let inputs been listen from both the stage and the listner.
multiplexer = new InputMultiplexer();
then add the two listners:
multiplexer.addProcessor(this);
multiplexer.addProcessor(stage);
Now you will have to simply delete the field from here:
#Override
public boolean keyDown(int keycode) {
Gdx.app.log("Debug:", "keydown : "+keycode);
//DO SMOTHING LIKE
// if(keycode==...) deleteTextField();
return true;
}
Let me know if you have questions about this solution.
Worked great for me.
This issue has been solved by the latest nightly version of libgdx, the issue is known and discussed in the following link:
nexus button

Categories

Resources