How do I lose focus on a JComboBox? - java

I have a JComboBox with an key listener.
When I hit <enter>, I fire off some action, and then I need to to lose focus on the JComboBox!
To focus on it, I can do JComboBoxObject.grabFocus();
But doing transferFocus() to get the focus to a next element (I don't care WHERE the focus goes, just away from combo box) does NOT work.
Doing grabFocus() from another combo box works, but seems like a pretty annoying hack to me. Is there a better solution?

I can suggest you to first use the
.getNextFocusableComponent()
and then use the
.requestFocusInWindow()
that means Implementing it like this,
JComboBox.getNextFocusableComponent().requestFocusInWindow();
One important note is that .getNextFocusableComponent() has become obsolete but it can work really better, you can use it but If you have any other solution, I would prefer not using this.

Updated: Starting from this two-combo example, adding either of these lines to the actionPerformed() implementation seems to do what you want.
combo1.transferFocus();
combo2.requestFocusInWindow();

Related

What is the best way to make a two option window?

Which is the best way to make a window that has a name a JLabel and two JButton components underneath.
I am new to Swing and I tried some methods but didn't understood too much from any.
What would you suggest to focus my attention on to do this specific thing?
DYM like this?
Or this?
Actually that 2nd one comes from a page linked in the first comment.
But, you might be over complicating the issue and a JOptionPane might be a more suitable solution - see How to make dialogs for more details.

JavaFX ChoiceBox EventHandling

I'm trying to detect ChoiceBox item selection. I read this post and I know that it is possible to do, this way:
choiceBoxObject.getSelectionModel().selectedIndexProperty().addListener(myChangeListenerObject)
also I saw this sentence in Documentation for ChoiceBox class which confirms code above:
ChoiceBox item selection is handled by SelectionModel As with ListView
and ComboBox
Another solution came to my mind and I was wondering is there anything wrong with it? why nobody mentioned this way? What is the difference between these two approaches?
choiceBoxObject.valueProperty().addListener(myChangeListenerObject);
There's nothing wrong with using the valueProperty, and in fact for simply reacting to changes in the selected value, it's probably the preferred solution.
The documentation is just indicating that there is a complete SelectionModel underlying the selection of items. This has a far richer API than simply knowing what is selected: there are selectNext(), selectFirst() methods, etc etc. So if you needed to programmatically change the selection there is a rich API available. As also pointed out in the documentation, you can even replace the selection model with a different implementation, though use cases for this are likely to be (very) rare.

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.

How do I keep keyboard focus on a single component?

I need to keep keyboard input focus on a single component inside a JPanel. It's for an application with a on-screen-keyboard.
Not sure I really understand the question. But you can try something like:
otherComponents.setFocusable( false );
You also might need to use a custom FocusTraversalPolicy.
If you need more help then post a SSCCE that demonstrates the problem.
You better put the focus back to the component (component.grabFocus()) after pressing a button on the on-screen keyboard.
Or you could set focus listener (component.addFocusListener(FocusListener l)) and never let go of the focus by calling grabFocus() in the focusLost() method of FocusListener.
This will give you focus on your singleComponent when opening your Frame, without having to change the focus policy of enything else: singleComponent.requestFocusInWindow(); Since the focus will not freeze, you will need to setFocusable(false) for the other components like camicr suggests.
Just a guess: take a look at the InputVerifier.

Reset/remove a border in Swing

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.

Categories