I have a frame and a JWindow. In my frame I have a textfield, whenever I type something to the field, the window will appear with list of suggestions below the textfield. I used a keylistener to the field.
When I press the enter key on the list of suggestion in the window, the word that I select goes to the field.
Now the problem is that the window still appear, I want the window to disappear whenever I select a word.
Could someone got an idea about this?
Thanks..
Try this:
jWindowInstance.setVisible(false);
I'm assuming you have an OK button on there, in which case you should be able to set the default button on the root pane of the window, e.g.
window.getRootPane().setDefaultButton(okBtn);
Try this:
jWindowInstance.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
jWindowInstance.dispose(); // Release resources
// OR
jWindowInstance.setVisble(false); // Just hide the window so you can reuse it afterwards
}
}
});
You can send the selected item to the textbox, right? I assume that you have used some kind of event listener to do that. At the end of the action method, make the window's visibility to false. Swaranga's way should work.
Related
I'm very new at coding Java and Netbeans. So basically, I have a "save" button and three text fields, I want to enable the Button when these three text fields are edited and disable the button when one of them is empty. Also I'm wondering where I should put my codes. Since it's Netbeans I'm only familiar with ActionPerformed methods, there you can set an action when a button is pressed.
If you can keep it simple it would be appreciated!
public project() {
initComponents();
//Here I want the window to appear in the middle of the screen
setLocationRelativeTo(null);
if(txfField1.getText().equals("")){
btnSave.setEnabled(false);
}
else {
btnSave.setEnabled(true);
}
}
I tried with this code on only one of the three text fields and It does not work, the button is always enabled. The button is initially disabled. Additionally I have also tried to put my code below this method:
public class project extends javax.swing.JFrame {
You can use event handlers to change the state of the button. For example, if you have one text field and you want to change the state of the button depending on the data inside the text field, you could use something like
if (!jTextField1.getText().equals("")) {
jButton1.setEnabled(true);
} else {
jButton1.setEnabled(false);
}
and the event handler you can use
private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {
You can generate this automatically in Netbeans by going to the event tab when you have clicked on a component in the design view.
It seems in your example you have the right idea, however you need to update the button using events such as key pressing, key releasing etc
You can add it in onblur() method of those text boxes.
If it can, you can add a validation with an error message on click of save button, which might be more meaningful.
the Title states my problem almost completely.
I have some combo box classes which derive from JComboBox, additionally we use the PlasticUI from JGoodies.
My Problem is that when I navigate through the available items in the drop down popup
those items are automatically being selected.
This only happens when I use the navigation keys, hovering with the mouse over the objects is fine.
In my case this is pretty bad because it somehow provokes the lazy-loaded data in the object to be loaded and slow the combo box down immensely.
How can I turn this behavior off?
I tried debugging, but I cannot find a place to set a breakpoint properly, too much magic happening in the background :/
Plzz help :)
You can use the function ActionEvent.getModifiers() to check if the ItemChangeEvent got fired with the keyboard or the mouse.
JCheckBox box = new JCheckBox();
box.addActionListener(new ActionListener() {
#Override
public void actionPerformed(final ActionEvent e) {
if (e.getModifiers() == 0) {
System.out.println("keyboard");
} else {
System.out.println("mouse");
}
}
});
I have the following code for JList. On click for an item in the list it should highlight the selected item. But if I press too fast it wont actually select the next item on list on the first click. How should I solve this?
MouseListener mouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
String selectedItem = (String) jl.getSelectedValue();
if(selectedItem == "Page One"){
System.out.print("Page one");
}
}
}
};
jl.addMouseListener(mouseListener);
A MouseListener is in appropriate for the task, instead use a ListSelectionListener
Take a look at How to write a List Selection Listener and How to use lists for more details
On click for an item in the list it should highlight the selected item
This is the default behaviour, so I'm not sure why you are doing this.
But if I press too fast it wont actually select the next item on list on the first click.
Probably because you aren't generating a mouseClicked event. A mouseClicked event is only generated when a mousePressed/mouseReleased event is generated at the same pixel location. Maybe the mouse is moving slightly. Try just adding your code to mousePressed.
but i only want mouse click, even if the user using the arrow key to change it should not happen
That is a terrible UI. The user should control whether they want to use the mouse or keyboard. Advanced users will use the keyboard and beginners will use the mouse.
I have a panel. This panel has one text field and a button. The text field has focus listener to search some db value, if not value is written it shows an exit display message when tab.
But, when edit the text field and button clicked without pressing tab key, following order occurs:
1) focus lost
2) action listener
Problem is the calling the focus lost, action listener should be call when
edit into text field ---> button clicked(without tab into text field)
Would you please kindly share your idea ?
"If the user leaves after typing something then call the action listner without calling lost focus
Okay, firstly, you can't not have focus lost fired, however, you can ignore it
public void focusLost(FocusEvent evt) {
if (textField.getText().length() > 0) {
// call action
} else {
// show error message
}
}
Okay, now that we can ignore the focus event, how to fire the action event?
Well, surprisingly, this really simple
button.doClick();
I want to show the Open File dialog box when a user clicks on a JTextField. When I added the following code (which I removed for now)...
this.textField.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent event) {
// Show the Open File dialog box.
// Same as lines 86-93 in the link below.
}
public void focusLost(FocusEvent event) {
// Do nothing.
}
}
(Code here.)
...it seems that after the user selects a file and then clicks on the OK button, the Open File dialog box will appear again, because I assume that the focus is still on the JTextField. The same thing happens when the user clicks on the Cancel button.
How do I fix this problem? Your advice will be greatly appreciated!
The problem is when the file chooser dialog appears, it takes focus. When it closes (I assume) you refocus the textfield (or the focus manager is returning focus to it), which triggers the focus event again.
I can think of two solutions. One, if you only want the file dialog to appear when the user "clicks" the field, use a mouse listener instead.
Two, use a internal flag to monitor that the current operational state. This might be more difficult to implement given the nature of the events processing