Set maximum size of JFrame - java

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.

Related

Position problem with adding something to JFrame in Java

I want to add JLabels to JFrame directly. I don't want use JPanel. But I have a position problem. I set layout as null.
I tryed draw line to see what's going on.
#Override
public void paint(Graphics g){
g.setColor(Color.red);
g.drawLine(0, 31, super.getWidth(), 31);
}
And the zero is actually 31.
Drawing screenshot
Why? And how can I fix that?
I want to add JLabels to JFrame directly. I don't want use JPanel.
If you're adding the JLabel "to the JFrame" then you're adding it to the contentPane which is almost always a JPanel, so 99% of the time, you're still using a JPanel, even without trying to.
But I have a position problem. I set layout as null.
Which is almost always a bad thing to do. This makes for GUI's that don't work on all platforms, fighting against the Java philosophy and structure.
And the zero is actually 31.
Why? And how can I fix that?
Because of the top part of the JFrame is taken up by the OS window's menu bar. The contentPane, starts 31 pixels below the top of the JFrame (in your case -- different for different OS's and screen resolutions).
Best to avoid drawing directly on the JFrame, which is actually composed of many sub-components -- the content pane, the root pane, the glass pane,... and instead draw within the paintComponent method of a JPanel that you either add to the contentPane or make as the contentPane. Then 0,0 is the top left of the usable portion of your main window.
Also, please elaborate more on the underlying reason why you're trying to avoid use of a JPanel. Your issue may in fact be an XY Problem type issue.
Positions in a JFrame are relative to the edge of the window, not the content pane. To get the dimensions of the content pane, use getContentPane().getWidth() and getContentPane().getHeight().

Editind image (JLabel) inside JScrollPane

I am developing an image editor capable of drawing ovals on an image. I am able to successfully add an image into a JScrollPane and draw on it using the fillOval() function. But each and every time I move the scroll bar all the drawn ovals disappear. Since the image to be uploaded is often large in size scroll bars cannot be disabled. I have incorporated the image in a JLabel. Please help.
I am able to successfully add an image into a JScrollPane and draw on it using the fillOval() function. But each and every time I move the scroll bar all the drawn ovals disappear.
This suggests to me that you're not drawing correctly. Since you've not shown us how you're drawing, we can only guess, but perhaps you're calling getGraphics() on a component and using an unstable Graphics instance. If so, you'll be better off calling getGraphics() on a BufferedImage itself, and drawing on it. Either that or drawing in the paintComponent method of your JComponent.
If this doesn't help, please provide more information on exactly what you're doing, preferably by creating and posting a Minimal, Complete, and Verifiable Example Program. We don't want to see your whole program, but rather you should condense your code into the smallest bit that still compiles, has no extra code that's not relevant to your problem, but still demonstrates your problem.

scaling up the contents whenver window size increases or decreases

Is there any possibilities to scale the contents of the J Frame For Example : if im giving the initial setSize(800,1000) in which i placed the textfields,labels,buttons etc..as per the size 800,1000 the application looks good,suppose im maximizing the window of the Frame, automatically the textfields,labels,buttons are kept in the static way...how to scale it when the maximize is pressed the entire contents are cleanly arranged in good manner..kindly give me solution to solve this issue
if im giving the initial setSize(800,1000) in which i placed the
textfields,labels,buttons etc..as per the size 800,1000 the
application looks good,suppose im maximizing the window of the Frame,
automatically the textfields,labels,buttons are kept in the static
way...
for AbsoluteLayout to have to place the JComponent by using the Insets that came from first container
Is there any possibilities to scale the contents of the J Frame
have to use ComponentListener (notice delayed by Swing Timer, because this Listener firing a new event for every pixels on all directions) for scalling JComponents together with container
this is job only for LayoutManager, don't to supply that by using AbsoluteLayout & Insets & ComponentListener, be sure this code could be longer an more complicated than by using GroupLayout

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.

setSize() doesn't work for JFrame

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.

Categories