Results 1 to 8 of 8
- 10-07-2009, 03:22 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 8
- Rep Power
- 0
setMaximumSize() on a JFrame problem
Hi, I will just post some simple code to explain.
JFrame myFrame = new MainGrid();
JFrame.setDefaultLookAndFeelDecorated(true);
myFrame.setMinimumSize(new Dimension(800,550));//Works
myFrame.setMaximumSize(new Dimension(1000,750));//Doesn´t work
The method setMininumSize works perfectly on my GUI, but then how come that I can´t set the maximum size of my window? It looks like it ignores the code.
Ty, Val.
- 10-07-2009, 04:15 PM #2
Member
- Join Date
- Oct 2009
- Posts
- 16
- Rep Power
- 0
Making JFrame Maximum Size
The following might help you solve the problem:
Good LuckJava Code:public class Program extends JFrame { public Program() { Rectangle maxBounds = GraphicsEnvironment.getLocalGraphicsEnvironment(). getMaximumWindowBounds(); this.setSize(maxBounds.width, maxBounds.height); }//end of constructor }//end of class Program
- 10-07-2009, 05:46 PM #3
Member
- Join Date
- Sep 2009
- Posts
- 8
- Rep Power
- 0
I´ve tried it, but no success.
I mean, I dont want my JFrame to get bigger than 1000x750.
- 10-08-2009, 12:45 AM #4
Member
- Join Date
- Oct 2009
- Posts
- 16
- Rep Power
- 0
About JFrame Resizing
Hi Valhallon
I think I understand what your'e trying to get at now.
The following is a quote from a different forum. I tried to paste the links to it, but unfortunately java-forums.org doesn't allow me to post hyperlinks until I have 20 posts or greater.
Try to Google: Java Programming - Re: JFrame maximum size
...and you will find the original post at forums.sun
Try it and see if it helps...The real issue is that setMinimumSize is declared in Window whereas setMaximumSize is declared in Component. Thus the two methods do not have complementary connotations.
Experimenting with workarounds posted on those bug report pages, I think this is the cleanest solution, even if it does involve using a painting method for something other than painting -- normally to be avoided. SSCCE:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class MaxSizeFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new MaxSizeFrame().makeUI();
}
});
}
public void makeUI() {
final JFrame frame = new JFrame("") {
@Override
public void paint(Graphics g) {
Dimension d = getSize();
Dimension m = getMaximumSize();
boolean resize = d.width > m.width || d.height > m.height;
d.width = Math.min(m.width, d.width);
d.height = Math.min(m.height, d.height);
if (resize) {
Point p = getLocation();
setVisible(false);
setSize(d);
setLocation(p);
setVisible(true);
}
super.paint(g);
}
};
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setSize(400, 400);
frame.setMaximumSize(new Dimension(500, 500));
frame.setMinimumSize(new Dimension(300, 300));
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
This way, instead of repeated flickering, there is just one flicker as the specified maximum size is crossed.
db
- 10-09-2009, 12:44 PM #5
Member
- Join Date
- Sep 2009
- Posts
- 8
- Rep Power
- 0
Yea I´ve tested that code in a separate code and it can solve the problem.
My only doubt at the moment is how to implement that in my frame. I mean the code alone works the way I want, but after I use it in my program it does not. Maybe I´m using the code wrongly I dunno, here´s the code:
@Override
public void run() {
new MainGrid().makeUI();//My frame, produces my Window
}//without any max or min sizes.
});
}
public void makeUI() {
final JFrame frame = new JFrame("") {//Procudes another random
- 10-09-2009, 02:19 PM #6
Member
- Join Date
- Oct 2009
- Posts
- 16
- Rep Power
- 0
JFrame Resize Problem
Hi Valhallon
Yes. I have tested that piece of code as well and it seems to be working fine. I wish I could look more into the problem and of course it is hard if I can't see more of the source code to elaborate on the problem (but it's ok) On the other hand there are just a few things I need to know.
The MainGrid class, is it extending JFrame? (maybe a silly question, but still need to make sure, because a class could extend JDialog as well)
Are you calling any methods in the MainGrid class that might affect the instantiation of the MainGrid instance? For e.g. is there not a chance that you could have tried to call a setSize() method or something that could affect the way the program is reacting?
Quoting the last lines on your post:
Have you included all the source code following the last left brace?public void makeUI() {
final JFrame frame = new JFrame("") {//Procudes another random
This is important, because it is required for the program in order to function properly. From experience I can tell that the code above should produce a compile-time error.
Also something that you need to take into consideration is that you will probably need to move all the source code from your MainGrid class to the "final JFrame" part above, because that will be the new frame on which all of your code will reside.
This is just an example on what I tried to do with the makeUI() method. I still admit that the source code wasn't originally developed by myself, but I want to use it as an example to try and help you reach some sort of possible solution, so here goes:
I would like you to see what I tried to do with the textField. The textField was placed successfully on the frame and the code still worked fine.
One last thing. Even the best software developer out there knows that debugging is a valuable tool when it comes to these problems. If you really get stuck and don't want to post your source code on forums (and I respect that), debug every line of code and really try to understand what every line of source code in your program tries to accomplish.Java Code:public void makeUI() { [B]JTextField txt1 = new JTextField(5);[/B] final JFrame frame = new JFrame("") { @Override public void paint(Graphics g) { Dimension d = getSize(); Dimension m = getMaximumSize(); boolean resize = d.width > m.width || d.height > m.height; d.width = Math.min(m.width, d.width); d.height = Math.min(m.height, d.height); if (resize) { Point p = getLocation(); setVisible(false); setSize(d); setLocation(p); setVisible(true); } super.paint(g); } }; [B]frame.setLayout(new FlowLayout());[/B] [B]frame.add(txt1);[/B] frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 400); frame.setMaximumSize(new Dimension(500, 500)); frame.setMinimumSize(new Dimension(300, 300)); frame.setLocationRelativeTo(null); frame.setVisible(true); }
I really hope this comment was useful.
Good luck!Last edited by Hugo; 10-09-2009 at 02:22 PM.
- 10-20-2009, 08:52 PM #7
Member
- Join Date
- Sep 2009
- Posts
- 8
- Rep Power
- 0
Hi Hugo! Sorry for my delayed answer, I only saw your reply today heh.
I´ve tried your last code again and now its working 100% fine :)
I figured that I was making 1 big error. Instead of initializing a normal JFrame like this:
final JFrame frame = new JFrame("") {
I should have put my own class: final MainGrid frame = new MainGrid() {
Thats why it was creating 2 JFrames instead of my main one.
About my code, I would have posted it all, but my main class is HUGE lool so I decided just to put some simple code to show the real problem. About debugging, that´s another problem. My program implements like 4 or 5 listeners, so I can´t debugg it properly. Just one simple action like moving my mouse will cause it to run some "infinite" code without even any breakpoints.
Finally, just wanna thank your help and sorry once again for the delayed answer.
Cumps, VaL.
- 10-20-2009, 09:14 PM #8
Member
- Join Date
- Oct 2009
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
JFrame resizing problem
By Bluefox815 in forum AWT / SwingReplies: 7Last Post: 02-24-2011, 07:56 PM -
Problem-----JFrame
By HariPrasad in forum AWT / SwingReplies: 2Last Post: 07-28-2009, 12:34 PM -
Simple JFrame Problem
By DC1 in forum New To JavaReplies: 4Last Post: 06-06-2008, 07:09 AM -
JFrame problem
By vassil_zorev in forum AWT / SwingReplies: 1Last Post: 01-25-2008, 02:53 AM -
JFrame problem
By saytri in forum New To JavaReplies: 6Last Post: 01-11-2008, 05:12 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks