Java show fullscreen swing application with taskbar without titlebar - java

I need to develop a full-screen GUI without a title bar on it (I am going to use border layout design page start as my own title), also need to show the taskbar. I have tried this:
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
Frame becomes full-screen without the title bar but hides the taskbar.
if I change to frame.setUndecorated(false);
the frame becomes full-screen and taskbar is shown, but title bar doesn't disappear
How can I fix that? Thank you.

Works for one desktop
Rectangle r = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.setSize(r.width, r.height);
frame.setVisible(true);
Edit: Works for multiple desktops too!

Related

Java JFrame weird behavior when resizing with setWindowDecorationStyle(JRootPane.FRAME);

I am creating a JFrame, using a custom look and feel named FlatLaf. But I am experiencing weird behavior when I resize the window. I can replicate the exact same problem with this snippet:
import com.formdev.flatlaf.intellijthemes.FlatOneDarkIJTheme;
import javax.swing.*;
import java.awt.*;
public class Main{
public static void main(String[] args){
try{ UIManager.setLookAndFeel(new FlatOneDarkIJTheme()); }
catch(Exception e){}
JFrame frame = new JFrame();
frame.setSize(1280, 720);
frame.setMinimumSize(new Dimension(750, 400));
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
frame.setVisible(true);
}
}
With that I can get what I need, this:
JFrame with Look and Feel
To achieve that I used the instructions frame.setUndecorated(true); and
frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME); (Because as I said, I don't want the default system border, like this: Frame with default system border)
The problem is when I resize from the left and the top of the frame, it starts to move in that direction, and I haven't find a solution, here is a gif showing that: JFrame with weird resize behavior.
That problem is obviously solved when I remove the lines:
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
Because I get back the system border, but that's not what I want.
How can I fix this?, Is there a better way to do it (Or different)?.
The problem is when I resize from the left and the top of the frame, it starts to move in that direction
This effect happens because you are setting the minimum size of the JFrame, together with the fact that you clear the frame decorations. So don't set the minimum size, or just leave the decorations enabled. If you leave the decorations enabled and still set the minimum size, then the user will be able to resize the frame until it reaches the minimum corresponding dimension where, instead of moving, the frame will stay in place.
As for the title bar, in case you need all of its good characteristics (such as the 3 buttons on the right, the title and icon of the frame on the left and the bar in the middle which can be actually dragged by the user to move the frame), but you also want it to have a specific color, then don't undecorate it and only change the color of the title bar component, which can be done like so:
import com.formdev.flatlaf.FlatDarkLaf;
import com.formdev.flatlaf.FlatLaf;
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(final String[] args) {
final FlatDarkLaf laf = new FlatDarkLaf();
FlatLaf.install(laf);
final Color controlColor = laf.getDefaults().getColor("control"); //Obtains the background color of FlatDarkLaf.
//Change the title bar active and inactive color...
UIManager.put("TitlePane.inactiveBackground", controlColor); //This is the color of the title bar when the frame doesn't have focus.
UIManager.put("TitlePane.background", controlColor); //This is the color of the title bar when the frame has focus.
final JFrame frame = new JFrame("Title");
frame.setSize(1280, 720);
frame.setMinimumSize(new Dimension(750, 400));
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
In this case, you have to leave the decorations enabled. But then that seems ok because you want the characteristics of the title bar, just with a different background color in it.
I found out about the properties/keys "TitlePane.inactiveBackground" and "TitlePane.background" in the documentation of the source code of the class FlatTitlePane which as someone can see (in the source code of FlatRootPaneUI) this is the class being installed on the root pane of the frame as its title bar.
I've seen about the laf.getDefaults().getColor("control"); part in the source code of FlatLaf (in the getDefaults method).
According to some instructions of the FlatLaf's web page, you should better set those properties after installing the LAF and before creating the components.

Java JFrame set not to resize, resizes anyways

I have a JFrame maximized using
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
Some code between that and at the end
frame.setVisible(true);
frame.setResizeable(false);
Tough, Dragging the top of the window down resizes it anyways.
I added a ComponentListener to it to see if it detects being resized, and it does. The componentResized function gets called when i do the resize.
Any fix for this?
Thanks.
Dragging the top of the window down resizes it anyways
This is the way applications work in Windows. Try a native Windows application and you will see the dragging the window causes it to shrink to a preferred size.
You could try code like the following:
JFrame frame = new JFrame();
frame.setResizable(false);
frame.pack();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
frame.setMinimumSize(frame.getSize());
You can still drag the frame from its position, but you will not be able to resize it.

Java Color Chooser panel in main window?

I'm trying to figure out how to add a HSV color chart to the window of my application. I'm aware of the color chooser offered by Java, but i would like to have the chart integrated in my own window instead of opening it a new window. Is there a way to add one of the panels from the color chooser directly to my window, or is there a way to create one myself?
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.getContentPane().setLayout(new BorderLayout());
JColorChooser colorChooser = new JColorChooser();
AbstractColorChooserPanel hsvPanel = colorChooser.getChooserPanels()[1];
frame.add(hsvPanel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
I was able to pull the HSV Panel out by accessing the element at 1 in the getChooserPanels for the default JColorChooser.
Although this might not be ideal considering the implementation of the JChooserPanel could change the ordering.

Draw graphics beneath components

So I am currently making a login screen that has a cool looking background effect made using the Graphics object and a 'game loop'. When I add in a JTextField though, it is seen underneath everything and not above. Is there anyway to to make the graphics draw underneath all components inside of the JFrame?
Here is an image of the graphics:
The text field is there, just underneath everything being drawn to the surface of the frame. I want to somehow reorder this so it draws underneath components.
Here is my current frame code:
frame = new JFrame("Login");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.getContentPane().setPreferredSize(new Dimension(450, 200));
frame.getContentPane().setBackground(Color.black);
frame.setBackground(Color.black);
frame.setLayout(new FlowLayout());
JTextField user = new JTextField(20);
user.setLocation(100, 200);
user.setVisible(true);
frame.add(user);
frame.pack();
frame.setVisible(true);
frame.createBufferStrategy(2);
buff = frame.getBufferStrategy();
Painter painter = new Painter();
frame.add(painter);
Any help please?
AnimationTest shows one approach. It overrides paintComponent() and invokes super.paintComponent() to ensure that components are rendered atop the background. Click anywhere to position a text field; resize to see how the default FlowLayout works. JPanel is double buffered by default using the existing buffer strategy.

Only small portion of window visible when shown. How do I show the whole thing?

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);

Categories

Resources