BrowserWindowOpener close event - java

I have such code
printWindow = new BrowserWindowOpener(invoicesBeanService.getHTMLStream()); printWindow.setFeatures("menubar=no,location=no,toolbar=no,resizable=no,scrollbars=yes,status=no,width=900");
printWindow.extend(this.button_2);
How can I detect close event of BrowserWindowOpener when I close the popup window?

Add click listener to the button
Button button_2 = new Button("Close");
button_2.addClickListener(new ClickListener() {
private static final long serialVersionUID = 1L;
public void buttonClick(ClickEvent event) {
close();
}
});
printWindow.extend(button_2);
layout.addComponent(button_2);

Related

Vaadin: How To programmatically perform a KeyPressEvent on TAB-Button?

is there a way to programmatically perform a Button Press Event i.e for the TAB-Button in Vaadin? I have to write a test for a ShortCutListener, which listens to ShortCut ShortCutAction.KeyEvent.TAB.
I have tried something like that:
Button button = new Button();
button.addShortcutListener(new ShortcutListener("ShortCut", ShortcutAction.KeyCode.TAB, null) {
private static final long serialVersionUID = 1L;
#Override
public void handleAction(Object sender, Object target) {
System.out.println("Click!");
}
});
button.setClickShortcut(ShortcutAction.KeyCode.TAB, null);
button.click();
If what you want is triggering the click event when pressing the tab key, you could do the following:
Button button = new Button();
button.addClickListener(new Button.ClickListener() {
private static final long serialVersionUID = 1L;
#Override
public void buttonClick(final ClickEvent event) {
System.out.println("Click!");
}
});
button.setClickShortcut(ShortcutAction.KeyCode.TAB);
button.click();
Using a Vaadin Button to do something useful on a key press is probably not a good idea, except if the keypress is a shortcut to clicking on the button (which the setClickShortcut method lets you define).
If you want to do something specific on a keypress, something that is different from what your buttons do, you should define an action handler on your Window or Panel, as Vaadin recommends.

Java Swings: Is there any provision to add a close button to popup window?

How can we add a close button on the Pop up window similar to a JFrame on the right corner so that we can close that popup by clicking on it?
Thanks for the help
You can create your own PopMenu by extanding JPopupMenu. By adding an action listener to the button you can react with anything you want(for example closing popup). Here is an example:
public class TestPopupMenu extends JPopupMenu {
private JLabel testLabel;
private JButton testButton;
public TestPopupMenu() {
super();
initMenuItems();
}
private void initMenuItems() {
add(getTestLabel());
add(getTestButton());
}
private JButton getTestButton() {
if (testButton == null) {
testButton = new JButton("Test");
testButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// Do something what you want
}
});
}
return testButton;
}
private JLabel getTestLabel() {
if (testLabel == null) {
testLabel = new JLabel("#Some text");
}
return testLabel;
} }

Java KeyEvent JUST ALT

I want to be able to (in a JPanel - I'm using KeyBindings for arrow keys and ALT + KeyEvent.VK_lots_of_different_keys) press ALT (and only ALT) and know when it is pressed so I can drag the mouse around and stop it from performing other things that should not happen when alt is pressed.
Is there a KeyEvent for this or a work-around? (I have tried the various (4) ALT key masks, but those obviously don't work).
some code:
altPressed = false;
InputMap inMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap actMap = getActionMap();
KeyStroke pressed = KeyStroke.getKeyStroke(KeyEvent.VK_ALT, 0, false);
KeyStroke released = KeyStroke.getKeyStroke(KeyEvent.VK_ALT, 0, true);
inMap.put(pressed, "alt" + "pressed");
inMap.put(released, "alt" + "released");
actMap.put("alt" + "pressed", new AbstractAction () {
private static final long serialVersionUID = 1L;
#Override
public void actionPerformed(ActionEvent e) {
altPressed = true;
}
});
actMap.put("alt" + "released", new AbstractAction () {
private static final long serialVersionUID = 1L;
#Override
public void actionPerformed(ActionEvent e) {
altPressed = false;
}
});
private class KeyListener implements ActionListener {//... yada yada yada ...
#Override
public void actionPerformed(ActionEvent e) {// check if keys are pressed and perform proper actions....
...
...
}
Timer timer = new Timer(100, new KeyListener());
timer.start();
If you trying to detect if the Alt button is pressed while clicking/dragging/etc. with the mouse, you can use the getModifiersEx() method of MouseEvent. The MouseEvent object will be sent to your listener in each of the methods of the MouseInputListener impelementation.

Both ENTER shortcut and TextArea in Vaadin

TextField f = new TextField();
Button b = new Button("Save");
b.setClickShortcut(KeyCode.ENTER); // For quick saving from text field itself
TextArea longText = new TextArea(); // "Enter" is garbled here
Hot to make the shortcut to work only in the from text field?
Use focus and blur listeners to remove and add the shortcut key:
f.addFocusListener(new FocusListener() {
#Override
public void focus(FocusEvent event) {
b.setClickShortcut(KeyCode.ENTER);
}
});
f.addBlurListener(new BlurListener() {
#Override
public void blur(BlurEvent event) {
b.removeClickShortcut();
}
});
Newer versions of Vaadin require the following code as addListener() is deprecated now.
f.addFocusListener(new FocusListener() {
private static final long serialVersionUID = -6733373447805994139L;
#Override
public void focus(FocusEvent event) {
b.setClickShortcut(KeyCode.ENTER);
}
});
f.addBlurListener(new BlurListener() {
private static final long serialVersionUID = -3673311830300629513L;
#Override
public void blur(BlurEvent event) {
b.removeClickShortcut();
}
});
Talking in terms of Vaadin 14,
I was looking for the answer and for me, this worked well
Button search = new Button("Search");
search.addClickShortcut(Key.ENTER);
As of Vaadin 23 (and probably for sometime before) the requirements have changed again.
private ShortcutRegistration primaryShortCut;
void customShortCutHandling()
{
myTextAreaField.addFocusListener((e) ->
{
System.out.println("disable");
primaryShortCut = primaryButton.addClickShortcut(Key.ENTER);
});
myTextAreaField.addBlurListener((e) ->
{
System.out.println("enable");
primaryShortCut.remove();
});
}
}
This code assumes that primaryShortCut was set when the form is created.

Swing: how do I close a dialog when the ESC key is pressed?

GUI development with Swing.
I have a custom dialog for choosing a file to be opened in my application; its class extends javax.swing.JDialog and contains, among other components, a JFileChooser, which can be toggled to be shown or hidden.
The JFileChooser component already handles the ESC key by itself: when the file chooser is shown (embedded in my dialog) and I press ESC, the file chooser hides itself.
Now I would like my dialog to do the same: when I press ESC, I want the dialog to close. Mind you, when the embedded file chooser is shown, the ESC key should only hide it.
Any ideas ?
You can use the following snippet. This is better because the rootPane will get events from any component in the dialog. You can replace setVisible(false) with dispose() if you want.
public static void addEscapeListener(final JDialog dialog) {
ActionListener escListener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
}
};
dialog.getRootPane().registerKeyboardAction(escListener,
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
JComponent.WHEN_IN_FOCUSED_WINDOW);
}
Use InputMap and ActionMap for dealing with key actions in Swing. To close the dialog cleanly, send a window closing event to it.
From my now defunct weblog:
private static final KeyStroke escapeStroke =
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
public static final String dispatchWindowClosingActionMapKey =
"com.spodding.tackline.dispatch:WINDOW_CLOSING";
public static void installEscapeCloseOperation(final JDialog dialog) {
Action dispatchClosing = new AbstractAction() {
public void actionPerformed(ActionEvent event) {
dialog.dispatchEvent(new WindowEvent(
dialog, WindowEvent.WINDOW_CLOSING
));
}
};
JRootPane root = dialog.getRootPane();
root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
escapeStroke, dispatchWindowClosingActionMapKey
);
root.getActionMap().put( dispatchWindowClosingActionMapKey, dispatchClosing
);
}
If your looking for a technique using new features of Java 8 , try a lambda expression:
dialog.getRootPane().registerKeyboardAction(e -> {
window.dispose();
}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
or
KeyStroke k = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
int w = JComponent.WHEN_IN_FOCUSED_WINDOW;
dialog.getRootPane().registerKeyboardAction(e -> window.dispose(), k, w);
I had problems implementing both of the top answers. Here's a rather compact version using AbstractAction to auto-implement most of Action's methods, which works within text-based fields (per #pratikabu's request):
final AbstractAction escapeAction = new AbstractAction() {
private static final long serialVersionUID = 1L;
#Override
public void actionPerformed(ActionEvent ae) {
dispose();
}
};
getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "ESCAPE_KEY");
getRootPane().getActionMap().put("ESCAPE_KEY", escapeAction);
References
the above answers
http://www.coderanch.com/t/335357/GUI/java/KeyPressed-JDialog
Here's mine, I add CtrlW as closing shorcut aswell
Action closeAction = new AbstractAction(){
public void actionPerformed(ActionEvent e){
dispose();
}
};
KeyStroke esc = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0);
getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(esc, "closex");
getRootPane().getActionMap().put("closex", closeAction);
KeyStroke ctrlW = KeyStroke.getKeyStroke("control W");
getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ctrlW, "close");
getRootPane().getActionMap().put("close", closeAction);

Categories

Resources