setSize() doesn't work for JFrame - java

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
class Demo
{
JFrame jf;
Demo()
{
jf=new JFrame("Demo");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(5000,5000);
jf.setVisible(true);
System.out.println(jf.getSize());
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
new Demo();
}
});
}
}
I use jf.setSize(5000, 5000) for JFrame but after that getSize returns other size: java.awt.Dimension[width=1386,height=788] (my screen resolution is 1366x768) Can I set frame size greater than screen size? probably such behaviour is provided with some frame properties but I don't know about them.

Try using setPreferredSize instead of setSize. Works in my case (Ubuntu 12.04+GNOME 3).

The javadoc says this:
"The method changes the geometry-related data. Therefore, the native windowing system may ignore such requests, or it may modify the requested data, so that the Window object is placed and sized in a way that corresponds closely to the desktop settings."
This covers the behavior that you are observing / complaining about.
It is not crystal clear, but one reason that Window.setSize(...) has this behaviour is that window managers (outside of Java) typically veto application attempts to do this. Presumably, that's because it is open to abuse and "not what the user wants". Anyway, it is ultimately not your application's call to override the window manager's restrictions.

Just for Friday fun (that is, nothing you should consider doing in production environment :-) - playing a bit further with #jinguy code, I noticed that the bigger-than-life size was used after minimizing the frame. Doing so programmatically let it appear as monster right from the start
jf.setPreferredSize(new Dimension(5000,5000));
jf.setMinimumSize(new Dimension(5000,5000));
jf.pack();
jf.setVisible(true);
jf.setState(Frame.ICONIFIED);
jf.setState(Frame.NORMAL);
System.out.println(jf.getSize());

I tried out a few combinations of calls, and the following seemed to work:
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setPreferredSize(new Dimension(5000,5000));
jf.setMaximumSize(new Dimension(5000,5000));
jf.setMinimumSize(new Dimension(5000,5000));
jf.pack();
jf.setVisible(true);
It still prints out a different size, but the window appears to be far larger than it prints.

These are my observations (used 1.6, now I'm using 1.7 under XP):
You can have undecorated frame of "almost" any size -- I use screen resolution of 1280x1024 (rotated) and didn't noticed any problems with frame 1500x1500, although some frames 2000x2000 look uncompleted (but work) and frame of 4000x4000 displays its thumb in taskbar but this thumb is unactive and the frame itself doesn't appear. I think the largest possible size of undecorated JFrame is dependent on system capabilities which is dependent on the graphic hardware.
With decorated frames there is a simple story -- they can be a little larger than screen size (by few pixels in generally).
In my application with size determined during runtime (e.g. games where you set board size dynamically) I use the following approach:
1). before packing set frame location relative to null. It places the upper-left corner of JFrame in the middle of the screen (before pack the JFrame is (0,0) dimensioned)
2). set preferred sizes of my frame content (I always use single JPanel) and remember them
3). pack the frame
4). if frame sizes after pack don't match those before pack dispose the frame, remove content's JPanel, add JScrollPane with this JPanel and set the preferred sizes of JScrollPane as JPanel preferred sizes PLUS the JScrollBar fixed dimensions (i.e. a width of the vertical scrollbar and a height of the horizontal one).
5). pack again -- this guarantees only the necessary scrollbars appear (if you don't increase the JFrame sizes then both scrollbars will always appear -- there is also a need to remove the JScrollPane default border).
6). set new location of the frame by moving it left and up by the half of the corresponding size to center it.

Related

How to output java full screen properly

Now what I am doing in my program is that I am using setundecorated = true and MAXIMIZED_BOTH So it makes it go full screen and the display looks very nice, But the problem is that there are images (border) on the left and the right side of my screen and also a blue background. What happens is that in changing screens and resolutions these get disturbed and are not shown properly. Those grey patches come up again
History:
I have a java program which I wanted to always open in full screen; I was not able to find a way to do it properly so I had adjusted the minimum to (1370, 727) and maximum size. Thus, it started opening properly on my laptop, but when I changed my laptop's display to LCD, it started giving problems:
It opens in a smaller window:
If I then click on the maximize button, a grey area comes on the side and bottom (I wanted the items on screen to get stretched or center themselves):
And here for example, there is a grey patch at the bottom. Instead, I want the background to cover the whole screen.
Update 1
If I change to stretchable gridbaglayout, this is the code I used and what happens:
Menu.setExtendedState(MAXIMIZED_BOTH);
GridBagLayout gbl = new GridBagLayout();
Menu.setLayout(gbl);
JButton component = new JButton("1");
gbl.layoutContainer(Menu);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.NONE;
gbl.setConstraints(component, gbc);
Menu.add(component);
Menu.pack();
Menu.setVisible(true);
Question
How do I set "this" frame to setExtendedState(MAXIMIZED_BOTH) as I have done to others? (if I do this in main function, I get an error; even if I make a function for this and call it in main I get an error)
How do I get everything to stretch/rearrange themselves according to the extra grey space?
Update 2
My files in this project:
Update 3
This is the current file I am working on "FormTTS.java"
Search for "MAXIMIZED_BOTH" in there and you will find the code I think you will want to check.
Usually, as far as games go, it's preferable to use full screen mode instead of using a maximized window. You can do this in Java by using:
GraphicsEnvironment gfxEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gfxDev = gfxEnv.getDefaultScreenDevice();
Window window = new GameWindow();
gfxDev.setFullScreenWindow(window);
If you still want to use a regular frame and center the content panel, you need to define some of the GridBagLayout constraints. It's impossible to tell which without out seeing the code for the rest of the components on that screen, but consider the following:
GridBagConstraints.fill
GridBagConstraints.anchor
GridBagConstraints.weightx
GridBagConstraints.weighty
And finally, regarding setting the screen to the largest size, it is already addressed here:
Java JFrame Size according to screen resolution
I am also having same requirement as you have, below code works for me.
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(0,0,d.width,d.height); // i assume you have extended JFrame
try this, hope it works for you as well.
MyFrame mFrame= new MyFrame();
mFrame.setVisible(true);
mFrame.setExtendedState(mFrame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
I know this is a terrible answer because I don't have time to write any code. Have you tried creating a listener so you can get the proper maximum size once the window is actually created, and then setting the GridBagConstraints weightx and weighty properties accordingly?
Did you try this code
JFrame frame = new JFrame();
frame.setSize(Toolkit.getDefaultToolkit().getScreenSize());
frame.setVisible(true);
You can get full screen size of any device by "Toolkit.getDefaultToolkit().getScreenSize()" in java. Above code I set frame size to fullscreen.
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
You can get hight and width of screen to your code by using above codes. I think this will be a help.
You can easily call
setExtendedState(MAXIMIZED_BOTH); on jframe or
use bellow code to set screen size to any PC.
//size of the screen
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
//height of the task bar
Insets scnMax = Toolkit.getDefaultToolkit().getScreenInsets(getGraphicsConfiguration());
int taskBarSize = scnMax.bottom;
//available size of the screen
setSize(screenSize.width, screenSize.height - taskBarSize);
setLocation(screenSize.width - getWidth(), screenSize.height - taskBarSize - getHeight());
if u want you can remove taskbar size to get full screen anyway this is the code and this will help you.
Try setting image as a background to you JFrame. So it will adjust with frame size
How to set Jframe Background Image in GroupLayout Java
so even in full screen it will be adjusting..
if you use panel then you can resize according to panel, it shows in full panel size
yourinternalframe.setSize(mainPanel.getSize());
yourinternalframe.show();
this may be not seem as your real need, you may do something according to this
I took a look at the code that you attached for FormTTS.java, what I found out is that your screen was set as using the absolute layout hardcoded to some numbers of pixels.
Look at the following code:
Menu.getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
Menu.getContentPane().add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 230, 530, 320));
Your JFrame is not using the GridBagLayout, instead it's using AbsoluteLayout from Netbeans library. So I guess you generated these UI codes with the tools from Netbeans.
And then regarding your picture that does not fill all the screen when maximized:
jLabel9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/freetts/equations.png")));
Menu.getContentPane().add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 1530, 990));
Same problem here, it's hardcoded to some numbers of pixels.
If you want everything to be centered when you maximized your screen, I think the only way to do is to use the gridbag layout for your JFrame and this requires you to update almost everything in your code. And you will need to fully understand how GridBagLayout works. Here is the place to start.
However if you only want the background image to fill the screen you can follow the steps here to let the picture scaled to fill the size of JLabel:
Resize a picture to fit a JLabel
If it still doesn't work, you should also get the size of the screen (from one of the answers here) and then set the prefferedSize of the JLabel with those values in addition of scaling the image.
To add on to #eitanfar's answer, the best way of enabling fullscreen in Java is using the FSEM (FullScreen Exclusive Mode) API.
As he stated, this is achieved by setting the windows as fullscreen on the GraphicsDevice you want the window to appear fullscreen on, usually the default one. Even if your device does not support FSEM (id est isFullscreenSupported() returns false), setting the window as fullscreen will still partially work as the API will emulate fullscreen. The only safety check is to verify whether the GraphicsEnvironment is headless (isHeadless()). If it is, then there are no devices to display to.
The advantage FSEM gives you is that all graphics processing is run on the GPU (the GraphicsDevice is the GPU, not the monitor), therefore making it faster on most systems. In your program's options, you can allow the user to choose to enable or not FSEM so that they can run at optimal performance.
However, the system's repaint events are undefined when in FSEM, you're better off using active rendering, therefore you're better off ignoring repaint (setIgnoreRepaint(true)) and then using a custom thread for drawing.
I am having a similar problem with my application. the nearest I have come is to set all components that reside on top to either component.setOpaque(false), or component.setBackground(new Color(255,255,255,0)). you could also try panel.setVisible(false) for the unused panels.
its hard to offer up code with out the entire program but give this a whirl:
Menu.setBackground(new Color(255,255,255,0);

Set maximum size of JFrame

I'm starting my journey with Java and I'm playing with swing. I made some simple JFrame with JScrollPane inside and I'm looking for a way to restrict my frame to preset minimum and maximum size. I've searched the net about setMaximum and setMinimum and how wrong they are, but I haven found nothing(Except some weird ComponentListener). However setMinimum is working fine(at least for now) but i can't get setMaximum to work. How can I set such constraints?
EDIT:
What I mean by "Except some weird ComponentListener" is that it lets me resize frame, then just resize it back. What I want to achive is invisible bound, that prevents frame from going more.
EDIT2:
It seems that this problem concerns my OS(which is OSX), ComponentListener works fine on Windows. But on Mac, when I start dragging the window out, ComponentMoved is called and only when I release mouse button ComponentResized is called. When I copy code from Resized to Moved, first, window resize itself over MaximumSize for fraction of second, then apropiet code is called and everything is fine, but this flicker is far from acceptable. I've even overloaded paint(Graphics g) to first reset size and then call super.paint but with same result.
You can add a ComponentListener to your frame and check into componentResized(ComponentEvent e) if the new values for width&height are allowed and, in case they are, you resize the frame through setSize.

Applet Stand alone issue

I attempted to make an applet program I have Stand alone by adding in:
public static void main(String[] args) {
JFrame frame = new JFrame("StartingPoint");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
StartingPoint sp = new StartingPoint();
frame.getContentPane().add(sp);
sp.init();
frame.pack();
frame.setVisible(true);
sp.start();
}
Right after my public class. When running as just the applet this does nothing, but when Running it as an application it runs as a very small, nearly flat box aside from the heading, and when manually resized, the screen is blank other then the backround color.
Any idea what may cause this?
I have also noticed, each time I resize the frame, what is on it freezes,as if a screen shot of what should happen, and when the screen is resized to nearly full screen I can see at the tip top of the screen a sliver of what should be moving.
I'd just like to say, that dropping an applet into a frame is a really bad idea. You are better off writing the application contents into a separate container (such as JPanel) and adding that to your applet or frame - IMHO.
From the Java Docs...
Window#pack
Causes this Window to be sized to fit the preferred size and layouts
of its subcomponents. The resulting width and height of the window are
automatically enlarged if either of dimensions is less than the
minimum size as specified by the previous call to the setMinimumSize
method.
If the window and/or its owner are not displayable yet, both of them
are made displayable before calculating the preferred size. The Window
is validated after its size is being calculated.
This would suggest that your applet needs to provide a preferredSize if you wish to use pack
You need to set the size of the JFrame:
frame.setSize(500, 400);
It sounds as if you are overriding the paint() method. If so, you will need to call
super.paint(g);
to repaint all child components of the applet container on resize.

Java Swing: component that resizes itself but doesn't influence the layout

I'll try to explain my problem as simply as possible but it's a tricky topic and people who haven't encountered the issue probably won't know what I'm talking about.
I want to use a BorderLayout using west, east, north, south, etc. components that are my "normal" components (JLabels, JButtons, etc.) then I want the center component to be an "image" (that is: pixels). To this end I'm using a BufferedImage and using setIcon on a JLabel that is inside a panel that is part of the "center".
However I want my image/pixels to be "fluid": whenever the user resizes the app, I want to compute the exact size of the JLabel (icon/text gap is set to 0) and then create a new image (pixels that I manipulate directly in a BufferedImage but whatever) that has exactly that size.
Now it does work fine. But only when I resize the main window ("window" as in "one of the window of the operating system) by making it bigger.
It doesn't work when I downsize my main window.
The reason, after a lot of testing, is obviously because the size of my JLabel (in which I did a setIcon( img ) is influencing the computation of the layout manager.
So here comes the billion dollar question: how should I use a BorderLayout (or any other layout) so that I can create a "fluid" rectangle of pixels in the center of my app?
Answering my own question with an answer that I will not accept even tough it does work...
The problem can be "worked around" by creating a picture a few pixels smaller than the getVisibleRect of the center area.
So in my case I create an ImageIcon from a BufferedImage that is 20 pixels smaller (both in width and height) than the area that will hold it.
What happens then is that because the picture is smaller it doesn't "block"/prevent the layout manager from putting everything at their correct place when downsizing the main window.
So by using such an hack I get the "fluid" behavior I want.
This is however an hack whose level of hackyness cannot be understated and I'm sure there's a very clean way to solve this.
The reason, after a lot of testing, is
obviously because the size of my
JLabel (in which I did a setIcon( img
) is influencing the computation of
the layout manager.
The preferred size of the JLabel is used in the preferred size of the panel, but this size is ignored when you resize the frame, since the CENTER only gets whatever space is left over after the preferred size of the other 4 components is considered.
To this end I'm using a BufferedImage
and using setIcon on a JLabel that is
inside a panel that is part of the
"center".
Sounds to me like it should work.
Create the panel with a BorderLayout. Add the JLabel to the Center of your main panel. Then add a ComponentListener to the panel. Now when the frame is resized the center panel size will be adjusted to take the space available to it. Now that you know the size of the center panel you can recreate the Icon and add it to your JLabel,
This is how you write a SSCCE:
import java.awt.*;
import javax.swing.*;
public class LabelTest2 extends JFrame
{
public LabelTest2()
{
JLabel picture = new JLabel(new ImageIcon("???.jpg"));
add(picture);
}
public static void main(String[] args)
{
LabelTest2 frame = new LabelTest2();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}

Netbeans GUI Designer & Fixed-Size Application Panels

I'm having a problem, creating a fixed-size overall panel for a touchscreen GUI application that has to take up the entire screen. In a nutshell, the touchscreen is 800 x 600 pixels, and therefore I want the main GUI panel to be that size.
When I start a new GUI project in NetBeans, I set the properties of the main panel for min/max/preferred size to 800 x 600, and the panel within the 'Design' view changes size. However, when I launch the app, it is resized to the original default size.
Adding this code after initComponents() does not help:
this.mainPanel.setSize(new Dimension(800,600));
this.mainPanel.setMaximumSize(new Dimension(800,600));
this.mainPanel.setMinimumSize(new Dimension(800,600));
this.mainPanel.repaint();
I've peeked into all of the resource files and cannot seem to find values that would override these (which seems somewhat impossible anyway, given that I'm setting them after initComponents() does its work). I'm using the FreeDesign layout, because I wanted complete control over where I put things.
I suspect the layout manager is resizing things based upon how many widgets I have on the screen, because different prototyped screens come in at differing sizes.
Help is appreciated!
Have you tried java full screen mode?
I use this method to complete the task, not sure if its the best and you need to wait calling it until the frame is actually on the screen.
protected void growBig()
{
int screenWidth = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
int screenHeight = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height;
Rectangle rectangle = getFrame().getBounds();
rectangle.setSize(screenWidth, screenHeight);
getFrame().setBounds(0, 0, screenWidth, screenHeight);
getFrame().setSize(screenWidth, screenHeight);
getFrame().doLayout();
getFrame().validate();
updateUI();
}
I'm not sure how your touchscreen device works. But if you use Netbeans preview your panel is put in some outer container like JFrame/JWindow. And just maybe you set the frame to 800x600.
If so, then the problem might be that the frame eats a few pixels for its own border, leaving your panel size < 800x600. And in that case your panel will be unable to use your min/max settings and revert to default sizes.

Categories