How can I block input from keyboard and mouse when my Java application is running.
Like we block input in AutoIt with BlockInput(1) , I also want to do same in Java.
I can only answer for a swing application. For your main frame you should have this
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
//or if you are using awt
Frame frame = new Frame();
frame.addWindowListener(new WindowAdapter() {
// handle the window closing event here
});
You can try This - JNativeHook, a global keyboard & mouse listener library for Java.
Create insance of transparent window (Window, not Frame or JFrame) and put it exactly on your application's window. User will not see anything (because it is transparent). But mouse and keyboard events will arrive to this transparent window.
Tip: the window should not be 100% transparent. In this case this trick does not work. put opacity 0.01.
Related
My Goal:
I am trying to make a program which will log me into my zoom classes at the correct time. I have figured out everything except trying to open zoom, enter the ID and passcode correctly, and logging in to the class.
The Problem:
There is no API which I can use to achieve my goal. There is a certain Zoom API, but it is paid, and I am a minor. The only option I think is left is to make java click and enter text anywhere outside a JFrame.
I tried using java.awt.Robot for this, but it can only click on a button inside the JFrame. I tried overlapping the two buttons and keeping the zoom window focused, but Java changes the focus to the JFrame.
If there is any other alternative in Java, or if there is any other external Library or API that may come in use, please suggest me the same.
Thank you in advance.
I tried using java.awt.Robot for this, but it can only click on a button inside the JFrame.
The Robot can be used to generate an OS level event.
If you want the Robot to click out side the frame then you need to move the mouse outside the bounds of the frame.
Simple example:
public static void main(String[] args) throws Exception
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setLocation(25, 25);
frame.setSize(100, 100);
frame.setVisible(true);
Robot robot = new Robot();
robot.delay(2000);
robot.mouseMove(15, 15 );
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}
The Robot will click the system menu of the window found at the top/left corner of your desktop. So make sure you have a window maximized.
You could use WindowListener,#windowDeactivated or WindowFocusListener#windowLostFocus, but you have to be careful with owned JDialog's (as you have to be with FocusListener anyway)
For debug purposes, I need to draw image on the screen in a simple window.
Swing handles all it's events in a separate message loop thread. That means that if I do the following:
while(true) {
//Get screenshot
BufferedImage screenshot = MSWindow.screenshot();
//Create JFrame
JFrame frame = new JFrame();
Container main = frame.getContentPane();
//This layout should force the JLabel as large as the window, am I right?
main.setLayout(new GridLayout(1,1));
//Create JLabel to display the screenshot
JLabel label = new JLabel(new ImageIcon(screenshot));
main.add(label);
frame.pack();
frame.setVisible(true);
//Delay is allways good when meddling up with dangerous things
Thread.sleep(2000);
}
... I end up with many many JFrames.
I used to use JDialog which is blocking and stops thread until you press OK:
JOptionPane.showMessageDialog(null, scrollPane, message, javax.swing.JOptionPane.INFORMATION_MESSAGE);
This has a flaw though - you can't see the debug window on the taskbar. Sometimes, it's hard to find where the window ended up. This is why I want to switch to JFrame.
My question is straight up this: How to make current thread wait until JFrame is closed?
How to create JFrame in the same thread so that it blocks?
Use a modal JDialog.
JOptionPane.showMessageDialog(null, scrollPane, message, javax.swing.JOptionPane.INFORMATION_MESSAGE);
This has a flaw though - you can't see the debug window on the taskbar.
Don't use null for the dialog owner. Make sure you specify the owner JFrame. Whenever you click on the taskbar icon the frame and child dialog will both show up.
Build your GUI outside the loop once and then use
// read new component data
screenshot = MSWindow.screenshot();
// modify components
label = new JLabel(new ImageIcon(screenshot));
// force the frame to repaint its chilfren
frame.revalidate();
frame.pack();
to "refresh" it inside the loop.
JFrame with hidden or no maximize button but should be able to re-size using mouse(clicking and dragging on jFrame border). setResizable(false) is only disabling the minimize button but not able to re-size using mouse.
I personally can't think of a reason to allow resize and not allow maximize but here is an example of how to prevent maximizing a JFrame while still allowing resize and minimize. Tested in windows, untested on all other platforms. Full screen flash is minimized using setMaximizedBounds().
final JFrame jFrameNoMax = new JFrame() {
{
setMaximizedBounds(new Rectangle(0, 0));
addWindowStateListener(new WindowStateListener() {
public void windowStateChanged(final WindowEvent e) {
if (e.getNewState() == MAXIMIZED_BOTH) {
setExtendedState(NORMAL);
}
}
});
}
};
// Tester
jFrameNoMax.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jFrameNoMax.setSize(300, 300);
jFrameNoMax.setLocation(300, 300);
jFrameNoMax.setVisible(true);
You can take the following steps:
-Right click on JFrame
-Select properties
-Uncheck the resizable checkbox
-Close properties
-Run the program
See the attached illustration:
One option could be to use a JDialog instead of a JFrame. This allows the window to be manually resizeable but not maximizeable. The only problem with doing this is that you lose both the minimize and maximize buttons. This may or may not be a problem for your application.
To be honest, I didn't quite know how to express my question in the title. So if someone has a clearer idea of what i'm trying to ask, then please be so kind as to edit it, for the greater good of mankind.
I have the following code:
public class Main {
public static void main(String[] args) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
JFrame frame = new JFrame();
Window window = new Window(frame);
JButton btn = new JButton("Quit");
window.add(btn);
if(gd.isFullScreenSupported())
gd.setFullScreenWindow(window);
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
gd.setFullScreenWindow(null);
}
});
}
}
What I want to do is make a library system, illustrated in full screen mode. Inside the full screen mode, there should be simple frame windows with text fields in them and so on... No heavy 2D/3D graphics required here...
My problem is, the quit button is resized to fit the entire screen. I've tried to use setBounds, setSize, etc, on the frame, window and even button... But it doesn't seem to let me be able to center the button in the middle.
So my question: Is it possible to make JFrame application windows inside a JFrame application window which is set to full screen exclusive mode? Or is it only possible to use full screen exclusive mode together with 2D/3D methods like drawing?
Note: The only thing I intend to use full screen mode for is to set it to a black background, and have a Jframe window ontop of this black background in the center of the application. It is to give the illusion that the application is actually a system.
Thank you.
P.S. please note that the quit button was merely for my own testing purposes. It won't be part of the actual system, so my question does not revolve around how to resize this particular quit button.
Fullscreen Exclusive Mode is not meant to have Swing components inside. It is meant as a high performance way to draw graphics in Java (and can benefit as much as possible of underlaying hardware if done right).
What you want to do could be done by using an undecorated JDesktopPane and maximize it over the dimensions of the screen. Then proceed with using a JInternalFrame to have a JFrame like window inside that JDesktopPane.
I've included links to the respective API sections, they should get you started.
I think that what you are after is an MDI Application. If that is what you are after you could take a look here.
Your problem is that you do not use layout manager correctly.
Learn about layout managers - the modules that put and size/resize the visual components on screen. Then decide how do you want your button to look like and then decide which layout manager to use. It will do its job.
You know what i also had the exact same problem and the only thing i know from my experience is that you can use jinternal frame and set its property undecorated to true then add your custom title bar according to your requirement.
I have a JFrame full of various components. When I run it, only a small portion appears. I have to resize the window (drag the edge with cursor) to make everything appear. Any idea how to fix this?
Call the pack() method on your frame before making it visible:
JFrame f = new JFrame();
...
f.pack();
f.setVisible(true);