Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
Modal window button focus
Hi,
I have problem with focusing button on modal window.
I am calling
Button.focus();
focus listener is triggered, but button loses focus
When I call TextField.focus(), it focuses TextField but not button.
http://dev.pakom.hr/Android_Maloprodaja/
- this is my program and I am calling focus to button Nastavi (F11)
Hmm upon checking the demo app the button sure does lose the focus. Can you attach your codes? What does the focuslistener do?
This i code from window:
package hr.pakom.android_maloprodaja;
import java.util.Iterator;
import com.vaadin.event.FieldEvents.BlurEvent;
import com.vaadin.event.FieldEvents.BlurListener;
import com.vaadin.event.FieldEvents.FocusEvent;
import com.vaadin.event.FieldEvents.FocusListener;
import com.vaadin.event.ShortcutAction;
import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.event.ShortcutListener;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;
public class Login extends Window {
GlobalneFunkcije global;
DatabaseHelper db;
Login_CC UpitPasswordCC;
AbsoluteLayout MainLayout;
Button ButtonNastavi;
Button ButtonOdustani;
TextField KorImeTextField;
PasswordField LozinkaPasswordField;
int _iFocusedTabIndex;
public Login() {
super("Prijava"); // Set window caption
center();
setClosable(false);
setModal(true);
setResizable(false);
global = GlobalneFunkcije.getInstance();
db = new DatabaseHelper();
UpitPasswordCC = new Login_CC();
setContent(UpitPasswordCC);
MainLayout = UpitPasswordCC.mainLayout;
ButtonNastavi = UpitPasswordCC.ButtonNastavi;
ButtonOdustani = UpitPasswordCC.ButtonOdustani;
KorImeTextField = UpitPasswordCC.KorImeTextField;
LozinkaPasswordField = UpitPasswordCC.LozinkaPasswordField;
ButtonNastavi.setClickShortcut(KeyCode.F11);
ButtonOdustani.setClickShortcut(KeyCode.ESCAPE);
DodajFocusListener();
addShortcutListener(skEnterListener);
addShortcutListener(skUpListener);
addShortcutListener(skDownListener);
ButtonNastavi.addClickListener(new ButtonNastaviListener());
ButtonOdustani.addClickListener(new ButtonOdustaniListener());
KorImeTextField.focus();
ButtonNastavi.focus();
}
public class ButtonNastaviListener implements Button.ClickListener {
@Override
public void buttonClick(Button.ClickEvent event) {
//PROVJERE
String sKorIme = KorImeTextField.getValue();
String sLozinka = LozinkaPasswordField.getValue();
//Da li korisnik postoji
if (db.Korisnici_ProvjeriPostojanje(sKorIme) == false) {
KorImeTextField.focus();
global.PokaziPoruku("Korisnik ne postoji!");
return;
}
if (db.Korisnici_IspravnaSifra(sKorIme, sLozinka) == false) {
LozinkaPasswordField.focus();
global.PokaziPoruku("Lozinka je pogreano unesena!");
return;
}
global.sGlobKorisnik = sKorIme;
global.bGlobPregled1 = true;
close();
}
}
public class ButtonOdustaniListener implements Button.ClickListener {
@Override
public void buttonClick(Button.ClickEvent event) {
global.bGlobPregled1 = false;
close();
}
}
private void DodajFocusListener() {
Iterator<Component> iterate = MainLayout.iterator();
while (iterate.hasNext()) {
Component c = iterate.next();
if (c instanceof TextField) {
TextField textField = (TextField) c;
textField.addFocusListener(focusListener);
}
if (c instanceof PasswordField) {
PasswordField passwordField = (PasswordField) c;
passwordField.addFocusListener(focusListener);
}
if (c instanceof Button) {
Button button = (Button) c;
button.addFocusListener(focusListener);
}
}
}
private FocusListener focusListener = new FocusListener() {
@Override
public void focus(FocusEvent event) {
Component c = event.getComponent();
int iTabIndex = -1;
if (c instanceof TextField) {
TextField textField = (TextField) c;
iTabIndex = textField.getTabIndex();
textField.selectAll();
}
if (c instanceof PasswordField) {
PasswordField passwordField = (PasswordField) c;
iTabIndex = passwordField.getTabIndex();
passwordField.selectAll();
}
if (c instanceof Button) {
Button button = (Button) c;
iTabIndex = button.getTabIndex();
}
_iFocusedTabIndex = iTabIndex;
}
};
ShortcutListener skEnterListener = new ShortcutListener("Enter", ShortcutAction.KeyCode.ENTER, null){
@Override
public void handleAction(Object sender, Object target) {
if ((target instanceof TextField) || (target instanceof PasswordField)) {
FokusirajDalje(0);
}
}
};
ShortcutListener skUpListener = new ShortcutListener("Up", ShortcutAction.KeyCode.ARROW_UP, null){
@Override
public void handleAction(Object sender, Object target) {
if ((target instanceof TextField) || (target instanceof PasswordField) || (target instanceof Button) ) {
FokusirajDalje(1);
}
}
};
ShortcutListener skDownListener = new ShortcutListener("Down", ShortcutAction.KeyCode.ARROW_DOWN, null){
@Override
public void handleAction(Object sender, Object target) {
if ((target instanceof TextField) || (target instanceof PasswordField) || (target instanceof Button)) {
FokusirajDalje(0);
}
}
};
private void FokusirajDalje(int iSmjer) {
//0 - Unaprijed
//1 - Unazad
int iZbrajatelj = 0;
switch (iSmjer) {
case 0: iZbrajatelj = 1;
break;
case 1: iZbrajatelj = -1;
break;
}
int iTabIndex;
Iterator<Component> iterate = MainLayout.iterator();
while (iterate.hasNext()) {
Component c = iterate.next();
iTabIndex = -1;
if (c instanceof TextField) {
TextField textField = (TextField) c;
iTabIndex = textField.getTabIndex();
}
if (c instanceof PasswordField) {
PasswordField passwordField = (PasswordField) c;
iTabIndex = passwordField.getTabIndex();
}
if (c instanceof Button) {
Button button = (Button) c;
iTabIndex = button.getTabIndex();
}
if (iTabIndex == _iFocusedTabIndex + iZbrajatelj) {
if (c instanceof TextField) {
TextField textField = (TextField) c;
textField.focus();
}
if (c instanceof PasswordField) {
PasswordField passwordField = (PasswordField) c;
passwordField.focus();
}
if (c instanceof Button) {
Button button = (Button) c;
button.focus();
}
break;
}
}
}
}
And this is from Main UI
package hr.pakom.android_maloprodaja;
import hr.pakom.android_maloprodaja.Sifrarnik_Porezi.UpitBrisiListener;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
import javax.servlet.annotation.WebServlet;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.Window.CloseEvent;
@SuppressWarnings("serial")
@Theme("android_maloprodaja")
public class Android_MaloprodajaUI extends UI {
GlobalneFunkcije global;
DatabaseHelper db;
@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = Android_MaloprodajaUI.class)
public static class Servlet extends VaadinServlet {
}
@Override
protected void init(VaadinRequest request) {
global = GlobalneFunkcije.getInstance();
db = new DatabaseHelper();
Login LoginSub = new Login();
LoginSub.addCloseListener(new LoginSubCloseListener());
UI.getCurrent().addWindow(LoginSub);
/*final VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
setContent(layout);
Button button = new Button("Click Me");
button.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
layout.addComponent(new Label("Thank you for clicking"));
}
});
layout.addComponent(button);*/
}
public class LoginSubCloseListener implements Window.CloseListener {
@Override
public void windowClose(CloseEvent e) {
if (global.bGlobPregled1) {
Uredjaji UredjajiSub = new Uredjaji();
UredjajiSub.setModal(true);
UredjajiSub.addCloseListener(new UredjajiSubCloseListener());
UI.getCurrent().addWindow(UredjajiSub);
}
}
}
public class UredjajiSubCloseListener implements Window.CloseListener {
@Override
public void windowClose(CloseEvent e) {
if (global.bGlobPregled1) {
Main mycomposite = new Main();
setContent(mycomposite);
}
}
}
}
To isolate the issue can you try commenting line 60, DodajFocusListener();.
It is adding focus listener to all components
It is below in code
private void DodajFocusListener() {
Iterator<Component> iterate = MainLayout.iterator();
while (iterate.hasNext()) {
Component c = iterate.next();
if (c instanceof TextField) {
TextField textField = (TextField) c;
textField.addFocusListener(focusListener);
}
if (c instanceof PasswordField) {
PasswordField passwordField = (PasswordField) c;
passwordField.addFocusListener(focusListener);
}
if (c instanceof Button) {
Button button = (Button) c;
button.addFocusListener(focusListener);
}
}
}
I mean try commenting it or remove it. Then rerun and test the application if focus is now placed on the button.
Same thing...
Try this, try to press button "Nastavi (F11)" on my app
It pops up messagebox, this is this messagebox:
https://vaadin.com/directory#!addon/messagebox
Online demo from messagebox:
http://env-0190796.jelastic.servint.net/messagebox/
Press: 14: Button focused
there is focused Button Save
I am using the same code for my messagebox but button again loses focus
MessageBox mb = MessageBox.showPlain(Icon.INFO, "Poruka", sText, ButtonId.OK);
mb.setButtonAlignment(Alignment.MIDDLE_CENTER);
mb.getButton(ButtonId.OK).focus();
Hi, which vaadin version do you use? There exists a bug in version 7.4.5 or higher regarding to focused components in windows.
Heinz-Jürgen Lutsch - You are correct
I have used Vaadin 7.4.6
Changed version to 7.4.4 an now it is working
Thank you all