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.
Related
jframe title bar image (run time)
I don't want it undecorated, want black color and change icons.
I am making my college project, I tried making a customized title bar with an undecorated frame, and it works fine, but I have problems dragging the frame and re-sizing it. So I thought maybe I could edit the original frame but didn't find any solutions. Can someone guide me through this?
Assumption : You need a by-default decorated JFrame, which acquires the look & feel of the machine's operating system.
Setting an icon and customizing text on Title bar is easy.It can be done as shown below,
class TitleBar {
TitleBar(){
Frame f=new Frame();
//Setting TitleBar icon with "YourImage.png"
f.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("YourImage.png")));
//set other parameters as per your choice viz size layout etc.
//Set the Title Bar text
f.setTitle("YOUR CUSTOM TEXT");
f.setVisible(true);
}
Now with the frame decoration turned "ON", it is not possible to change the color of titlebar, font of the top-level JFrame (to the best of my knowledge). You CAN change the color of an InternalFrame however.
If you want to customise these then turn off windows decorations with f.setUndecorated(true) and you can change the title bar appearance with the help of LAF (Look And Feel). This is better explained in the answer below :
Change the color of the title bar in javax.swing
You may also try modifying the UIDefaults as shown here :
Modifying UIDefaults | For Windows 10 and above
Do upvote if my answer is useful !!
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)
I have searched for a solution to this but can't seem to find it.
If this has already been answered please link me to the answer. I am creating a JFrame setting it to full screen and changing the display mode.
This all works fine and the display mode is supported by my graphics card (I have already checked) so that is all ok. I am adding a canvas to the JFrame, creating a buffer strategy and using it to draw custom graphics to the screen. The issue is that when the display mode changes and the JFrame enters full screen mode it doesn't cover the full screen. This doesn't make sense as the display mode is 1024 x 768 and the canvas size is also 1024 x 768.
Instead the canvas sits in the top left corner of the screen in the same proportions as when the display mode wasn't changed. Furthermore, the graphics the don't draw properly. Everything works fine when I don't try to change the display mode and set full screen.
Here are the three lines of code that sets everything to full screen.
gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
gd.setFullScreenWindow(this);
gd.setDisplayMode(new DisplayMode(1024,768,32,48));
I am running windows 10 and my normal display mode is 3840x2160. Here is a screenshot of what happens when I run the code.
Although it doesnt look like it the display mode definitely changes as the screen goes black and then the screen becomes smaller and more blured.
Have you tried to maximize the window using setExtendedState? (Like in below example)
import javax.swing.JFrame;
public class MaximizedFrame
{
public static void main(String[] args)
{
JFrame frame = new JFrame("My Frame");
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
}
I'm struggling with two Java GUI things at the moment.
This is the situation: I'm designing a word game using Swing components. I have a main JFrame where everything is placed (my GUI class extends JFrame). There are two things I want to do:
1st: I would like to set an image as the background image of the main frame, it has to be displayed behind all components. i've searched around but haven't found a working solution. I tried making an extended BackGroundPanel class but when I create an instance of BackGroundPanel I have no idea how to make it the background of the frame... I also haven't find a good way to load in an image from an 'images' directory in my src folder...
2nd: when the program starts the user is greeted with an undecorated JDialog, the main frame needs to be disabled, which I figured out, but I would also like to make it a bit darker. I believe it should be possible with the GlassPane, but I have no idea how to set the GlassPane to cover the panel with one color...
Help will be much appreciated, I don't think I have any helpful code to share, but I think the situation explained above gives a general idea? I would just like someone to get me on track with this so I can further work this out! Thanks!
My Main class extends JFrame and it has a BorderLayout.
Add your BorderLayout to a JPanel having, e.g. GridLayout().
This AnimationTest illustrates painting a background image behind components.
This Translucent example illustrates using an AlphaComposite; see also this AlphaTest.
Well for your first question, you can use a label and set the icon of it:
JLabel lblimage = new JLabel("");
lblimage.setIcon(new ImageIcon(Main.class.getResource("/img/background.png")));
lblimage.setBounds(0, 0, 794, 711); //size of frame
contentPane.add(lblimage); //bottom
contentPane.add(component1); //middle low
contentPane.add(component2); //middle top
contentPane.add(component3); //top
as for your second question.. you could possibly do the same thing, just use an image with a solid color and lower the transparency, and place on top of your other components (not sure on this solution though).
So if I was writing pseudo code:
if(mouseInsideFrame==true)
frame.setVisible(true);
else
frame.setVisible(false);
What method would I use for the mouseInsideFrame in the if statement?
Thanks
I came across a post on java.net that covers visibility options, including this one using a private AWT API.
public class TransparentFrame {
private static final float OPAQUE = 1.0f;
private static final float TRANSLUCENT = 0.1f;
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
com.sun.awt.AWTUtilities.setWindowOpacity(frame, OPAQUE);
}
#Override
public void mouseExited(MouseEvent e) {
com.sun.awt.AWTUtilities.setWindowOpacity(frame, TRANSLUCENT);
}
});
frame.setVisible(true);
}
}
This is OK for toy code, but obviously I wouldn't use a private com.sun class for portable production code.
Update: Same method as before, but with more explicit step-by-step explanation.
Other respondents wonder what you want to achieve with this and question the design behind an app that unexpectedly jumps up at the user. I guess it's a matter of how dead set you are to implement this functionality exactly as you described. The technique itself may be useful for other purposes too, which is my main motivation for my new, improved revision of this answer.
As far as I know, a frame that's not visible can't capture mouse events. So it won't know if the mouse is inside it or not.
There's a loophole around this problem: If you make your frame visible but borderless and fully transparent, it will be visible in the technical sense but invisible to the user for practical purposes.
The borderless part is easy: setUndecorated(true).
It would be great if JFrame had a method like setOpaque() or setTranslucent() where you could make it fully transparent, but alas, it doesn't.
Another answer mentions a solution based on a Sun private class which does permit you to make the window transparent. This will work for current and probably near-future Sun JREs but is far from guaranteed to work with other and future JREs, so I can't recommend it. Sun expliticly advises against using their private classes this way.
There's an alternate, somewhat hacky alternative: The frame is left fully visible but it displays the image of a screenshot of the screen behind it. Because this means we're effectively looking through the frame, it's effectively invisible. This solution is described here: http://onjava.com/pub/a/onjava/excerpt/swinghks_hack41/index.html?CMP=OTC-FP2116136014 . The author and I both admit to this being a a bit clumsy; it also involves a lot more code than should be necessary. But it's based on standard Java coding and should be supported unchanged in many Java environments upward of about version 1.4 or so.
The tip describes how to create a Component that displays the screen background. That's fine for when you want the frame to be invisible, but what happens when you want it to be normally visible?
The thing to do is to give the JFrame's ContentPane a CardLayout and add both the TransparentBackground component and your intended main visible component (likely a JPanel) to it. With that set up, switching between "invisible" and visible involves simply:
setUndecorated(false) // (true)
cardLayout.last() // (first)
This switching, of course, will be controlled by a MouseListener you can add to the JFrame.
Out of the top of my head, there is a fairly easy way to get the position of the mouse on the screen (I think it has something to do with Toolkit). Now, if you can combine that with a way to find out your frame's position on the screen (if you don't already know), you have your solution.
I'm curious what you're trying to do though.
Given what you are trying to do, I would say you need two frames (or perhaps just JPanels and frame that does a lot of changing. One is to capture the mouse moving over it (make it transparent, undecorated or otherwise acceptably out of the way) and when the mouse moves over it, show the new frame (or panel) and then hide that when the mouse moves out of it.
The other answers here give you the basics on how to capture the mouse events and set the frame undecorated and transparent.
Normally you could use listeners. Specifically:
frame.addMouseListener(new MouseListener() {
public void mouseEntered(MouseEvent evt) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent evt) {
frame.setVisible(false);
}
});
But the problem is that since your JFrame is not visible, there is no way to listen to mouse events!!!! At least, from what I know....