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.
Related
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.
I have had this problem a few times before, and I finally attempted to solve it, but I cannot fond the right solution. I have a simple JFrame program. The basic JFrame code looks like this:
JFrame frame = new JFrame("halp");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setResizable(false);
frame.setVisible(true);
Here's an image of what I'm trying to accomplish:
As you can see, there's space taken up by the top bar and the borders of the window. One way to fix this is to increase the size of the window, but that isn't neat at all, and also not every system has the same border sizes of the window.
I've done research and tried a few things I found on earlier asked questions:
frame.pack(). This method only set the window to minimal size, as if I didn't set the size at all.
frame.getContentPane().setSize(500,500);. The window still has the borders counted in the frame size.
frame.setPreferredSize(new Dimension(500, 500)); and JPanel.setPreferredSize(new Dimension(500, 500));. The frame went into minimum size, as with frame.pack().
I tried combining these methods, but nothing seemed to work.
What am I missing? Is there another aspect of the JFrame I need to add? Is it an incompatibility issue of BlueJ?
frame.getContentPane().setPreferredSize(new Dimension(500,500));
followed by a
frame.pack();
should solve your problem.
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!
i have created FlowLayoutEx with some operations.then tried to put them into frame in standart way.
public static void main(String args[]){
FlowLayoutEx applet=new FlowLayoutEx();
JFrame frame=new JFrame("HW2LayoutSettings");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(applet,BorderLayout.PAGE_END);
applet.init();
applet.start();
frame.setSize(400,300);
frame.pack();
frame.setVisible(true);
}
It probably does show up, but maybe it's too small or an Exception was thrown before, it's impossible to know without knowing what your FlowLayoutEx class is.
But when you call pack() you set the window to a size corresponding to the preferred size of its components. It means:
Your previous call to setSize is useless since the size is set again by the call to pack
Your custom component should be set a preferred size.
What is the preferred size (re getPreferredSize()) of applet? Could it be (0, 0)? Is the JFrame using a BorderLayout? I think that is the default, but I'm not sure. Try setting it yourself: frame.setLayout(new BorderLayout()). Not sure about BorderLayout.PAGE_END - I always use BorderLayout.CENTER (for the main, or only, component in a JFrame).
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);