javafx - how to bind the DialogEvent.DIALOG_SHOWN on a Dialog? - javafx

On a Stage I can do the following:
getScene().addEventHandler(WindowEvent.WINDOW_SHOWN, s -> {});
On Dialog, the following DOES NOT WORK:
getDialogPane().getScene().addEventHandler(WindowEvent.WINDOW_SHOWN, s -> {});
getDialogPane().getScene().addEventHandler(DialogEvent.DIALOG_SHOWN, s -> {});
This works:
setOnShown(e -> {});
But I want do use the addEventHandler so other events can be added.
How can I archieve this?

This works for me.
getDialogPane().getScene().getWindow().addEventHandler(WindowEvent.WINDOW_SHOWN,
s -> System.out.println("hello"));

Related

how to bind a command to 'Restart and Run All'?

I have recent build of Jupyter than has a menu action allowing you to Restart & Run All:
I would like to add a keyboard shortcut that binds to this action. I have seen the documentation for keyboard customization, but I'm still unsure how to add a keyboard shortcut.
I've built Juypter from source, so based on the help, it would appear that I need to add some code to notebook/static/custom/custom.js.
I've tried adding the following:
IPython.keyboard_manager.command_shortcuts.add_shortcut('meta-r', function (event) {
IPython.notebook.restart_kernel();
IPython.notebook.execute_run_all();
return false;
});
However, when I press [Meta-r], the kernel seems to restart but execute_run_all() does not get executed.
As of Jupyter Notebook 5.0, you can now create and edit keyboard shortcuts directly in menu options. As of now, it's Help -> Edit Keyboard Shortcuts. There's a guide at the bottom of that dialogue box.
The docs on it are here:
http://jupyter-notebook.readthedocs.io/en/latest/examples/Notebook/Custom%20Keyboard%20Shortcuts.html
Here's what I have in my custom.js. For it to work, the shortcut addition has to happen after the app is initialized:
$([Jupyter.events]).on("app_initialized.NotebookApp", function () {
Jupyter.keyboard_manager.command_shortcuts.add_shortcut('0,1', {
help: 'restart and run all',
help_index: '0,1',
handler: function (event) {
Jupyter.notebook.kernel.restart();
restartTime = 2000 // decrease this if you have a fast computer
setTimeout(function(){ Jupyter.notebook.execute_all_cells(); }, restartTime);
return false;
}}
);
});
Just in case someone stumbles upon this post looking for the same answer: you need to wait for the kernel to restart with a timeout before executing.
See this discussion on GitHub.
In your case, it would give:
IPython.keyboard_manager.command_shortcuts.add_shortcut('meta-r',
function (event) {
IPython.notebook.kernel.restart();
setTimeout(function(){ IPython.notebook.execute_all_cells(); }, 1000);
return false;
});

Is there any way to insert a callback before/after a templates context is updated?

I'm aware of Template.onRendered, however I have the need to destroy and setup some plugins that act on the dom when the actual context is updated.
So saying I have content template, I'd need something similar to the following:
Template.content.onBeforeChange(function () {
$(".editor").editable("destroy");
});
Template.content.onAfterChange(function () {
$(".editor").editable();
});
Is there any current way I can achieve this with the existing Template api?
You should be able to detect a context change within a template autorun by looking at currentData like this:
Template.content.onRendered(function() {
this.autorun(function() {
if (Template.currentData()) {
// the context just changed - insert code here
}
});
});
I'm unclear if that works for your particular case because this technique only gets you the equivalent of onAfterChange.

With Meteor's iron-router, how do you re-render the template from a template event?

My template shows different content based on a non-reactive value (in localStorage). I would like to do the following:
Template.foo.events
'click #link': ->
localStorage.setItem 'key', 'different'
// re-render template foo
this.render() is undefined. Router.render('foo') does nothing.
The easiest way is to use a dependency tied to your value.
keyDep = new Deps.Dependency()
Template.foo.events
'click #link': ->
localStorage.setItem 'key', 'different'
keyDep.changed()
Template.foo.key = ->
keyDep.depend()
return localStorage.getItem 'key'

Dojo on query click doesn't work in toolbar on toggle button

Following the example here I've tried doing the same but the query doesnt work for me.
http://jsfiddle.net/qDbd5/
require(["dojo/parser", "dijit/Toolbar", "dijit/form/ToggleButton", "dojo/query", "dojo/dom-class", "dojo/on", "dojo/domReady!"], function (parser, ToolBar, ToggleButton, query, domClass, on) {
on(query(".dijitToggleButton"), "click", function (e) {
query(".dijitToggleButton").forEach(function (node) {
console.log('Captured clicked event');
domClass.remove(node, "dijitToggleButtonChecked dijitToggleButtonRtlChecked dijitRtlChecked dijitChecked");
});
domClass.add(this, "dijitToggleButtonChecked dijitToggleButtonRtlChecked dijitRtlChecked dijitChecked");
});
});
I'm trying to make only 1 button toggle at a time.
Why the click event not triggered?
Wrapping everything in a ready() callback solves the problem.
See http://jsfiddle.net/cFQGq/
require(["dojo/ready", "dojo/parser", "dijit/Toolbar", "dijit/form/ToggleButton", "dojo/query", "dojo/dom-class", "dojo/on", "dojo/domReady!"], function (ready, parser, ToolBar, ToggleButton, query, domClass, on) {
ready(function() {
on(query(".dijitToggleButton"), "click", function (e) {
query(".dijitToggleButton").forEach(function (node) {
console.log('Captured clicked event');
domClass.remove(node, "dijitToggleButtonChecked dijitToggleButtonRtlChecked dijitRtlChecked dijitChecked");
});
domClass.add(this, "dijitToggleButtonChecked dijitToggleButtonRtlChecked dijitRtlChecked dijitChecked");
});
});
});
If you look at the explanation of domReady!, it mentions that it is insufficient for working with dojo widgets, because it executes after the DOM has loaded instead of after the widget has finished initializing. dojo/ready executes the callback after the widgets have finished loading.

I can't get my added element to activate a function

I am trying to add a link to the colorbox interface as it is created. It will eventually link to the currently displayed image file but for now I just want to get the console.log to work. It adds the link correctly (#print-one) but I can't get a function to run when the link is clicked. Any help would be much appreciated!
$(document).bind('cbox_complete', function () {
// Show close button with a delay.
$('#cboxClose').css('opacity', 1);
// Add Print Button
if ($('#print-one').length ) {
// Do Nothing
}else {
$('#cboxNext').after('Print');
}
});
$('#print-one').click(function() {
console.log('Works');
});
This is all wrapped inside the $(document).ready function. I just can't get the console log to work when the link is clicked. I have been beating my head against a wall trying to figure it out. Thanks for any help!
You need to delegate the event.
$(document).on('click', '#print-one', function(){});
It seems that the link is added just after cbox is initialized, which may not be ready when the compiler gets to the click binding function a few lines below.
Try this:
...
// Add Print Button
if ($('#print-one').length ) {
// Do Nothing
}else {
$('Print').insertAfter('#cboxNext').click(function() {
console.log('Works');
});
}

Resources