Make JavaFX window active - javafx

I am trying to make a JavaFX application (running in the background to show up (set visible)) by a specific keystroke and to make the window the active window immediately. Therefore I set the primary stage alwaysOnTop-property true, call stage.toFront() and finally call stage.requestFocus(). Afterwards I request focus for a text field. When the window is made visible I would like to instantly start typing into the text field.
However, when I for example have a Ubuntu-terminal selected and make the window visible and start typing, the application is shown on top of everything, however, the typing goes to the terminal. The application window is not active! Nevertheless, the focused property of the stage is true. Is that a bug or am I missing something? Is it OS related?
Edit: I am willed to give my little hack-around for this problem that I am using at the moment, because the internet is suggesting, that a lot of people are facing this problem: Since I am working on a linux maschine I have access to the wonderful tool wmctrl. It is part of most standard repositories. wmctrl -a WINDOWNAME sets the window with the name WINDOWNAME active. For now, I simply call this tool from my source code when I need the window to be active. Since this is more of a dirty hack than any thing else, I sure want to get rid of it.

Not perfect, but it works:
Platform.runLater(() ->
{
//primaryStage.setAlwaysOnTop(true);
//primaryStage.setAlwaysOnTop(false);
primaryStage.setIconified(true);
primaryStage.setIconified(false);
});

If your node is not getting focused, try wrapping requestFocus() in a Runnable and call Platform.runLater():
final TextField text = new TextField();
Platform.runLater(new Runnable() {
#Override
public void run() {
text.requestFocus();
}
});

Related

Stop AltGr From Triggering Menu Bar Items?

Introduction
The first MenuItem inside my MenuBar receives focus whenever I press down my AltGr key. This is by no means a wanted behavior—although it appears as though it’s the default behavior offered by the MenuBar itself.
This gets slightly annoying since I’m on a Swedish keyboard—meaning both the [] and the {} are called upon using the AltGr key.
Problem
I’d like to remove the functionality whereas the first MenuItem inside the MenuBar gets activated upon pressing down AltGr on a keyboard.
Research
As usual I’ve been browsing around Stackoverflow in the hope to find an answer—but in vain. It’s honestly not very surprising that no one has had this problem before due to the majority of Stackoverflow not actually using Swedish keyboard layouts.
Moreover
Perhaps someone has either seen a post like this somewhere—in that case, do mark this as a duplicate—in any other case, either point me and anyone who might come across this question in the right direction, or simply answer this question with a somewhat shallow example.
Looking at MenuBarSkin's constructor, it adds a scene event key handler which focuses the menu if any keypress involving Alt is not consumed by the time it reaches the scene:
// put focus on the first menu when the alt key is pressed
scene.addEventHandler(KeyEvent.KEY_PRESSED, e -> {
if (e.isAltDown() && !e.isConsumed()) {
firstMenuRunnable.run();
}
});
I've worked around this by putting an event handler on the main content pane of my window, that looks for the key-pressed event of ALT_GRAPH and consumes it. Since my handler goes before the scene handler, it should prevent the menu-focus behaviour from triggering. Roughly:
tabPane.addEventFilter(KeyEvent.KEY_PRESSED, e -> {
if (e.getCode() == KeyCode.ALT_GRAPH)
{
e.consume();
return;
}
});
I'm not sure if AltGr always shows up as ALT_GRAPH; I believe I have seen it show up as ALT with e.isControlDown() being true, but you could also consume that event if none of your menu shortcuts involve Ctrl+Alt (which I'm guessing they won't, as they would be triggered by AltGr since it maps to Ctrl+Alt on Windows).
I had exactly the same problem and based on above answer I was able to solve it by using below code:
mainAnchorPane.addEventFilter(KeyEvent.KEY_PRESSED, e -> {
if (e.isAltDown() || KeyCode.ALT_GRAPH == e.getCode()) {
e.consume();
}
});
On my environment when I pressed AltGr then event reported that Ctrl and AltGr buttons were pressed in this exact order. I didn't care about Ctrl but Alt was anoying so I kill both of my Alts with above code.

JComboBox only appears where window was created

I have a very large database program that I am developing in IntelliJ IDEA, I am using the GridLayoutManager from IntelliJ and just became aware of a bug with my JComboBoxes, when I click on them they appear where the window was initially shown; If I move the window the combo box list stays in place if its not showing, else if it is showing it moves with the window, any ideas would be much appreciated
OSX 10.9
Java 8
IntelliJ Idea 16
This is the source for the dialog, IntelliJ handles creation of components
behind the scenes...
public DestinationSetupDialog(){
super(ResourceLoader.TITLE);
this.add(mainPanel);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.pack();
this.setVisible(true);
closeButton.addActionListener(e -> dispose());
saveButton.addActionListener(e -> saveData());
selectStartDate.setModel(Dates.populateDates(365).getModel());
}
private void saveData(){
}
Is this an issue with setting the model ? Because even newly created empty JComboBoxes do the same thing.

Program crashes with WebKit Previewer example with on_textEdit_textChanged()

I recreated the example of a webkit that displays the content of a textEdit containing HTML: http://qt-project.org/doc/qt-4.8/webkit-previewer.html
I changed it so rather than the webkit HTML being changed upon clicking the button, it's changed upon the text in the textEdit being changed:
// changed when button is click. Works fine.
void Previewer::on_previewButton_clicked()
{
// Update the contents in web viewer
QString text = htmlTextEdit->toPlainText();
webView->setHtml(text);
}
// change when text is changed. Crashes.
void Previewer::on_htmlTextEdit_textChanged()
{
// Update the contents in web viewer
QString text = "<html><body><h1>No crash!</h1></body></html>";
webView->setHtml(text);
}
This causes the program to crash as soon as it starts. I altered the program to run the function only a bit later (I thought maybe something needed to be initialized) but it still crashed once it reached the textChanged function. Why is it crashing? How can I fix this?
Your program is entering an infinite loop because, in the example, there's a connection between the webView's loadFinished(bool) signal and the text/html editor's updateTextEdit() slot.
Basically, editing the HTML causes the page to load again, which causes an update to the editor, which causes the page to load again, so on and so forth.
A quick way I solved this was to add a static bool flag to the updateTextEdit SLOT/function that only allows it to run once.
void MainWindow::updateTextEdit()
{
static bool once = false;
if (once) {
return;
}
once = true;
QWebFrame *mainFrame = centralWidget->webView->page()->mainFrame();
QString frameText = mainFrame->toHtml();
centralWidget->plainTextEdit->setPlainText(frameText);
}
Doing this worked for me, but your version might work differently than mine. I followed the example closely, but added an htmlchanged() slot to the previewer class, and made the connection like so:
connect(centralWidget->plainTextEdit, SIGNAL(textChanged()), centralWidget, SLOT(html_changed()));
Also, I'm no expert, but I'm pretty sure this is not the best way to get around this, and I assume that updateTextEdit() needs to run more than once. It'll work for the time being, though.

Adobe Flex PopUpManager -- multiple instances of a TitleWindow opened

Setup: My Flex application is one consisting of several "subapps". Basically, the main application area is an ApplicationControlBar with buttons for each of the subapps. The rest of the area is a canvas where the subapps are displayed. Only one subapp is visible at a time. When switching between subapps, we do a canvas.removeAllChildren(), then canvas.addChild(subAppSwitchedTo). It's essentially a manual implementation of a ViewStack (the pros and cons of which are not the topic of this, so refrain from commenting on this).
Problem: In one of my subapps (let's say subapp "A"), I have a search function where results are displayed in a TitleWindow that gets popped up. Workflow is like enter search criteria, click search button, TitleWindow pops up with results (multiple selection datagrid), choose desired result(s), click OK, popup goes away (PopUpManager.removePopUp), and continue working. This all works fine. The problem is if I switch to a different subapp (say "B" -- where A gets removeAllChildren()'d and B gets added), then switch back to A and search again, when the results TitleWindow pops open, there will be TWO stacked on top of each other. If I continue to navigate away and back to A, every time I search, there will be an additional popup in the "stack" of popups (one for each time A gets addChild()'d).
Has anyone else experienced this? I'm not sure what to do about it and it's causing a serious usability bug in my application. Does this ring any bells to anyone? It's like I somehow need to flush the PopUpManager or something (even though I'm correctly calling removePopUp() to remove the TitleWindow). Please help!
EDIT
Flex SDK = 4.5.1
// Subapp "A"
if (!certificateSearchTitleWindow)
{
certificateSearchTitleWindow = new CertificateSearchTitleWindow;
certificateSearchTitleWindow.addEventListener("searchAccept", searchOKPopupHandler);
certificateSearchTitleWindow.addEventListener("searchCancel", searchClosePopupHandler);
}
PopUpManager.addPopUp(certificateSearchTitleWindow, this, true);
My guess is that the popup is removed from the main display list when you remove its parent (this in the PopUpManager.addPopup() method), but not from its parent display list. Why don't you listen, in your subapps, to the Event.REMOVED event, and then remove your popup ? That would be :
private var pp:CertificateSearchTitleWindow;
private function onCreationComplete():void
{
addEventListener(Event.REMOVED, onRemovede);
}
private function addPopUp():void
{
if (!pp) {
pp = new CertificateSearchTitleWindow();
PopUpManager.addPopUp(pp, this, true);
}
}
private function onRemoved(event:Event):void
{
if (pp) {
PopupManager.removePopUp(pp);
pp = null;
}
}
Thank you to those who gave suggestions. It turned out I was re-registering an eventListener over and over.
I am using a singleton to act as "shared memory" between the subapps. I was setting singleton.addEventListener(someType, listener) in subapp A's creationComplete callback. So everytime I navigated back to A, the creationComplete was running and re-adding this listener. After the search, the listener method (that opened the popup) was being called multiple times, i.e., as many times as the event had been added.
xref: http://forums.adobe.com/message/3941163

Main application window and a dialog interaction in Qt

Good day!
Have a problem: main window (MyApp for example) works in background (behind all other windows or in tray), not necessary to show it without need. After some period of time some reminding StayOnTop dialog appears (having parent = 0, to be not tied to main window) and asks for some user interactions. After dialog closes I’d like to keep an application window user currently working with active, and user continue do his job not switching to MyApp. However, instead of above behaviour, main MyApp window appears and user forces to switch back to his window (job) – inconvenient.
How to prevent MyApp main window appearing after closing the dialog? Need to install some event filter or access OS API? Problem exists in Mac, Windows, Linux.
You could try to re-implement the main window's showEvent and ignore that event, in case other windows are visible.
void main_window::showEvent( QShowEvent* e )
{
if( /*one or more of its children are visible */ )
{
// nothing to do
}
else
{
QMainWindow::showEvent( e );
}
}
Maybe just try invoking hide method after dialog call? Other possibility - try setting this:
http://doc.qt.io/qt-4.8/qwidget.html#windowFlags-prop to Qt::Popup.

Resources