I'm using a JComboBox to search a query from a sql DB. I want to search query when a letter is typed. I'm using net beans IDE. Here is the automated code.
srch.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
srchKeyReleased(evt);
}
}
private void srchKeyReleased(java.awt.event.KeyEvent evt) {
//Searching code
}
But nothing happens. I'm sue that there is no error in searching code.
Please give me the code to do this without automated code from IDE.
name of JComboBox is srch . Combo box editable=true.
Thank you.
Since your combobox is editable, you should try this:
srch.getEditor().getEditorComponent().addKeyListener()
instead of this
srch.addKeyListener()
Related
how can i delete the auto generated java.awt.event.ActionEvent evt in Netbeans when i double click in Swing on a Button?
Here is a sample from a auto generated Button-Click. How can i delete the unused parameter? Sadly the Codeline is grayed out in Netbeans.
private void sampleActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
thanks in forward!
" How can i delete the unused parameter?"
The answer is, you Don't want it deleted. The parameter is necessary. Why? If you look in the uneditable auto-generated initCompoents(), you will see something like this
jButton1.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
jButton1ActionPerformed(e);
}
});
And the editable method they create for you is the
private void jButton1ActionPerformed(ActionEvent e) {}
which requires the ActionEvent to be passed to it
Go to this
delete java.awt.event.ActionEvent evt
Go to Design and right click on button and select properties then click on Events and remove the unwanted one which you added wrongly.
I am writing Bing/Google instant search kind of feature in combo box, so this combo box provides suggestions to the user based on what he has typed. The program works like a charm but their is one bug that I am unable to figure out how to solve. The problem is, the first character typed is recognised once the second the character has been typed, same goes for other position of characters too.
Here is the code:
public MyClass extends JFrame
{
private Document doc;
public MyCode()
{
comboxBox= new JComboBox();
Handler handle = new Handler();
JTextComponent comp = (JTextComponent) comboBox.getEditor().getEditorComponent();
doc = comp.getDocument().addDocumentListener(handle);
comboBox.addKeyListener(handle);
}
private class Handler implements DocumentListener,KeyListener
{
String dataTobeSearched= "";
#Override
public void changedUpdate(DocumentEvent event) {
try
{
dataTobeSearched = doc.getText(0, doc.getLength());
System.out.println("Data to be searched "+dataTobeSearched);
}
catch(Exception e)
{
e.printStackTrace();
}
}
#Override
public void keyPressed(KeyEvent event) {
changedUpdate(null);
}
}
What am I doing wrong?
I added the keyListener to the combobox editor because the DocumentListener wasn't getting invoked when something was being typed in the combobox? If there is an other easy alternative to this, then please share it.
How can I solve the above stated problem?
Wrap the call inside changedUpdate() in SwingUtilities.invokeLater()
According to the Java tutorial on Oracle website, changedUpdate() method will not work for plain text documents. If this is your case, use insertUpdate() and/or removeUpdate().
The recommendation of using SwingUtilities inside the method is still valid.
I find that a useful way to draw attention to a jcombobox when one wants the user to select from it is to make it drop down at the point it gains focus usually when the previous item has been completed by the user.
How can this been done in Java?
You could do:
comboBox.addFocusListener(new FocusAdapter() {
#Override
public void focusGained(FocusEvent e) {
comboBox.showPopup();
}
});
You want JComboBox#setPopupVisible
Add in a FocusListener to monitor for focus gained and you should be right.
Depending on if the combo box is editable or not, you may need to add a focus listener to the editor as well
rightclick on the combo box. go to events ---> mouse ----> mouseentered.
it will take you to :
private void jComboBox1MouseEntered(java.awt.event.MouseEvent evt) {}
inside the curly braces, type: jComboBox1.showPopup();
it should look like:
private void jComboBox1MouseEntered(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
jComboBox1.showPopup();
}
I'm building a Gui using net beans (Java) and my question is how to get the string in the text field to the variable without pressing the Enter key?
I wrote this code:
private void idTextBoxActionPerformed(java.awt.event.ActionEvent evt) {
this.Id = evt.getActionCommand();
}
The problem is that if I just enter the text and move to the next text field, the data doesn't go into the Id variable, if I press Enter everything OK.
Pressing Enter invokes an ActionEvent from that textfield which you listen for in your actionPerformed method and that is why your code only works in that scenario.
You could use a FocusListener to acheive what you want. You will want to listen for the focusLost event, which is when you move away from the textfield.
class foo implements FocusListener {
JTextField textField = new JTextField("A TextField");
textField.addFocusListener(this);
public void focusGained(FocusEvent e) {
// Do whatever you want
}
public void focusLost(FocusEvent e) {
// Save the text in the field to your id variable
}
}
EDIT
The following tutorial shows how to use a formatted textfield. You can ignore the formatting bit and focus on the propertyChangeListner aspect of it.
The idea is the same as my first example but using a different type of listener.
You could use the action event or focus event to track the changes at component level. But if its the text changes that you are interested in, then you should consider using a DocumentListener. Read the tutorial here
I have this code:
this.trigger = new Trigger();
this.presentationModel = new PresentationModel(this.personBean, this.trigger);
final ValueModel firstNameAdapter = presentationModel.getBufferedModel("firstName");
final JTextField firstNameTextField = BasicComponentFactory.createTextField(firstNameAdapter);
and
firstNameTextField.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
trigger.triggerCommit();
}
});
So when I push the enter button on the JTextField, I expect the value in my ValueModel class to be the same as the value in my JTextField. This doesn't happen unless I click outside the JTextField, then back inside the JTextField, and then push enter. If I just type in the text and hit enter, the ValueModel does not get the updated value. I am stuck on this problem, can anybody help?
BTW, I used this link for figuring out JGoodies in the first place: JGoodies Tutorial
I hope I am understanding your question correctly.
You need to get the text in the text field and set it in the ValueModel.
firstNameTextField.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
//this get the text from the text field
String firstName = firstNameTextField.getText();
//now write your code to set the firstname into the ValueModel
trigger.triggerCommit();
}
});
I looked through the JGoodies API (should have done this sooner) and found an unexpected static call, Bindings.commitImmediately()
If I call this method before my call to trigger.triggerCommit(), everything works as expected :)
Create a text field that commits on each key typed instead of when focus is lost:
BasicComponentFactory.createTextField(firstNameAdapter, false);
Also, you should consider architecting your program to not use buffered models. I find that they make things more complicated and tricky, and think I saw Karsten Lentzsch recommending not to use them as well in a mailing list.
The most useful way for me to learn JGoodies was to look at the tutorial code for the JGoodies binding and validation libraries.