I had a Java assignment about a month ago, which was about building a GUI. I used GroupLayout to manage the position of the components. I ran into a problem where if I put a very long string of text into a JTextField and resize the outer window, the textfield suddenly "bursts".
I fixed the issue using GridBagLayout, but I wanted to come back to the original problem in hopes of getting a better understanding of GroupLayout.
Here's a SSCCE that demonstrates this problem. (I tried to minimize it as much as I can, I apologize if my example is too long.)
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame {
JTextField text1;
JTextField text2;
JPanel myPanel;
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(() -> new Main());
}
public Main() {
super("Sussy Imposter");
createComponents();
setLayout();
configureSettings();
}
public void createComponents() {
text1 = new JTextField(20);
text2 = new JTextField(20);
text1.setMaximumSize(text1.getPreferredSize());
text2.setMaximumSize(text2.getPreferredSize());
myPanel = new JPanel();
myPanel.setBackground(Color.CYAN);
myPanel.setPreferredSize(new Dimension(100, 100));
}
public void setLayout() {
Container c = getContentPane();
GroupLayout groupLayout = new GroupLayout(c);
c.setLayout(groupLayout);
groupLayout.setAutoCreateGaps(true);
groupLayout.setAutoCreateContainerGaps(true);
groupLayout.setHorizontalGroup(
groupLayout.createSequentialGroup()
.addComponent(myPanel)
.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(text1)
.addComponent(text2))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup()
.addComponent(myPanel)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(text1)
.addComponent(text2))
);
}
public void configureSettings() {
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
}
When I copy-paste this text: Let me send you to space 🚀Space travel ASMR Roleplay 🚀(Eng)(Kor) | Roleplay, Storytime, Whitenoise into one of the textfields, and resize the outer window, the textfield "bursts".
I've set the maximum size of the textfields to their preferred sizes in createComponents(), so I don't understand why the size of the textfield exceeds its maximum size when I resize the window.
Can anyone explain why I'm getting this odd behavior?
EDIT: I've overrided the paint() method to see how the width of the textfield size change.
public void paint(Graphics g) {
super.paint(g);
System.out.printf("min: %d\n", text1.getMinimumSize().width);
System.out.printf("pre: %d\n", text1.getPreferredSize().width);
System.out.printf("max: %d\n", text1.getMaximumSize().width);
}
Output before resizing
min: 5
pre: 224
max: 224
Output after resizing
min: 569
pre: 224
max: 224
As #matt pointed out in the comments, this seems to happen because the minimumSize becomes very large. More notably, the minimumSize grows larger than the preferredSize and the maximumSize, which is very unexpected.
Edit:
The behavior of the min size, growing after a resize, and becoming larger than the max size seems like a bug.
Setting the min size explicitly is a workaround it:
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(text1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(text2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
This sets the component min and max size to default size as explained in the documentation:
To make a component fixed size (suppress resizing):
group.addComponent(component, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
You can achieve the same behavior by setting the min as well as the max sizes:
text1.setMinimumSize(text1.getPreferredSize());
text1.setMaximumSize(text1.getPreferredSize());
Related
I am using MigLayout for a very long window.
and I wish to "push" the second and fourth column to fill all the length of the whole window, but I cannot achieve it. There's no push option in column constraint, only grow and fill.
Here's a SCCEE, as someone once suggested, whose name I already forgot:
package com.WindThunderStudio.MigLayoutTest;
import java.awt.Cursor;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
public class MigLayoutTest extends JFrame{
private JFrame mainFrame;
private JPanel panel;
private JLabel lblResumenAuto;
private JLabel lblResumenAutoResult;
private JLabel lblResumenRazonSocial;
private JLabel lblResumenRazonSocialResult;
private JLabel lblResumenPeriodo;
private JLabel lblResumenPeriodoResult;
private JLabel lblResumenFechaHora;
private JLabel lblResumenFechaHoraResult;
public MigLayoutTest(){
run();
}
public void run(){
mainFrame = new JFrame();
mainFrame.setBounds(0, 0, 1250, 500);
mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel p = new JPanel();
p.setSize(mainFrame.getSize());
p.setLayout(new MigLayout("fill","[max!, grow]","[50:20:30]10[100::]10[20::]10[50!]10[20!]"));
mainFrame.setContentPane(p);
panel = new JPanel();
panel.setLayout(new MigLayout("fillx", "[left, 15%]10[left, grow, 35%]10[left, 15%]10[left, grow, 35%]", "[center]10[center]"));
lblResumenAuto = new JLabel("MY LABEL 1111111111111");
lblResumenAutoResult = new JLabel("1111111111111111111111");
panel.add(lblResumenAuto);
panel.add(lblResumenAutoResult);
lblResumenRazonSocial = new JLabel("MY LABEL 2222222222");
lblResumenRazonSocialResult = new JLabel("2222222222222222222222");
panel.add(lblResumenRazonSocial);
panel.add(lblResumenRazonSocialResult,"wrap");
lblResumenPeriodo = new JLabel("MY LABEL 33333333333333");
lblResumenPeriodoResult = new JLabel("3333333333333333333333333333333333333333333333333333333");
panel.add(lblResumenPeriodo);
panel.add(lblResumenPeriodoResult);
//poner el texto como html puede tener otra linea, porque es muy largo
lblResumenFechaHora = new JLabel("<html>MY LABEL <br /> 4444444444444444</html>");
lblResumenFechaHoraResult = new JLabel("4444444444444444444444444");
panel.add(lblResumenFechaHora);
panel.add(lblResumenFechaHoraResult);
p.add(panel,"cell 0 0");
getContentPane().setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
setBounds(0, 0, 1250, 500);
getContentPane().add(mainFrame.getContentPane());
pack();
setVisible(true);
setLocationRelativeTo(null);
setResizable(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
MigLayoutTest test = new MigLayoutTest();
}
});
}
}
If you run the code, you can note that the columns' width increases as its containing text's length changes. But it never fills the whole width of its container.
What's desirable, is to fix the column 0 and 2 by 15% of the whole width, and let column 1 and 3 to ocupy the rest, 35%, with the first two columns occupying the 50% size of the whole width.
Am I missing something here? I don't want to specify the width of every column, setting pre:min:max, because it is bad practice, as suggested by this post, which gets lots of vote up.
panel.setLayout(new MigLayout("fillx",
"[left, 15%]10[left, grow, 35%]10[left, 15%]10[left, grow, 35%]",
"[center]10[center]"));
But, if I set pref:min:max, it can fill the whole width.
First the code, then explanation. Try this:
import java.awt.Cursor;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
public class MigLayoutTest extends JFrame {
private JPanel panel;
private JLabel lblResumenAuto;
private JLabel lblResumenAutoResult;
private JLabel lblResumenRazonSocial;
private JLabel lblResumenRazonSocialResult;
private JLabel lblResumenPeriodo;
private JLabel lblResumenPeriodoResult;
private JLabel lblResumenFechaHora;
private JLabel lblResumenFechaHoraResult;
public MigLayoutTest() {
run();
}
public void run() {
panel = new JPanel();
panel.setLayout(new MigLayout("debug, fill",
"[left, 15%]10[left, 35%]10[left, 15%]10[left, 35%]", "[center]10[center]"));
lblResumenAuto = new JLabel("MY LABEL 1111111111111");
lblResumenAutoResult = new JLabel("1111111111111111111111");
panel.add(lblResumenAuto, "sg label");
panel.add(lblResumenAutoResult, "sg value");
lblResumenRazonSocial = new JLabel("MY LABEL 2222222222");
lblResumenRazonSocialResult = new JLabel("2222222222222222222222");
panel.add(lblResumenRazonSocial, "sg label");
panel.add(lblResumenRazonSocialResult, "sg value, wrap");
lblResumenPeriodo = new JLabel("MY LABEL 33333333333333");
lblResumenPeriodoResult = new JLabel("3333333333333333333333333333333333333333333333333333333");
panel.add(lblResumenPeriodo, "sg label");
panel.add(lblResumenPeriodoResult, "sg value");
// poner el texto como html puede tener otra linea, porque es muy largo
lblResumenFechaHora = new JLabel("<html>MY LABEL <br /> 4444444444444444</html>");
lblResumenFechaHoraResult = new JLabel("4444444444444444444444444");
panel.add(lblResumenFechaHora, "sg label");
panel.add(lblResumenFechaHoraResult, "sg value");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
getContentPane().setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
getContentPane().add(panel);
pack();
setVisible(true);
setLocationRelativeTo(null);
setResizable(true);
}
public static void main(String[] args) {
MigLayoutTest test = new MigLayoutTest();
}
}
The explanation of my changes:
Now except for simplifying the code and layout I used “debug” within the layout constraints to see what actually happens to the layout. I suggest using it anytime things go wrong with the layout - it makes MigLayout draw the borders of components and cells, thus visualizing potential problems.
I removed the unnecessary mainframe and p - if you really need it for a nested layout try to add it once you have solved the inner layout to your liking.
As to p and panel - it might be that you need here two different layouts, one nested in another, but this is the actual source of your problem. p had also its own grid layout, with
p.add(panel,"cell 0 0");
you put panel in the top left cell of the p - this is why panel was not distributed over the whole window but sat in the upper left corner.
As you see without p it positions nicely in the middle of the screen without any constant size, still showing all components, but more importantly it has 50% of the window size for the first and 50% for the last two columns. This was achieved by giving the components a “sizegroup”:
Gives the component a size group name. All components that share a
size group name will get the same BoundSize (min/preferred/max). It is
used to make sure that all components in the same size group gets the
same min/preferred/max size which is that of the largest component in
the group. An empty name "" can be used.
And it also resizes like it should!
The nested layout might also had been the root of another problem – don’t know if you didn’t notice it or it just didn’t show up on your machine, but if I tried to resize your window the panel got wider and wider (never narrower) even if I shrunk the window. At some point it got wider than the window itself and even then growed further on each resize.
Next - setting the dimensions to a constant value didn’t make sense, since after pack the layout manager starts sizing everything based on preferred sizes of the window and its content. Besides, you never know which size is your users’ screen, so any constant size could be equally bad if effectively used. Better to drive the size through the content and available runtime environment. With your code on my machine it took all available horizontal space of my two screens (2 x 1280) and did’t look pretty.
I also think that you do not need to start the frame using EventQueue.invokeLater, just create a MigLayoutTest and that’s it.
EDIT after OP's own answer
Setting the size using setBounds(0, 0, 1250, 500) before pack is not working correctly (by this I mean making the window be that size). Even in the screenshot below OP's own answer it is not 500px high. Here is what I get on Windows 7 running JDK 1.8.0_91:
The size of my screen is 1280 x 1024, the size of the programm's window is 914 x 301.
I'd suggest using one of the following:
To set it to the constant size of 1250 x 500 px move the setSize between pack and setVisible:
...
pack();
setSize(1250, 500);
I'd use setSize, setBounds doesn't make sense, since by calling setLocationRelativeTo(null) you centering the programm's window on the screen anyway, so the origin is being dismissed immediately.
To maximize horizontally and let the height be 500 px set main window's preferred size before pack:
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setPreferredSize(new Dimension(screenSize.width, 500));
And to maximize horizontally and let the preferred height as it was originally:
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setPreferredSize(new Dimension(screenSize.width, getPreferredSize().height));
Of course you can make the window 1250 x 500 big by setting its preferred size instead of using setSize, too.
The resize problem is not so big now, but it's still there - make the window wider, than narrow it just a little bit. You'll notice that the panel gets wider even if the window got narrowed. The problem is that panel component doesn't get big enough to fill the one column of p initially (BTW you can add the 'debug' flag to each MigLayout, also that of the panel - it will then outline all of the inner components as well).
To make it fill the parent container add it like this:
p.add(panel, "cell 0 0, grow");
Now it is the full width of p from the very beginning and resizing works as expected.
Regarding starting the JFrame using invokeLater - we start our main windows usually without it and had never had problems, since there were no interactions with Swing until the first frame was visible, yet I have just noticed that it is regarded to be the best practise - even in Oracle's tutorials. It looks like I had learned something here, too :-).
Comparision of the frame's window with and without adding with "grow"
Test scenario: start the application and resize it to be wider.
As you see in the first screenshot the component size is smaller than the column width - it looks like the component were lying behind the column size while resizing. On the second screenshot the component width remains the same as the column width at all times. As I said previously the reason might be the Java and/or operating system combination, I don't know. But obviously it behaves differently and on my machine less than optimal.
Thanks to #Tomasz Stanczak, I have solved it finally. However, I found part of what he said is relevant, and others are not. For future readers who may see this, I have to made it clearer.
The final working code is:
import java.awt.Cursor;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
public class MigLayoutMySCCEE extends JFrame{
private JFrame mainFrame;
private JPanel panel;
private JLabel lblResumenAuto;
private JLabel lblResumenAutoResult;
private JLabel lblResumenRazonSocial;
private JLabel lblResumenRazonSocialResult;
private JLabel lblResumenPeriodo;
private JLabel lblResumenPeriodoResult;
private JLabel lblResumenFechaHora;
private JLabel lblResumenFechaHoraResult;
public MigLayoutMySCCEE(){
run();
}
public void run(){
JPanel p = new JPanel();
p.setLayout(new MigLayout("debug, fill","[grow]","[50:20:30]10[100::]10[20::]10[50!]10[20!]"));
panel = new JPanel();
panel.setLayout(new MigLayout("fillx", "[left, 15%]10[left, grow, 35%]10[left, 15%]10[left, grow, 35%]", "[center]10[center]"));
lblResumenAuto = new JLabel("MY LABEL 1111111111111");
lblResumenAutoResult = new JLabel("1111111111111111111111");
panel.add(lblResumenAuto);
panel.add(lblResumenAutoResult);
lblResumenRazonSocial = new JLabel("MY LABEL 2222222222");
lblResumenRazonSocialResult = new JLabel("2222222222222222222222");
panel.add(lblResumenRazonSocial);
panel.add(lblResumenRazonSocialResult,"wrap");
lblResumenPeriodo = new JLabel("MY LABEL 33333333333333");
lblResumenPeriodoResult = new JLabel("3333333333333333333333333333333333333333333333333333333");
panel.add(lblResumenPeriodo);
panel.add(lblResumenPeriodoResult);
//poner el texto como html puede tener otra linea, porque es muy largo
lblResumenFechaHora = new JLabel("<html>MY LABEL <br /> 4444444444444444</html>");
lblResumenFechaHoraResult = new JLabel("4444444444444444444444444");
panel.add(lblResumenFechaHora);
panel.add(lblResumenFechaHoraResult);
p.add(panel,"cell 0 0");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
getContentPane().setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
setBounds(0, 0, 1250, 500);
getContentPane().add(p);
pack();
setVisible(true);
setLocationRelativeTo(null);
setResizable(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
MigLayoutMySCCEE test = new MigLayoutMySCCEE();
}
});
}
}
And the window looks like:
Some notes:
The debug trick is very useful and urges me to read DOC again. I wish it can gain more attention and importance in the Quick Start page, however.
The unnecessary nesting is a problem and Tomasz make me to review the hierarchy, great! I changed the nesting part to make it clearer. But it's irrelevant.
The sizeGroup part is great idea and I decide to use it in future development as much as possible, but it's irrelevant to my case. I solved it without using it.
I have found the wider-and-wider problem after Tomasz's tip, but it is due to [max!] combined with adding the panel to the first cell of grid layout, not frame/panel nesting. I removed [max!] and changed it to [grow] and the width is not expanding anymore. I didn't touch the p.add(panel, "cell 0 0") part. As observed and by definition,
p.setLayout(new MigLayout("debug, fill","[grow]","[50:20:30]10[100::]10[20::]10[50!]10[20!]"));
the first line of the panel has only one column, if I understand well.
EDIT after Tomasz's edit
I surely have learned more than you did :) I tried to get rid of setBounds() part and to change add(panel, "cell 0 0") to add(panel, "grow"), but I cannot see much difference, am I missing something here? Yet "grow" is almost always the better choice and desirable.
Here's 2 GIFs showing what I got: (by ScreenToGif, a light-weighted but powerful tool, especially useful for showcase)
So I've made a class WindowDisp which extends JFrame:
public class WindowDisp extends JFrame{
private static final long serialVersionUID = 3245091489595286109L;
private int height, width;
private JPanel panel;
private JLabel mainPane;
public WindowDisp(int a, int b, int pw, int ph){
height = a;
width = b;
Container c = getContentPane();
c.setPreferredSize(new Dimension(width, height));
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLocationRelativeTo(null);
mainPane = new JLabel();
mainPane.setPreferredSize(new Dimension(pw, ph));
mainPane.setFont(new Font("Monospaced", Font.PLAIN, 10));
panel = new JPanel();
panel.setPreferredSize(getSize());
panel.add(mainPane);
add(panel);
setVisible(true);
pack();
}
public static void main(String[] args){
WindowDisp win = new WindowDisp(400, 400, 400, 400);
}
}
From my Main class, I declare a WindowDisp where its height and width are equal to its ph and pw. The problem, however, is that, upon running my program, white 'bars' appear around the default background colored JPanel in the frame. They appear to be padding the panel from the right and the bottom, as though there is space in the frame that the panel is not occupying, although, if my coding is correct, the panel should be the same size as the frame's ContentPane, should it not?
I've found that removing either of the two pack(); commands does not remove these bars, although removing the first one changes them to black, and removing the second widens the one on the right. Of course, removing both of them causes the frame not to be the same size as its ContentPane. Furthermore, removing the add(panel); altogether has no effect.
I can't figure out what in this code is causing that seemingly empty space to appear in my frame, and, again, in my program, all four values being passed to the Window constructor are equal. What seems really strange is that, even if I just remove the add(panel);, nothing at all changes visa vi the white padding. In fact, I can comment out everything from mainPane =... to add(panel); and that doesn't affect it at all.
I can't seem to replicate the issue exactly, but I think I can replicate the desired results...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class WindowDisp extends JFrame {
private static final long serialVersionUID = 3245091489595286109L;
private JPanel panel;
private JLabel mainPane;
public WindowDisp(int width, int height) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
mainPane = new JLabel();
mainPane.setBorder(new LineBorder(Color.BLUE));
mainPane.setFont(new Font("Monospaced", Font.PLAIN, 10));
panel = new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(width, height);
}
};
panel.setLayout(new BorderLayout());
panel.setBorder(new EmptyBorder(4, 4, 4, 4));
panel.add(mainPane);
add(panel);
pack();
setLocationRelativeTo(null);
setVisible(true);
System.out.println(getSize());
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
WindowDisp win = new WindowDisp(400, 400);
}
});
}
}
Things that jump out at me (as been of issue)...
panel.setPreferredSize(getSize()); - The size of the window is generally larger than the window size, this is because the window has decorations which are painted WITHIN the frame boundaries. pack uses the layout information (and preferredSize of the components indirectly) to ensure that the content has the amount of space that it asks for, it then sizes the window around this to accommodate the frame decorations. By calling getContentPane().setPreferredSize you are superseding any information that the layout manager might provide and ignoring the requirements of the other components. This is one of the (many) reasons why we recommend that you NEVER call setPreferredSize, ever...
To reiterate...
Container c = getContentPane();
c.setPreferredSize(new Dimension(width, height));
Forces the viewable space of the window to be set to the width/height values (400x400). This container will no longer be able to react to changes to it's content and will ALWAYS prefer to be 400x400
mainPane.setPreferredSize(new Dimension(pw, ph));
Sets the preferred size of the mainPane to 400x400 (based on your example). Again, it will ALWAYS prefer to be 400x400, but the simple fact of setting the content pane, means that this value is actually ignored...
panel.setPreferredSize(getSize());
Now, the nail in the coffin. This sets the panel to be the same size of the frame, BUT, the frame is larger than the contentPane (400x400 + frame decorations), it is also offset within frame (it won't be positioned at 0x0, but will be offset so that it appears below the frame's title and right border), but could expand beyond the frames boundaries
This combination of issues are all working against you. Instead of worrying about the frame size, worry about the size needs/requirements of the what the frame displays.
Each OS uses different frame decorations, so the actual, physical, frame size will be different on different OSs, but if you focus on the requirements of the content, you won't care
See Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? (Yes.)
Instead use layout padding and borders for white space. Finally, call pack() to ensure the frame is exactly as large as (the smallest size) it needs to be in order to display the components and white space.
import java.awt.Font;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class WindowDisp extends JFrame {
private JPanel panel;
private JLabel mainLabel;
public WindowDisp(int t, int l, int b, int r, String title) {
super(title);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setResizable(false);
setLocationByPlatform(true);
mainLabel = new JLabel("Hello Padding!");
mainLabel.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 10));
mainLabel.setBorder(new EmptyBorder(t, l, b, r));
panel = new JPanel();
panel.add(mainLabel);
add(panel);
pack();
setVisible(true);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
new WindowDisp(50, 150, 50, 150, "Window 1");
new WindowDisp(50, 100, 50, 100, "Window 2");
}
};
SwingUtilities.invokeLater(r);
}
}
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().
One thing I have learned from swing days is to never mix setPreferred... with pack(). You either use one or the other.
Try this code:
import javax.swing.*;
import java.awt.*;
public class WindowDisp extends JFrame {
private int height, width;
private JPanel panel;
private JLabel mainPane;
public WindowDisp(int a, int b, int pw, int ph){
height = a;
width = b;
Container c = getContentPane();
// c.setPreferredSize(new Dimension(width, height));
mainPane = new JLabel("Hello from Pane");
mainPane.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 10));
panel = new JPanel();
// panel.setPreferredSize(new Dimension(pw, ph));
panel.add(mainPane);
c.add(panel, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLocationRelativeTo(null);
pack();
setVisible(true);
}
public static void main(String[] args){
WindowDisp win = new WindowDisp(400, 400, 400, 400);
}
}
Output:
I have these two JFrames that work just fine when setUndecorated is set to false, but it doesn't work consistently when it's set to true;
frame = new JFrame("Name of the frame"); // main frame
frame.setAlwaysOnTop(false);
frame.setSize(width, height);
frame.add(canvas);
frame.setUndecorated(true);
frame.setVisible(true);
menu = new MenuUI(); // this is also a undecorated JFrame
menu is called by a mouse event, but it sometimes shows up in the front and sometimes it shows in the back of the main frame...
public static void checkMove(int action, MouseEvent e) {
int x = e.getX();
if(x == 0){
menu.setVisible(true);
menu.toFront();
}else{
menu.setVisible(false);
}
I've tried to do frame.toBack(); but it sends the frame behind everything else as well...
How can I bring undecorated JFrame in front of another undecorated JFrame and make it consistent?
EDIT: the menu frame disappears on mouse event
contentPane.addMouseListener(new MouseAdapter() {
#Override
public void mouseExited(MouseEvent e) {
setVisible(false);
}
});
EDIT2: after mKorbel's answer, I've tried to create a JDialog, but I get the same result as with JFrame. How can I set the parent if I extend a JDialog?
package menu;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JDialog;
import openGL.main.openGLMain;
#SuppressWarnings("serial")
public class menuTest extends JDialog {
public menuTest() {
setUndecorated(true);
setBounds(0, 0, 250, frame.getHeight());
GroupLayout groupLayout = new GroupLayout(getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGap(0, 450, Short.MAX_VALUE)
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGap(0, 700, Short.MAX_VALUE)
);
getContentPane().setLayout(groupLayout);
getContentPane().addMouseListener(new MouseAdapter() {
#Override
public void mouseExited(MouseEvent e) {
setVisible(false);
}
});
}
}
JFrame by default never react to AlwaysOnTop or toFront correctly, isn't main functionality for primary Top-Level Container (without iconify and deiconify, but then flashing on the screen)
you have to use JDialog for this job, set for parent and modality (if needed)
JFrame by default never react to AlwaysOnTop or toFront correctly, isn't main functionality for primary Top-Level Container (without iconify and deiconify, but then flashing on the screen)
you have to use JDialog for this job, set for parent and modality (if needed)
exactly and also you can use JinternalFrame that makes you have some children frame in your main Jframe and you can iconify and deiconify or close which is don't needed and also make them resizable if you need.
and if you want make them in front or back to others,you can make it easily by Drag&Drop that Frame Children!
I am playing around with JFrame and when I do this:
public class UI extends JFrame {
public UI() {
pack();
setSize(50, 50);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
UI test =new UI();
}
}
The frame is not square, but when I change (50,50) to something larger (500,500) it is square. Can anyone tell my why this is?
SetSize(50,50) is very small size for Jframe, JFrame is a root component so at least use proper size for it. It contains title control box and because of it, to get square with small size is difficult. alternative you can use Jwindow for small square size.
around 133x39 is the minimum size of JFrame, "setBounds(100, 100, 133, 40);" will help you.
To reduce more, should be setUndecorated(true);
Please have a look at the following code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GridLayoutTest2
{
private final JDialog msgDisplayer;
public GridLayoutTest2()
{
JLabel maleLabel = new JLabel("Male",JLabel.CENTER);
maleLabel.setBorder(BorderFactory.createEmptyBorder());
JLabel femaleLabel = new JLabel("Female",JLabel.CENTER);
femaleLabel.setBorder(BorderFactory.createEmptyBorder());
JLabel fmaleIcon = new JLabel();
fmaleIcon.setIcon(new ImageIcon(getClass().getResource("/images/TESTING-Image.gif")));
fmaleIcon.setBorder(BorderFactory.createEmptyBorder());
JLabel maleIcon = new JLabel();
maleIcon.setIcon(new ImageIcon(getClass().getResource("/images/TESTING-Image.gif")));
maleIcon.setBorder(BorderFactory.createEmptyBorder());
msgDisplayer = new JDialog();
msgDisplayer.setLayout(new GridLayout(4,1,1,1));
msgDisplayer.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
msgDisplayer.setTitle("Body Fat Percentage Classification");
msgDisplayer.add(femaleLabel);
msgDisplayer.add(fmaleIcon);
msgDisplayer.add(maleLabel);
msgDisplayer.add(maleIcon);
msgDisplayer.pack();
msgDisplayer.setVisible(true);
msgDisplayer.setLocationRelativeTo(null);
}
public static void main(String[]args)
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new GridLayoutTest2();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
This code contains a HUGE gap (space) between the labels and images screenshot attached). I do not want to have this space between labels and images. How can I eliminate it? I know I can go with the GridBagLayout to do it, but, is there any way in GridLayout? Please help!
GridLayout allocates an equals amount of space for all components based on the largest component in the container. If you don't wish to use a complex layout manager such as GridBagLayout, you could use BoxLayout, which uses the component's preferred sizes. A BoxLayout with Y_AXIS alignment would be suitable here.
Example
Thats not the gap space, its real size of JLabel's (maleLabel, fmaleLabel). Size of image determines size of parent JLabel and in GridLayout, all components will take size of largest component. Gap between components is 1 as you defined when setting layout. So solution of your problem lays in finding suitable layout manager.
Reimeus gave you an example of GridBagLayout and BoxLayout, and I would like to recommend you MiGLayout which is quite easy to use.
Its the Layout what matters!!
Here I have done a short EG with my GUI builder to show the adjustment of white space(or the size of JLabel):
More white space:
Code:
public class udyfash extends javax.swing.JFrame {
public udyfash() {
initComponents();
}
#SuppressWarnings("unchecked")
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images.jpg"))); // NOI18N
jLabel1.setText("yooo!!");
jLabel1.setBorder(javax.swing.BorderFactory.createCompoundBorder(null, javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))));
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 405, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(44, 44, 44)
.addComponent(jLabel1)
.addContainerGap(39, Short.MAX_VALUE))
);
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new udyfash().setVisible(true);
}
});
}
private javax.swing.JLabel jLabel1;
}
We say GridBag is complex,but use the Layout Manager you are ok with.