App window on top of all windows including others app windows
Try to use this
self.windowController?.showWindow(nil)
self.makeKeyAndOrderFront(self)
NSApp.activate(ignoringOtherApps: true)
NSPanel is always on top of all other app windows
You do not pass nil
, Nil
, 0, or NULL
to didEndSelector
. Ever. You pass a selector to a method (-sheetDidClose:returnCode:contextInfo:
) that you have declared. That you are passing something other than a selector makes whatever else you're doing to dismiss the sheet suspect. (You haven't said if the panel stays on top because it's never dismissed, but that's my assumption here.)
Take a look at Using Custom Sheets, which is Apple's own documentation on the subject.
Also, be sure that the panel you are using has its "Visible At Launch" flag turned off in IB.
Visual Basic .Net, keep windows on top of other on-top windows
Apparently the solution was pretty simple.
I was afraid of side-effects, but the solution I applied works perfectly.
I added a timer, enabled it, and every tick it simply does:
me.Topmost = true
If I now click the taskbar, the program is obviously pushed to the back, but every 100ms when the timer ticks, the program is automatically put back on top without the app actually receiving focus.
How to make window appear on top of all other windows?
Thanks to the answers of Mike and Sheridan, I found the solution to be simply activating the window in the thread it is created in. So
var shell = (Shell) Shell;
Application.Current.MainWindow = shell;
shell.Topmost = true;
shell.Show();
shell.InjectInitialViews();
shell.Activate();
does the trick.
Keeping Window OnTop of all other windows C/Java
Short answer: Not really, no.
Longer answer: A "full-screen" app uses the display in a different way than a normal Windows GUI app. Exactly how they work depends on the application and the OS; most will leverage the DirectX or OpenGL APIs which allow applications to directly manipulate the memory representing the screen, as well as access the accelerated capabilities of the GPU. These apps are supported by basically creating a GUI "window" (more like a panel) that covers the entire primary display and is "always on top", preventing the desktop elements from ever being painted as they're always behind the full-screen app's window in the Z-order. The application then gets "relaxed" access to the memory representing the display rectangle of that GUI window, so it can manipulate individual pixels without needing to use the message loop to repaint that area.
In this situation, the display is being painted when the application wants (which is virtually always either "as fast as possible" or "synchronized with the next monitor scan"), NOT when the Windows GUI thinks it's a good time. So, anything that IS painted when Windows thinks it's a good idea will flicker; Windows paints your "always on top" window over the app's "window" in the GUI's Z-order, then the app paints back over the window by drawing directly onto its rectangle. That causes Windows to invalidate and redraw your window, and the cycle continues.
The solution is not only to make an "always-on-top" window, but to somehow programmatically "task-switch" from the full-screen app's window to yours. This may require your app to have privileges that most managed runtimes can't or won't grant. If it's possible, then when it happens the full-screen app will minimize (which may or may not be a HUGE problem for your users; whatever your app is trying to tell me, it will almost certainly NOT be worth minimizing my StarCraft 2 session in the middle of an online melee).
Window visible on all spaces (including other fullscreen apps)
If you want a window to be visible on top of other fullscreen windows (in spaces), you should make an agent (accessory) application.
You can do it by setting LSUIElement
key in the application’s Info.plist
to 1 (YES)
If you still need a regular application you can do following:
Create a separate agent (helper) application inside your main application bundle which will show your window. (There are plenty of good examples on how you can create such application)
Play with
NSApplicationActivationPolicy
. You can try to change application's activation policy during a runtime.
Swift 3:NSApp.setActivationPolicy(.accessory)
switch to agent(accessory)NSApp.setActivationPolicy(.regular)
switch to ordinary application
Keep in mind that .accessory
policy hides the icon from Dock and you still need the code you have already:
window.collectionBehavior = .canJoinAllSpaces
window.level = Int(CGWindowLevelForKey(.floatingWindow))
How to call launch() more than once in java
You can't call launch()
on a JavaFX application more than once, it's not allowed.
From the javadoc:
It must not be called more than once or an exception will be thrown.
Suggestion for showing a window periodically
- Just call
Application.launch()
once. - Keep the JavaFX runtime running in the background using
Platform.setImplicitExit(false)
, so that JavaFX does not shutdown automatically when you hide the last application window. - The next time you need another window, wrap the window
show()
call inPlatform.runLater()
, so that the call gets executed on the JavaFX application thread.
For a short summary implementation of this approach:
- See the answer by sergioFC
If you are mixing Swing you can use a JFXPanel instead of an Application, but the usage pattern will be similar to that outlined above.
- For an example of the JFXPanel apprach, see Irshad Babar
s answer.
Wumpus Sample
This example is bit more complicated than it needs to be because it also involves timer tasks. However it does provide a complete stand-alone example, which might help sometimes.
import javafx.animation.PauseTransition;
import javafx.application.*;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.util.*;
// hunt the Wumpus....
public class Wumpus extends Application {
private static final Insets SAFETY_ZONE = new Insets(10);
private Label cowerInFear = new Label();
private Stage mainStage;
@Override
public void start(final Stage stage) {
// wumpus rulez
mainStage = stage;
mainStage.setAlwaysOnTop(true);
// the wumpus doesn't leave when the last stage is hidden.
Platform.setImplicitExit(false);
// the savage Wumpus will attack
// in the background when we least expect
// (at regular intervals ;-).
Timer timer = new Timer();
timer.schedule(new WumpusAttack(), 0, 5_000);
// every time we cower in fear
// from the last savage attack
// the wumpus will hide two seconds later.
cowerInFear.setPadding(SAFETY_ZONE);
cowerInFear.textProperty().addListener((observable, oldValue, newValue) -> {
PauseTransition pause = new PauseTransition(
Duration.seconds(2)
);
pause.setOnFinished(event -> stage.hide());
pause.play();
});
// when we just can't take it anymore,
// a simple click will quiet the Wumpus,
// but you have to be quick...
cowerInFear.setOnMouseClicked(event -> {
timer.cancel();
Platform.exit();
});
stage.setScene(new Scene(cowerInFear));
}
// it's so scary...
public class WumpusAttack extends TimerTask {
private String[] attacks = {
"hugs you",
"reads you a bedtime story",
"sings you a lullaby",
"puts you to sleep"
};
// the restaurant at the end of the universe.
private Random random = new Random(42);
@Override
public void run() {
// use runlater when we mess with the scene graph,
// so we don't cross the streams, as that would be bad.
Platform.runLater(() -> {
cowerInFear.setText("The Wumpus " + nextAttack() + "!");
mainStage.sizeToScene();
mainStage.show();
});
}
private String nextAttack() {
return attacks[random.nextInt(attacks.length)];
}
}
public static void main(String[] args) {
launch(args);
}
}
Update, Jan 2020
Java 9 added a new feature called Platform.startup()
, which you can use to trigger startup of the JavaFX runtime without defining a class derived from Application
and calling launch()
on it. Platform.startup()
has similar restrictions to the launch()
method (you cannot call Platform.startup()
more than once), so the elements of how it can be applied is similar to the launch()
discussion and Wumpus example in this answer.
For a demonstration on how Platform.startup()
can be used, see Fabian's answer to How to achieve JavaFX and non-JavaFX interaction?
Related Topics
"Ambiguous Use of 'Children'" When Trying to Use Nstreecontroller.Arrangedobjects in Swift 3.0
Parse.Com Pfquery Order by Time (Swift)
Moving an Object Across the Screen at a Certain Speed.(Sprite Kit)
iOS 13 Swiftui: App Crashes Upon Launch on Real Device
Elegant 'Bounded' Methodology in Swift
How to Pass Arguments into a Function with Completion Swift
How to Notify a Queue in Swift (Gcd)
How to Convert [Any] to Nsarray
Var Declaration with Type VS Without
Simple Swift Class Does Not Compile
Error While Using Property 'Cgrectgetwidth', It Says It Was Replaced with 'Cgrect.Width'
How to Have Either or Wherefields for Firestore Query
Getch() Equivalent in Swift: Read a Single Character from Stdin Without a Newline
How to Make a Function with a Loop Asynchronous in Swift