I'm trying to create a 50x50 window in Java but the window won't go smaller than 125x50, even if I try to manually resize it.
Here's my code currently:
import javax.swing.*;
public class smallwindow {
public static void main(String args[]) {
JFrame frame = new JFrame("");
frame.setSize(50, 50);
frame.setVisible(true);
}
}
I am running this with the latest version of Java on Mac OS X.
Is there any way to do this with a JFrame, or would I need to use something else, like maybe AWT?
**edit: is there a way to do this while retaining the titlebar, window management buttons, etc.?
You would have to do the following on the JFrame:
frame.setUndecorated(true);
Guys, I did a little more looking into it, apparently there is a method called setMinimumSize
Basically, all you need to do is add
Dimension minimumSize = new Dimension(50, 50);
frame.setMinimumSize(minimumSize);`
I've found that if the size is less than about 75x75, then resizing it will suddenly change the minimum width to around 75. The solution is to just to do frame.setResizable(false)
But anyways, thanks for all your help!
but is there anyway to do this such that you still retain the tilebar, window management buttons, etc.?
You can use the Metal LAF which includes the title bar:
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame(...);
Related
I have a Java Swing GUI and everything in my JFrame is offset by a few pixels. On MacOS, I had to offset everything by 12 pixels downward to account for it. On Windows, everything is shifted to the left and downward as well. I discovered that
setUndecorated(true);
removes the JFrame border (which I suspect is the cause of my problems) but it also removes the title bar.
Is there a way I can remove the JFrame border (or some other alternative to make sure everything is centered) and still keep the title bar? I need the title bar so that I can move the JFrame around and have the maximize/minimize/close functions.
Also, the layout is set to null in case that matters. (Everything I'm doing is pixel - based so I cannot set it to anything else).
Thanks.
I found a solution after Googling a bit more. Calling
getContentPane().setPreferredSize(...)
pack();
inside the JFrame constructor will adjust everything so that the titlebar and borders will not impact the view of the content pane.
For anyone else that may need this, you have to set the preferred size of the content pane specifically as that is what you want to appear correctly.
This way you can keep the titlebar and all the normal functionality of a JFrame window that you would otherwise lose with setUndecorated(true);.
The reason why you are adding 12px is because this is consume by the Title and border.
If you use setUndecorated(true) You will loose the title bar and you have to implement the addWindowListener to add a location changing of of an application.
The best way to do is:
Class Main{
public static void main(String[] args){
//JFrame
JFrame frame = new JFrame();
//Create a Main Panel and setPreferred Size and not set Size or set Bound
JPanel mainPanel = new JPanel(); //You value down
mainPanel.setPrefferedSize(new Dimension(x, y));
frame.add(mainPanel);
//and then in last add
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//the pack method help you to setSize of the JFrame According to the
//Component size
frame.pack();
frame.setVisible(true);
}
}
Also posted on coderanch.com.
import javax.swing.*;
public class Tmp {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.setSize(200, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JTextField());
frame.setVisible(true);
}
});
}
}
A problem regarding resizing this JFrame.
This is how it looks by default right after program starts:
When I try to resize it like shown on a picture and move a mouse pointer to the top of a screen (like on picture below) I see this:
When I release the mouse the frame is resized but unresponsive. And there is a black space on it.
This is how it looks:
This happens on Windows 8.1 and java 1.7.0_45 (it also happens on Windows 7).
The problem does not occur when using other ways of resizing a frame in Windows.
It only happens when "Show window contents while dragging" is active in system settings.
Why is it happening?
How can this be fixed?
This sounds a lot like the bug reported here. Supposed to be fixed in JDK8 and 9, and according to the issue tracker the bug fix is backported into version 7u80.
i have windows 7 with jdk1.7.0_25 and your code worked as fine for me
i have 3 solution :
1-i think Oracle is wrong in update of 45 you can replaced 45 with 25
2-any graphical user interface in java uses from OS and maybe windows 8 not compatible with java 7 yet
3-you can add a listener for frame resize and call repaint(); in body of listener or set size to actual size
also you can try to using setUndecorated(true) and custom mouse listener which implements frame resize
I'm working on an online mode for a new game and in order to prevent cheating I need fix window sizes (and both players need a window with the same sizes).
I used 'jframe.setResizable(false);' but it seems to be "glitchy".
When I click the window and move it away from the border of the screen, Windows does minimize it.
Here's a video about it:
http://www.youtube.com/watch?v=AQ7OHJOuLSk&feature=youtu.be
I've tried following code in order to fix it:
Dimension d = new Dimension(width, height);
panel.getJFrame().setMaximumSize(d);
panel.getJFrame().setMinimumSize(d);
panel.setMaximumSize(d);
panel.setMinimumSize(d);
and I created a Component Listener:
if (max_height!=-1){
if (e.getComponent().getSize().getHeight()>max_height){
e.getComponent().setSize((int) e.getComponent().getSize().getWidth(),max_height);
}
}
if (max_width!=-1){
if (e.getComponent().getSize().getHeight()>max_width){
e.getComponent().setSize(max_width,(int) e.getComponent().getSize().getHeight());
}
and I tried to work with Layouts but nothing worked.
What I need now is either the possibility to prevent that minimize "glitch" (If it is a glitch) or a way to make the JPanel not resizable. Like when the size of the JFrame window is changed, the JPanel always stays the same. It's neither streched nor minimized.
Help is much appreciated :)
Sincerely Felix
So far the best patch for this annoying issue is the following. Doesn't matter where you call the setResizable(false) method. Just add this piece of code after you setVisible(true).
private void sizeBugPatch() {
while (frame.getWidth() > yourWidth) {
frame.pack();
}
}
Where yourWidth is the width you've set in any of the possible ways, either manually or by overriding setPreferredSize methods. The explanation is quite easy, frame.pack() seems to reset frame.setResizable(boolean b) somehow. You could use an if instead of the while loop but I prefer while to exclude the case the window would still be extra-sized even after a second pack().
Did you initialize the variable jframe or are you calling the general Class?
Because if you do it like this:
JFrame frame = new JFrame();
frame.setResizable(false);
It works fine for me...
I want to know how to make a java JFrame start out maximised. I don't want it to be fullscreen (no window around it) I just want it to start out like a normal program such as a web browser.
I already know about using:
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
To get the screen size, but when you apply this Dimension to the JFrame it can still be dragged around even though it takes up almost all of the screen. You can press the maximse button to stop this but I would rather that the window started out maximised.
Also, I fear for the effects that maximising the window would have upon the contents of the window.
How do I go about doing this?
Use java.awt.Frame.setExtendedState():
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
I've only just begun writing GUI programs, this being my second one. With both projects (both homework assignments) I have had the same issue. The GUI objects (such as JTextField) do not show when the application runs until after I resize the window or move keyboard focus to them. If I do not do one of those two things, then I'll just have an empty application window.
Any ideas why this is happening and what I could do to solve it? I'm working on Mac OS 10.6.1.
My code is below. Feel free to comment on my coding style, but please focus on the problem I'm having.
import javax.swing.*;
import java.awt.*;
public class ToDo extends JFrame {
private int height = 30,
width = 300;
public ToDo() {
this.setSize(400,400);
this.setVisible(true);
this.setLayout(null);
this.setResizable(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("To Do List");
JTextField todoItem[] = new JTextField [10];
Container contentpane = this.getContentPane();
contentpane.setLayout(null);
for(int i=0; i<10; i++) {
todoItem[i] = new JTextField();
todoItem[i].setBounds(10,(height*(i)+10),width,height);
contentpane.add(todoItem[i]);
}
}
public static void main(String[] args) {
new ToDo();
}
}
You have to add the elements before the component is made visible.
Put this as your last line:
this.setVisible(true);
alt text http://img10.imageshack.us/img10/8210/capturadepantalla200911s.png
This is not OSX related, it happens in Windows also.
There are some rules about how you should never touch Swing objects from outside the Swing thread once they've been realized. I always ignore these rules, but it could well be you've been bitten by them under Mac OS.
As a step in the officially right direction, try to not do setVisible() until you've assembled the whole thing, i.e. at the bottom of the constructor.
Reference material: http://www.math.vu.nl/~eliens/documents/java/tutorial/ui/swing/threads.html
A guess: add the component before setBoundsing it.
I could be wrong--been a long time since I've done a GUI in java--but I'm guessing your issue is making the JFrame visible before you finish adding elements. I think you need to either do that afterwards, or call update on the frame.
EDIT - Also, not sure setting the layout to null is a good idea. I've always used GridBag, but it might lose its default if you set it to null.