Java AutoComplete : Popup on every KeyPress - java

I am using Java AutoComplete library to make an IDE. I was wondering how can I make the popup window show on every KeyPress. I have tried :
autoCompletion.setAutoCompleteSingleChoices(false);
textArea.addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {
autoCompletion.doCompletion();
}
#Override
public void keyPressed(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
}
});
But it just doesn't work how I wanted. I need it to work this way :
Demonstration here
Any help is highly appreciated. Thanks :)

Related

Change image on JButton on mouse over

I am using an Icon with Java (Swing) JButton. Is it possible to change the icon when I take my mouse arrow over it?
I saw somewhere on Youtube that it is possible, but am unable to recall it.
You can take advantage of the JButton API which provides this kind of support.
Take a look at JButton#setRolloverIcon and JButton#setRolloverSelectedIcon
You will need to implement MouseListener like this:
public class YourClass extends JFrame implements MouseListener {
#Override
public void mouseEntered(MouseEvent e) { }
#Override
public void mouseExited(MouseEvent e) { }
#Override
public void mouseClicked(MouseEvent e) { }
#Override
public void mousePressed(MouseEvent e) { }
#Override
public void mouseReleased(MouseEvent e) { }
}
Add your function where needed.
You can override the mouseEntered() function by implementing a MouseListener and add the code to change the icon in that function.
If you're using an abstract button, you can just use setRolloverIcon() to set an image which will appear on rollOver.

Create an actionlistener(?) in Java

So I searched Stackoverflow, but couldn't find any actual answer that I got. If there's already an answer to this question, please tell me.
I have a class with a showDescription method. This prints a string variable.
I require this method to be called whenever the "d" key is pressed, in the main method. So, what would the code be to implement the key press/down event?
Do this if you have a swing application:
f.addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
if ((e.getKeyCode() == KeyEvent.VK_D) && ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
System.out.println("woot!");
}
}
#Override
public void keyReleased(KeyEvent e) {
}
});
you can read more here and here
If you have a console application then use:
Read Input until control+d

Java Swing: Using a Document Listener to Handle the Return Key

I have a document listener that works just fine. However, I'd like to add some functionality to it so that when the user hits the Enter key, the focus shifts to another object. I can't figure out how to trap this. Here is my code:
txtNum1.getDocument().addDocumentListener(new DocumentListener() {
#Override
public void insertUpdate(DocumentEvent e) {
setAnswer(e);
}
#Override
public void removeUpdate(DocumentEvent e) {
setAnswer(e);
}
#Override
public void changedUpdate(DocumentEvent e) {
setAnswer(e);
}
private void setAnswer(DocumentEvent e) {
if (txtNum1.getText().equals("")) {
num1 = 0;
} else {
num1 = Integer.parseInt(txtNum1.getText());
}
calcAnswer();
System.out.println(e); //trying to output the event 'Enter'
}
I can do this with a key listener, but I've been scolded on this site before about using that approach, so I'm trying to learn this the correct way.
Thanks!
EDIT:
Per the suggestions below, I added the following code, but it seems to have no effect. Can anyone see what I am missing? Thanks!
/* If the user hits the Enter key, we want the focus to shift to
* the next text field */
txtNum1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
txtNum2.requestFocus();
}
});
On a JTextfield, you can trap the Enter key simply by adding an ActionListener. It will get fired when the users types enter

KeyListener only sometimes works

I am writing a game and I have just tried to add the KeyListener. I have experience with java including KeyListeners but I for some reason cannot figure out why this code only works some of the time.
Here is my listener function:
public void Listener() {
System.out.println("[INFO] Listener() Ran.");
KeyListener kl = new KeyListener() {
public void keyPressed(KeyEvent e) {
if(e.getKeyChar()=='a'){
System.out.println("[DEBUG] A Pressed.");
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
};
panel.addKeyListener(kl);
System.out.println("[DEBUG] panel added KeyListener.");
}
This code works probably only 1 out of 10 times that I run it. Maybe even less. Any ideas on why this is?
The getKeyChar should be called in the keyTyped. The getKeyCode() == KeyEvent.VK_A in the other both methods.

how to listen for clicks in Java (JFreeChart) using events?

This is the code I'm currently using:
#Override
public void mouseExited(MouseEvent e) {
System.out.println("detectado");
}
You can use addChartMouseListener() to add a ChartMouseListener to your ChartPanel. For example, in BarChartDemo1, add the following:
chartPanel.addChartMouseListener(new ChartMouseListener() {
public void chartMouseClicked(ChartMouseEvent e) {
System.out.println(e.getEntity());
}
public void chartMouseMoved(ChartMouseEvent e) {}
});
To listen for clicks, you must check the type of event.
In particular, you override the
public void mouseClicked(MouseEvent ev)
method, which is part of the interface for MouseListeners.
For a fill example see : this link

Categories

Resources