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
Related
I'm making a custom view that can be annotated with typical paint tools like draw, shape dropping, or writing text, and I'm stuck on how to enable text input without resorting to creating new views on touch. I'd like to be able to handle all of the annotation in this one custom view class.
My idea is to extend EditText, since it seems to have all of the functionality I need for creating editable text onscreen. Currently, I override onTouchEvent(MotionEvent event) to annotate whatever drawable has been loaded into my custom EditText called AnnotationView. I can call super.onTouchEvent(event) when the text tool is selected, and the soft keyboard displays. Entered text is displayed on screen, and I can edit it. However, at the moment I can only enter text in one place, seeing as how this currently is just an EditText with added drawing capabilities. Any other overridden methods (onSizeChanged, onDraw, performClick) start with a call to their super implementation.
Has anyone here attempted something like this before? I need to allow the user to start editing text at their touch coordinates, but I have no idea what methods I'd need to override or create to do that. It seems like this would be a good way to avoid requiring more views be created from the Activity using AnnotationView, but I'm not sure how feasible this approach really is.
Issue can be seen here:
Image of AnnotationView being used in a sample app
To explain why I'm trying to avoid creating new views to write text in - I'd like to release this as a library, so I'm trying to require the least amount of hassle for someone using this view. Ideally, a developer could just place this view and then choose whatever tools I've implemented via their own UI. If I need to create views on top of this, I'll need to require users to roll their own text editing implementation via some kind of listener... maybe? If I'm missing a simpler way of doing this, please let me know! I've seen similar questions asked here, but it seems like a definitive good practice for capturing user input and displaying it on canvas would be useful for a lot of beginners such as myself.
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.
I'm working on something in NetBeans, and I want to create an actionListener in a jTextPane for when the user presses Enter. However, I also need to input a String array (from a different subroutine within the source code) into the listener. But in NetBeans, events are generated automatically and I'm not allowed to edit this apparently extremely sensitive code. So, then, I tried typing up my own event thing (sorry about my terminology; I'm very new to GUI programming):
private void jTextPaneEnterPressed(KeyEvent evt, String[] stringArray)
{
int key = evt.getKeyCode();
if (key == KeyEvent.VK_ENTER)
{
// do something when the user presses enter that involves the program knowing what stringArray is
}
}
But when I run the program, pressing Enter in the text pane does nothing. I understand that this is because there is no actionListener associated with jTextPaneEnterPressed, but NetBeans gives no option for such customized code.
So what I want to know is either how I can pass in my own parameters when NetBeans creates an event handler, or how I can write my own actionListener alongside this actionPerformed block.
(And for the record, this is not an import problem)
I have tried looking this up, but have found nothing specific, as all other similar issues are not relevant to NB. Any help would be greatly appreciated.
*EDIT: This may seem like a trivial problem to most, but I'm open to any actual answers that tell me how to get done what I am trying to do, though I would prefer to stick with NetBeans. All I need is for the action listener to know that this string array exists, because the program needs to deal with that array when the user presses Enter. I'm sorry I can't give any specific context, but it's too much to get into.
I see there are methods already built for adding and removing mouse listeners, I suppose I could add them and remove them as needed. I just wasn't sure if there was a better way.
Having my JLabel respond to all mouse events over it but sometimes do nothing isn't quite what I'm looking for, it would be nice if my Labels had the mouse listener only when I needed it to. Maybe this whole idea is just a weird approach.
You register the Listener once and for all; you don't turn it off and on.
You can code logic inside to only take action under certain conditions of your choosing. As long as you make the information available that's needed for a decision your implementation should be fine.
Here's a very specific coding question:
I've recently been asked to maintain some old-ish Java Swing GUI code at work and ran into this problem:
I've attached my own subclass of InputVerifier called MyFilenameVerifier to a JTextField (but it may as well be any JComponent for these purposes). I've overridden the verify() method such that it calls super.verify(input) (where input is the JComponent parameter to verify()). If super.verify(input) comes back false, I do:
input.setBorder(BorderFactory.createLineBorder(Color.RED));
This is a convention used throughout the UI of this application that started long before me, so I don't have a lot of choice as far as using other ways to get the users attention (wish I did). This is just the way it works.
Problem is, once the user goes back and types something valid into the text field, I need a way to set it back to default border (instead of just saying set it to Color.GRAY or whatever, which is a different color from its original border). I need a way to say, "remove the extra decoration and go back to normal" or just set the border to its default, in other words.
Couldn't you just call input.getBorder() and cache it somewhere before setting the border to red?
Or without caching anything, you could tell the JComponent to update its UI back to the look and feel's defaults via component.updateUI. That should make the component reset its colors, borders, fonts, etc to match the original settings.
input.getBorder()
Wouldn't it be awesome if no one ever saw this and I got away free without the ass-beating I deserve for asking this question?
Not sure how your system is build, but I think you can store the original border before changing it. So you can change it back later
// assuming the Border was not null before
if (!super.verify(input)) {
original = input.getBorder();
input.setBorder(...);
} else {
if (original != null) {
input.setBorder(original);
original = null; // not needed
}
}
You need to preserve the existing border when you change it.
One way to do this is to use the methods putClientProperty() and getClientProperty(), which you'll find documented in the API.
Another possibility, if there are only a few input widgets you need this for is to subclass, e.g. JTextField, add setBorderOverride() and modify getBorder() to return "overriddingBorder" if it is not null.
Then you just use setBorderOverride(redBorder) to make it red and setBorderOverride(null) to clear it.
This of course depends on the painting to use getBorder(), which it may or may not do, and which may be implementation specific.
Incidentally, you only need a single static reference to the border-- it's the selfsame border instance used by all JTextFields.