Is there a postActionEvent for a JComboBox? - java

I successfully use postActionEvent() on a JTextField that has an ActionListener to simulate User action (pressing Enter key). I would like to create the same type of simulation for a JComboBox that has an ActionListener, but I don't find a postActionEvent() for a JComboBox. How could this (simulating User pressing Enter key) be accomplished?

How could this (simulating User pressing Enter key) be accomplished?
Combobox has an "enterPressed" Action. So you should be able to access the Action from the ActionMap of the combobox and then manually invoke the actionPerformed(...) method of the Action.
Check out Key Bindings for a program to list all the bindings for all Swing components.

You could also use a KeyListener:
addKeyListener(new KeyAdapter() {
#Override
public void keyReleased(KeyEvent event) {
if (event.getKeyChar() == KeyEvent.VK_ENTER) {
if (((JTextComponent) ((JComboBox) ((Component) event
.getSource()).getParent()).getEditor()
.getEditorComponent()).getText().isEmpty())
System.out.println("please dont make me blank");
}
}
});
See this question

Related

Using spacebar in desktop application

I write desktop application using Swing. I have JFrame and many JButton and JTextField inside frame.
Using KeyEventDispatcher I catch spacebar press event Keyevent.VK_SPACE and click in cancelButton
new KeyEventDispatcher(KeyEvent.VK_SPACE) {
#Override
public void performAction(KeyEvent event) {
cancelButton.doClick();
cancelButton.requestFocus();
}
}
It works, but on cancelButton other event change his state to cancelButton.setEnabled(false) and when I press spacebar after disable this cancelButton then other button in the same frame is clicked. I think this is option of system but maybe can I blocked this ? I care about this that the pressing spacebar do one thing which is declared in KeyEventDispatcher.
I think about change this spacebar to other letter from keyboard but I want to know that I can solved this problem using spacebar.
Any idea how to resolve this problem?
EDIT
I try with CTRL key but at every press this keys other button is highlighted like pressing TAB. The same action is with C key also when set at JTextField it changes value of this field.
SOLVED
I solved it by adding event.consume() and remove cancelButton.requestFocus()
new KeyEventDispatcher(KeyEvent.VK_SPACE) {
#Override
public void performAction(KeyEvent event) {
event.consume();
cancelButton.doClick();
}
}

How to make Enter key and Submit button have same ActionEvent?

So I have a Submit button with an ActionEvent that consists of around 50 lines of code. How would I assign the exact same ActionEvent for the JFrame as the Submit button whenever it detects the Enter key being pressed? This is what my Submit button's ActionEvent looks like
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// miscellaneous code that needs to be repeated for 'Enter' key press
}
});
What and where would the code for giving the JFrame the same ActionEvent as the Submit button go?
Start by taking a look at How to Use Root Panes and in particular JRootPane#setDefaultButton
When you have components which may consume the Enter key (like text fields), you might need to consider using the key bindings API
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "Enter.pressed");
am.put("Enter.pressed", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
btnSubmit.doClick();
}
});
Now, about now, I might consider making an Action which be applied to both the JButton and key binding
Have a look at How to Use Key Bindings and How to Use Actions for more details
I don't know if there's a more correct swing way, but this should do the trick:
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
//...
}
}
btnSubmit.addActionListener(listener);
btnEnter.addActionListener(listener);
One way to do this is to use the .doClick() method on the Submit button and create a KeyAdapter:
KeyAdapter Enter = new KeyAdapter(){
#Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER){
btnSubmit.doClick();
}
}
};
txtField1.addKeyListener(Enter);
txtField2.addKeyListener(Enter);

When it's called actionPerformed?

When I add a object o1 to a button with addActionListener(), for what actions is the actionPerformed() of o1 called? This is available for JTextField?
actionPerformed is called when user performs any operation on that swing component. ActionListner can be added every Swing component. So yes you can add it on the JTextField. But it gets called only in case if somebody press enter key on JTextField. For other actions you need to add other listners such as DocumentListner.
actionPerformed(ActionEvent e) is a abstract method of ActionListener interface. You should add it following way.
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//............
}
});
Is this available for JtextField?
Yes, this is available for JTextField.
For a JButton the ActionListener is called when the button is clicked.
The ActionListener of a JTextField is fired when Enter is pressed.
This is explained in the documentation.

add ListSelectionListener on JComboBox

I'm quite new to Java Swing. And I'm stuck on trying to add a ListSelectionListener on a JComboBox instance. It seems only the ListSelectionModel interface has addListSelectionListener method. I kind of cannot figure it out...
Why I want to do add it is that I want program do something even the item in the combo box is not changes after selecting.
POTENTIAL ANSWER
I was simply thinking of attaching an actionListener on combobox not working. and i think it's bug of openjdk. I've reported it here
Thanks in advance.
Take a look at JComboBox#addItemListener:
JComboBox combo = createCombo();
combo.addItemListener(new ItemListener()
{
#Override
public void itemStateChanged(ItemEvent e)
{
if (e.getStateChange() == ItemEvent.SELECTED)
{
Object selectedItem = e.getItem();
// Do something with the selected item...
}
}
});
This event is fired for both mouse and keyboard interaction.
For JComboBox, you'll have to use ActionListener.
JComboBox jComboBox = new JComboBox();
jComboBox.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("combobox event");
}
});
AFAIK, actionPerformed is raised whenever the user makes a selection for the JComboBox even if it's the same item that was already selected.
It depends on your requirement. The ActionEvent is only fired when the keyboard is used, not when the selection changes as the mouse is moved over the items.
If you want to do some action when the item selection changes even if the mouse is moved then yes you will probably need access to the JList. You can access the JList used by the popup with the following code:
JComboBox comboBox = new JComboBox(...);
BasicComboPopup popup = (BasicComboPopup)comboBox.getAccessibleContext().getAccessibleChild(0);
JList list = popup.getList();
list.addListSelectionListener(...);
Use a PopupMenuListener. When the popup menu closes get the selected index and do your processing.

Swing: detect Enter in JComboBox?

I've tried using getInputMap() + getActionMap() on a JComboBox and it seems to have no effect.
I've tried addActionListener() / addItemListener() on a JComboBox and I can't seem to distinguish a change of selection from someone pressing the Return/Enter key.
Any suggestions? In my application, I want the Return/Enter key to be stronger than just selecting, it's a selecting + applying action.
Here's my code to setup the key binding. It works fine (e.g. note("hit ENTER") is called) when component is a JList, but doesn't work when component is a JComboBox.
private void setupApplyProfile(final JComponent component, final MyComboBoxModel mcbm)
{
String enterAction = "applyItem";
KeyStroke enterKey = KeyStroke.getKeyStroke("ENTER");
component.getInputMap().put(enterKey, enterAction);
component.getActionMap().put(enterAction, new AbstractAction()
{
#Override public void actionPerformed(ActionEvent e) {
note("hit ENTER");
applySelectedProfile(mcbm);
}
});
}
Aha, this seems to work: note("cb editor action") gets called when I hit Enter in the combo box field.
comboBox.getEditor().addActionListener(new ActionListener() {
#Override public void actionPerformed(ActionEvent arg0) {
note("cb editor action");
}
});
In my application, I want the Return/Enter key to be stronger than just selecting, it's a selecting + applying action.
If I understand the question you can use the following:
comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
The ActionEvent and ItemEvents will only be fired when an item is selected from the drop down list when you use the mouse or the enter key. The eEvents will not be fired if you navigate the drop down list using the up/down arrow keys.

Categories