2
My intention is to create a search field where you type and appear the data of a query in the BD as a popup menu. I got this, it’s working. I created the menu and I can show it on the screen with the data, but the position is the problem: I want something like the Google search autocomplete! The way I’m doing the menu is over the TF and still exceeds in width and still has more: I don’t even know if the way I’m positioning the menu is the most appropriate.
My code (the part where the menu is created):
public void mostrarPopUp(DocumentEvent e) {
tfTexto.setEditable(false);
pumAutoComp.removeAll();
List lista = buscar(tfTexto.getText());
for (int i = 0; i< lista.size(); i++)
{
String nmItemMenu;
nmItemMenu = lista.get(i).toString();
System.out.println(nmItemMenu);
JMenuItem item = new JMenuItem(nmItemMenu);
pumAutoComp.add(item);
tfTexto.add(pumAutoComp);
tfTexto.setComponentPopupMenu(pumAutoComp);
}
try {
int dotPosition = tfTexto.getCaretPosition();
Rectangle popupLocation = tfTexto.modelToView(dotPosition);
pumAutoComp.show(tfTexto, popupLocation.x, popupLocation.y);
} catch (BadLocationException badLocationException) {
System.out.println("Oops");
}
tfTexto.setEditable(true);
}
Screenshot of my current menu:
As I would like to: (don’t worry about making the text fit, that I already got :))
the part "System.out.println(nmItemMenu);" I was just supposed to test if I was searching right! You can ignore it!
– Mariana Sempe