Differentiate between typing and pasting in JTextField - java

i've been looking around and haven't been able to find any solution to this problem: i have a JTextField and i want to do some things when the user paste something in there, i've found this: What event to use when pasting something in a JTextField?
which works ok, except that i want only to do things when the user paste something, not when it writes on the text field, i've though of saving the previous value of it and compare it with the new, and if it was empty and now is not, do things, but this won't work since it will enter in that condition when the user types the first letter in the text field.
If anyone knows how to do it whit the documentListener or whit any other listener it would be of grate help.
Update: since various people has asked, the reason i want to do this is because the text will come from a bar code reader or some similar device.

except that i want only to do things when the user paste something
Why should pasted text be treated any different than typed text? Sounds like a design issue. If you specify a better reason/requirement for doiong this we might be able to come up with a better solution.
i want to do some things when the user paste something in there
You might be able to override the paste() method of the JTextField. Just override the method to invoke super.paste() and then add your custom code.
how to do it whit the documentListener
Maybe you would consider a "paste" to mean more than one character is added at a time. In which case you just test the length of the String that is added to the Document.

I was able to fix my problem by configuring my bar code scanner and making it send a "new line" after each reading, and executing my code every time this happens with the actionPerformed of the JTextField. Thanks to everybody who tried to help.

Related

JComboBox: arrow-button should be shown at any time

If a JComboBox is not selected, the arrow-button on the right is not shown. This leads to the fact that a combo-box cannot be distinguished from a normal textfield.
The question is now: how is it possible to show the arrow-button permanently? I alrady came across BasicComboBoxUI.createArrowButton() etc., but I did not find out the mechanism of hiding / showing the arrow-button.
Can anyoune give me a hint how to show the arrow-button permanently ?
Can you add below code as first line in your main method.
Toolkit.getDefaultToolkit().setDesktopProperty("win.xpstyle.themeActive",Boolean.FALSE);
and then check your program.
sorry for the inconvenience. Everything is clear now. In have been told that we use a special framework which changes the behaviour. This information would have been useful if given to me earlier... :-(

How do you append both pre-established text and user responses into a text area in Java?

I will do my best to explain-- I am trying to make a choose-your-own-adventure type game while using a TextField and a TextArea, where what is written in the TextField is appended into the TextArea (this I know how to do via ActionListener).
However, I need to have the TextArea start with a pre-written 'intro', which asks the user at the end if they want to continue or not. Therefore, I need it to be able to scan the user's response ('yes' or 'no') and choose the appropriate selection of pre-written text to follow.
I don't want to overwrite what is already in the TextArea, I want to add to it. I suppose what I'm confused about it how I'm supposed to lay out the entire file so that it functions properly, because the different choices for the adventure span different methods. Having
"String text = textField.getText();" only within the actionPerformed method means I can't use 'text' elsewhere, but moving that line up with my other variables tells me it can't reference the field before it's defined.
I am fairly new to Java and am working on this as a project for a non-programming school course. I've been through many iterations thus far and this is what seems to be my final attempt, as I've remade it repeatedly and don't have much time left. :(
Your questions/comments and my attempts to answer:
I am trying to make a choose-your-own-adventure type game while using a TextField and a TextArea, where what is written in the TextField is appended into the TextArea (this I know how to do via ActionListener).
As per my comment, be sure to create a Swing GUI which would use a JTextField and a JTextArea. You would then add your java.awt.event.ActionListener to the JTextArea, and the ActionListener would respond whenever the user pressed <ENTER> within the JTextField.
However, I need to have the TextArea start with a pre-written 'intro', which asks the user at the end if they want to continue or not. Therefore, I need it to be able to scan the user's response ('yes' or 'no') and choose the appropriate selection of pre-written text to follow.
This can be done easily, but sounds as if you may be trying to shoe-horn a linear console type program into a GUI. If so, consider reconsidering your program design since what works best for one often doesn't work well for another. If you do re-write, then you should consider redoing most including your program flow, but excepting perhaps the "model" portion of your previous program, the "business logic" that underlies everything.
I don't want to overwrite what is already in the TextArea, I want to add to it. I suppose what I'm confused about it how I'm supposed to lay out the entire file so that it functions properly, because the different choices for the adventure span different methods. Having "String text = textField.getText();" only within the actionPerformed method means I can't use 'text' elsewhere, but moving that line up with my other variables tells me it can't reference the field before it's defined.
Again as per comments a JTextArea has an append(String text) method that will add new text to existing text that is already displayed in your JText Area. So on that note, your ActionListener's actionPerformed method could be very simple and look something like:
public void actionPerformed(ActionEvent e) {
String text = textField.getText();
textArea.append(text):
}
Although you may need to add line feeds, "\n" either before and/or after the text you are going to append.

Adding text input to a blackberrycanvas

The BlackBerryCanvas documentation says:
This class extends the functionality of the Canvas class to include full touch support and featured text input support.
I have extended BlackBerryCanvas, but am having trouble adding any text input.
It's not that I know what to do but cannot get it work -- I simply do not know how to add a text input box or field.
EDIT: Or have I misunderstood and this is not possible? From reading around, it seems as though it is, but I'm starting to wonder why it's so hard to find anything on it.
EDIT2: I'm think maybe it's something to do with the BlackBerryTextBox?
EDIT3:
Applications using this class can call the #setInputHelper method to get the text input support.
might also be something. It hasn't given me quite enough clues to be able to do it myself though I'm afraid.
Thanks.
If you don't particularly need to use the Canvas hierarchy, I suggest you use the more commonly used Field hierarchy.
That means you should create an application and start with a Screen. An easy concrete implementation of Screen is the FullScreen. To get the text input you are seeking, add an AutoTextEditField to the screen.

java: fully controlled input on JTextField

I want to have a JTextField (or another text-like input which supports focus and a cursor -- or maybe I should code my own?) where I want to have full control over the immediate input. I.e. I want to ignore most inputs and handle the rest specifically. It seems that JFormattedTextField is not what I want because it does the verification not on the immediate input but when the focus is lost.
I tried to add a key listener but it's a bit non-obvious how I should use it. It seems I can catch up events like cursor changes (via keys) or copy/paste (via key-shortcuts) in keyPressed and I can catch all normal key input in keyTyped. I am not sure how I can catch cursor changes or copy/paste made via mouse (for some reason, right click does not work anyway, so copy/paste via mouse in the context menu seems not possible but I am not sure if that might be a different issue or if it should work and just does not for some reason).
I basically want something like the following handler:
interface InputHandler {
void addStringAt(int pos, String s);
void deleteSubString(int pos, int len);
void setFocusTo(int pos);
}
And it should be absolutely impossible for the user to get any stuff onto the textbox which are not going through this handler.
Right now, it seems like I have to take care about a lot of specific cases (like on some architectures, there may be some context menu where they could copy/paste or input something, handle all mouse events manually, etc).
Or the other way, which also involves a lot of special handlings: To recode such input field myself. This still seems saver because I can know for sure that there are no tricky methods how the user could bypass the handler (and I really must know that for sure). Btw., how would I do this? I thought of extending a JLabel. How can I extend it so that it can get a focus?
I wonder how you would implement something like this.
It sounds like you need a DocumentFilter. A couple of existing questions that will help with this:
Cut and Paste in JTextArea
Help on writing your own javax.swing.text.Document

Returning a JTextField to editing after a warning dialog

In my application I have a JTextField that will only allow the user to fill it with input formatted in a certain way. I have a method that checks if input into the field is valid and if not it displays a dialog. After this occurs I would like the focus to return to the text field so that the user can correct there mistake and type in the appropriately formatted input. I tried to do this by having the JTextField call requestFocus() on itself but this seems to only half work. The JTextField displays the flashing cursor but I cannot actually type in any text until I click on the text field again. Is there another method I need to call? I could not find anything else in the documentation.
It's not exactly the solution you asked for, but you might want to look at javax.swing.InputVerifier. This object allows you to verify the input to a field and prevent taking focus away from it if the format isn't right. I don't think it would allow you to put up a dialog explaining the error (though it might - try it) but it would certainly allow you to put a message in a 'message area'.
What you do is set an InputVerifier on the JComponent, have it test the contents for validity; if it isn't right then the JComponent won't release focus (and can write an error message).

Categories