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.
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 sorry this is probably way too basic to be on here, but it's a subject I've been struggling with for about a month now and I don't know where else to go (as far as I know there is no "noob overflow", lol).
I'm trying to create a class that would:
1. put an image on a window (a JFrame, JPanel or other container)
2. be able to support keyboard and mouse listeners
3. could have multiple instances in the same container
So anyway I've tried all the usual places - Google, YouTube, the official Java site (sorry forgot the URL) and of course here on Stack Overflow - but haven't been able to find anything even remotely similar to what I'm trying to do.
Of course, I've also considered the possiblity that maybe it can't be done at all. There doesn't seem to be any kind of standard "JImage" or "JGraphic" that works like JButton or JLabel, and for whatever reason graphics requires a completely different list of (extremely involved) processes and procedures. As an example, in this post: How to "really" draw images in a Java app - it took me 60+ lines of code and 2 classes to just come close. That project didn't work in the end because for some reason it would only let me create one instance (even it you created 2-4 in the main method, it would only display the last one you told it to add).
But anyway, assuming that I'm not trying to "re-invent the wheel" here and it is actually possible (in Java), does anyone have an idea as to how (or at least know of a better site to study it)? Unfortunately most of the sites I've visited tend to assume you know all the inner workings of images (I know what a pixel is but that's about it - Buffers, Rastars etc. are still beyond me). It would be absolutely outstanding if there were a site that would explain it in layman's terms, if such a site exists. Thanks in advance.
Just use a plain old JLabel.
Regarding your requirements:
put an image on a window (a JFrame, JPanel or other container).
You can give a JLabel an ImageIcon of the image of interest and it will display it. This can then be easily placed in any other container such as a JPanel or JFrame.
be able to support keyboard and mouse listeners
Any component that extends JComponent, such as a JLabel allows for use of MouseListener, MouseMotionListener and can listen for keyboard input via Key Bindings.
could have multiple instances in the same container
You can add as many as you'd like to any container. Just be cognizant and respectful of the layout managers in use.
This is for an application so I don't want a hyperlink. I first tried using a Jbutton without all of border/background stuff and then hooking up an actionListener to it but I couldn't get it to the point where I thought it looked nice. I also tried using a JLabel and hooking up a mouse listener to that but I also couldn't get it to look right.
Basically I would like a way using swing to make a button exactly like a url link in an application. What is the standard way of doing this?
but I couldn't get it to the point where I thought it looked nice
You might want to go into greater detail on just what "looked nice" means. I can see you solving this by either a JButton or a JLabel, but the key is perhaps not to look for another solution but to play with the settings of the button or the label til they look nice. If you can't find a nice solution, then post your code (an SSCCE would work best of all) and perhaps we can help you.
that isn't answer to your question but are you tried to add ButtonModel to your JButton example here
It is a rather heavy hammer to use, but SwingX has a JXHyperLink control that is probably exactly what you want. The source is at http://java.net/projects/swingx/sources/svn/content/trunk/swingx-core/src/main/java/org/jdesktop/swingx/JXHyperlink.java?rev=4027 and you can see an article about it at http://www.javalobby.org/java/forums/t18617.html.
It is old, but SwingX continues to do good things.
It's you're trying to make a desktop application which looks like HTML inside a browser, you might try using some of the richer Swing text components in a read-only mode. You could use a mouse-listener to map X/Y clicks to a particular character of text, and then cause an action to occur on that basis.
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.