I'm trying to figure out a way to make a Java application invisible to the user.
Basically just trying to remove this
<- Image
How can this be done?
public class TransparentWindow extends JFrame {
public TransparentWindow() {
initComponents();
}
#SuppressWarnings("unchecked")
private void initComponents() {
setExtendedState(Frame.MAXIMIZED_BOTH);
setResizable(false);
setUndecorated(true);
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
setAlwaysOnTop(true);
System.setProperty("sun.java2d.noddraw", "true");
WindowUtils.setWindowTransparent(this, true);
WindowUtils.setWindowAlpha(this, 0.6f);
}
public static void main(String[] args) {
new TransparentWindow().setVisible(true);
}
}
I just seems to have found the answer, just put the line setVisible(false); into comments and you will see the actual program, UNCOMMENT the line to see no trace is left, as far as I can see, that the Java Program is running somewhere, until you won't add the icon to your system tray, manually. Moreover how to remove your Application from Task Manager that question still remains, though you can remove the said icon, as pointed by you in your question.
import javax.swing.*;
public class TransparentWindow extends JFrame
{
public TransparentWindow()
{
initComponents();
}
#SuppressWarnings("unchecked")
private void initComponents()
{
setExtendedState(JFrame.MAXIMIZED_BOTH);
setResizable(false);
setUndecorated(true);
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
setAlwaysOnTop(true);
setOpacity(0.8f);
setSize(200, 200);
//System.setProperty("sun.java2d.noddraw", "true");
//WindowUtils.setWindowTransparent(this, true);
//WindowUtils.setWindowAlpha(this, 0.6f);
setVisible(true);
setVisible(false);
JOptionPane.showMessageDialog(this, "It is working!", "Guess : ", JOptionPane.INFORMATION_MESSAGE);
}
public static void main(String[] args)
{
TransparentWindow tw = new TransparentWindow();
}
}
Here is a snapshot of my desktop on running this program, see the taskbar
Extend from JWindow insted of JFrame. (I did not test this on Windows 7 as I don't sit in front of a Windows box right now. It worked for XP and works for Unity, which surprised me.)
As far as I know, there is no way to remove the task bar icon.
Related
I am trying to create a simple program using JInternalFrame on Swing and when i run my code, it suddenly produces a blue background. Can anyone tell me how i can remove it?
here is the code i tried
import javax.swing.*;
public class Main extends JFrame {
JDesktopPane dp = new JDesktopPane();
JInternalFrame intf = new JInternalFrame("demo");
public void initialize() {
setTitle("Test Program");
setSize(500, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public Main() {
intf.setSize(150, 200);
intf.setVisible(true);
dp.add(intf);
add(dp);
initialize();
}
public static void main(String args[]) {
new Main();
}
}
It's part of the PL&F.
To literally remove it you can make the JDesktopPane non-opaque:
dp.setOpaque(false);
Or set the background to a colour that you like:
dp.setBackground(new java.awt.Color(200,200,200));
But it looks weird with a light colour.
There's probably someway of configuring the macOS PL&F. All just live with macOS looking like macOS wants to look.
I solved the problem. I was using outdated code later in the program.
I have changed the Title to "Sign Up", but it still says "login" at the top from when I previously had it at that. Also the size of the window is 300,300 not 500,1500. This is just the part of the code with this problem. I use eclipse.
SignUpScreen()
{
super("No Layout Manager");
setLayout(null);
setTitle("Sign Up");
setSize(500,1500);
show();
}
Without seeing all of your code, I can only venture a guess. But from experience, the issue might be the following line:
setLayout(null);
Try not to use null layouts.
I'm assuming that you're working in Swing and trying to extend a JFrame? Here is a simple program that does this:
package com.example;
import javax.swing.JFrame;
public class HelloWorldSwing {
public static void main(String[] args) {
HelloWorldSwing helloWorldSwing = new HelloWorldSwing();
helloWorldSwing.execute();
}
private void execute() {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SignUpScreen();;
}
});
}
private class SignUpScreen extends JFrame {
private static final long serialVersionUID = 1L;
SignUpScreen() {
super("No Layout Manager");
setLayout(null);
// setTitle("Login");
setTitle("Sign Up");
// setSize(300, 300);
setSize(500, 1500);
show();
}
}
}
Changing to the commented-out setTitle and setSize lines changes the title and size of the window for me, on Save. If you paste this code into your environment, and it doesn't change on Save, check Project -> Build Automatically as mentioned in the comments.
I've just started learning Swing/JFrame, basically making a GUI.
I've been doing Java for a month now, just using the console, making a sin/true or false games and it is pretty easy for me now.
I decided to take a further step, and I must say it's totally a pain, different logic.
That's what I've done so far:
Main.java:
import java.awt.*;
import javax.swing.*;
import java.io.*;
class Main {
public static void main(String[] args) {
final Gui gui = new Gui();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
gui.createMyGui();
}
});
}
}
gui.java
class Gui {
protected JFrame j = new JFrame("My First window");
protected JPanel p = new JPanel();
protected Container c;
public Gui() {
j.setSize(500, 400);
p.setSize(j.getSize());
this.c = j.getContentPane();
}
public void createMyGui() {
setButtons();
setGuiBackground();
j.setVisible(true);
p.setVisible(true);
this.c.add(p);
}
private void setGuiBackground() {
this.c.setBackground(Color.green);
}
private void setButtons() {
p.add(new JButton("Hey"));
}
}
Problem
I can't really get the button to show up, people are telling me to use setBounds but I am not really sure on how to start as I can't even place a button there. I've tried searching about my problem, but no luck actually.
Basically what happens is a 500x400 green GUI opens, and that's it.
Why won't the button show?
people are telling me to use setBounds
Dont! Layout managers are the correct way to go.
Your problem is you add your buttons to the "p" panel, but you never add it (p panel) to the contentPane
I made a game that is based in a jpanel. When I add the jpanel to a jframe, it works fine on both pc's and macs.
here is the class where I add the jpanel to the jframe:
import javax.swing.JFrame;
public class Start{
public static void main(String[] args){
JFrame f = new JFrame("Rocks");
f.setSize(600,500);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Board b = new Board();
f.add(b);
f.setVisible(true);
}
}
However, when I add this jpanel to a japplet, it still works perfectly on pc's, but not on macs. In the eclipse applet tester, the first screen paints, but it doesn't accept keyboard input to start the game. In any browser/html page though, the game doesn't seem to load at all, and when I open the java console of chrome I see no errors.
here is the class where I add the jpanel to the japplet
import javax.swing.JApplet;
public class rockAppletStart extends JApplet{
public void init(){
Board b;
b = new Board();
add(b);
b.focus();
}
public void start(){}
public void stop(){}
public void destroy(){}
}
I would appreciate any help that could be offered, and I'm willing to provide more information if necessary. I could even provide the other classes of the game, but there very long and messy, and I'd rather not unless necessary.
The applet version of the game can be found here at gamejolt.com, if you feel like testing it out. If you have a pc, it should work fine, but with a mac it won't.
** edit **
here you can download the .jar file of all the classes and resources. Feel free to use the files to test out your solution yourself if you want... Otherwise, Ill have access to a mac on Tuesday and I will test all solutions then.
http://dl.dropbox.com/u/18832480/Rocks_Source_file.jar
This is just a SWAG, but since Swing threading issues can often cause pernicious, unpredictable and hard to detect errors, what if you create your applet in a thread-safe manner? i.e.,
public void init() {
try {
javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createGUI();
}
});
} catch (Exception e) {
System.err.println("createGUI didn't successfully complete");
}
}
private void createGUI() {
Board b;
b = new Board();
getContentPane().add(b);
b.focus()
}
The JApplet is stealing the focus from the Board. To prevent it, add the following to the end of your init() method:
setFocusable(false);
I would like to create a JFrame with two specifal features:
JFrame should not grab focus while maximized from minimized state.
When a JFrame created or became maximized from minimized state, it should flash in the Windows bar until a user will grant a focus to it. (like as in ICQ clients ).
Does anybody know how the second requirement can be implemented?
Little self-explained example:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class JFrameTest {
private static JFrame childFrame;
public static Container getParentContentPane() {
JPanel panel = new JPanel();
JButton button = new JButton("Create\\Restore child frame");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
createOrRestoreChildFrame();
}
});
panel.add(button);
return panel;
}
private static void createOrRestoreChildFrame() {
if (childFrame == null) {
childFrame = new JFrame("Child Frame");
childFrame.setLocation(200, 200);
childFrame.add(new JLabel("Child Frame"));
childFrame.pack();
setChildFrameVisible();
} else {
setChildFrameVisible();
}
}
private static void setChildFrameVisible() {
childFrame.setFocusableWindowState(false);
childFrame.setVisible(true);
flashInWindowsBar(childFrame);
childFrame.toFront();
childFrame.setFocusableWindowState(true);
}
/**
* Should Make child frame flash in Windows bar.
* Currently, it does not work for me.
* Could anybody help me to fix this please? )
*/
private static void flashInWindowsBar(JFrame childFrame) {
childFrame.setState(JFrame.ICONIFIED);
childFrame.toFront();
}
private static void createAndShowGUI() {
JFrame parentFrame = new JFrame("JFrame Demo");
parentFrame.setLocation(100, 100);
parentFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
parentFrame.setContentPane(getParentContentPane());
parentFrame.pack();
parentFrame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Thanks!
The following code worked for me exactly as you described:
f.setState(JFrame.ICONIFIED);
f.toFront();
f is a JFrame.
Unfortunately, this isn't something that you can do natively under any Java platform. Anyone who manages to get it working by using the kind of 'trickery' that you've shown, will be disappointed to find that it is unlikely to work on another version of Windows, or even another computer with the same version of Windows. The only times I've ever seen a Java window flash is due to some glitch in Swing when minimizing all windows to the taskbar.
As this article on making Java applications feel native shows, it's the same on Mac OS.
Your best bet is to use the techniques described in the above article to make a JNI which does the Windows API call, or get a license for JNIWrapper (search for it) which does it all for you (best option if you are making a commercial app, or making it for a client who is willing to pay for such a feature). It looks like you can get a 30-day trial for that.
The only other thing I could suggest is create a poor-man's equivalent of a pop-up notification system. When you want to alert the user, create a Frame without a border, put it in the bottom-right corner of the screen, make it non-focusable and show it for a brief period of time.
The JPanel does not flash. Try it instead of JFrame.