Listeners on JxSearchPanel - java

Do you have a solution to implement a listener with jXSearchPanel to be able to execute some code when users adds text on the search field?
I searched within javadoc but I can't find what I'm looking for.

I don't know anything about JXSearchPanel, but I would guess the panel uses (or extends) a regular JTextField. If this is the cause then you can just add a DocumentListener to the Document of the text field.
Read the section from the Swing tutorial on How to Write a Document Listener for a working examples.

Related

JAVA .. validating input when textfields lose focus

I'm (a beginner) creating a sign up form in netbeans. What I really want to happen is, a JOptionPane will appear when the textfield loses focus if a users input is wrong . I've been searching for the answer on the internet for the whole day but the answers I find don't satisfy me.
You need to implement a FocusListener, add it to your textField and in your focusLost implementation of the FocusListener you need to do whatever you want it to do when focus is lost.
http://docs.oracle.com/javase/tutorial/uiswing/events/focuslistener.html
I'm not experienced with NetBeans but I'm assuming you use the graphical tool to create your form. I think you will need to start coding to implement the listener.
You should be using an InputVerifier for this type of validation.

Capturing "Tab" key press by both the focused component and its container

Is there a way to propagate a key press from say a JTextField to its container's KeyListener implementation?
So in effect, the keypress would be acted upon by both the text field and the JPanel. Right now the text field is consuming the key press so is nonexistent to the JPanel underneath.
In Swing, the tab key is used to change the focus from one component to another. The article Validating Input discusses InputVerifier, which may help you do what you want.
The question is why do you want to do this? What is your actual requirement as oppose to your attempted solution. Having an event handled by two components is generally not a good idea.
In general you should not use KeyListeners. Swing was designed to use Key Bindings. However, in this case it won't help because as mentioned earlier the focus subsystem handles the tab key.
If this is the only solution to your problem, then I think you can use KeyEventPostProcessor to listen for any KeyEvent. See Global Event Listeners for more info.

What is the best way to make clickable text in java?

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.

Displaying and interacting with HTML within Java

I have a problem which I am currently trying to wrap my head around, and any advice or nods in a good direction would be greatly appreciated.
I want to display a Google Map within my Java Swing project, (the map will be a URL specified within an HTML document I think).
I also want to be able to communicate and interact with the map using JavaScript, injected via buttons in java swing, etc. So for example, I could have java buttons 'Satellite', 'Hybrid', and 'Earth' next to the map, and clicking them would perform the corresponding javascript action on the map. JavaScript methods would probably already be created within the HTML file (such as 'switchToSatelliteMap'), it would just be a matter of calling them within Java.
Thanks in advance for any help whatsoever,
tre.
I don't know whether this answers your question at all, but I think you will find these links helpful:
http://today.java.net/article/2007/10/24/building-maps-your-swing-application-jxmapviewer
http://swinglabs.org/downloads.jsp
GoogleEarth inside Java Swing
Is there a Swing component for Google Maps?
http://code.google.com/p/gameplan/source/browse/trunk/src/org/crazydays/gameplan/map/swing/JMapFrame.java?spec=svn62&r=62
I am more of a SWT fan, so I would have used a browser control as it allows me to execute javascript on the browser component. But again its a design choice.
Hope this will help.
I am not entirely clear on the scope of your issue, but you may find it helpful to attach MouseListener interfaces to your swing buttons
http://download.oracle.com/javase/7/docs/api/java/awt/event/MouseListener.html
http://download.oracle.com/javase/tutorial/uiswing/events/mouselistener.html
Implementing the mouseClicked method to run the required java script.
Additionally you could use a MouseAdapter if all you need is the mouseClicked functionality.

Linking a variable and a text box in Java

How can I link a variable and a text box in the simplest possible way?
I.e. If the user changes the text box contents the variable changes and if the program changes the variable the text in the text box changes.
N.B. I'm using the swing and awt libraries.
Thanks in advance.
put your variable in an Observable wrapper. It will tell the JTextfield about the changes.
add a DocumentListener to the field.getDocument() to tell your variable that its value should change.
If you find yourself doing lots of custom UI/Bean binding, consider JGoodies Binding.
Using Observable or a DocumentListener is ok.
For more advanced stuff, you may want to look at PropertyEditor, BeanDescriptor, BeanInfo and Customizer classes/interfaces.
I would simply add an ActionListener to the JTextField to listen to changes. When it's time to update the field, make changes to the JTextField and be prepared to ignore the events it fires off as a result.
The other option is to edit the field's Document directly, and add a DocumentListener to listen for changes. I find working with Documents to be more complex than needed, however.

Categories