Java; Getting insets before frame is visible - java

Assuming a normal JFrame, I'm trying to get the inset values before the frame is made visible. I can get these values fine once the frame is made visible (and i suppose I could create the jframe offscreen), but was wondering if there is some way to tickle Java into setting the insets before visibility. Prior to this call, all inset values are zero.
Net, I'm trying to get the exact dimensions of a frames client area -- or said better, I'm trying to create a JFrame that has very specific client area dimensions.
Thanks in advance.

Will this help?
frame.pack();
System.out.println("Frame Insets : " + frame.getInsets() );
I'm trying to create a JFrame that has
very specific client area dimensions.
Then you set the preferred size of the panel added to the frame:
panel.setPreferredSize( new Dimension(...) );
frame.add( panel );
frame.setResizable( false );
frame.pack();
frame.setVisible(true);

Related

How to automatically resize JFrame if elements are too large?

I have been researching for 30 minutes on how to automatically resize a JFrame when the elements are too large. I am trying to fit line segments inside the JFrame but it always exceeds the space but does not automatically generate more space.
What should I do?
DrivePanel panel = new DrivePanel(aCar, coordinates);
JFrame application = new JFrame();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(panel);
application.setSize(600,600);
application.setVisible(true);
Example of output:
Some things to consider :
if you are doing custom painting on your panel, remember that the panel's size it's not changed by what you are drawing.
For example, if the "last" point (i mean the point with the biggest values of x and y) is drawn at (1000,1000) coordinates, you should set the preferred size of your panel in order to contain it.
To let your application using the preferred size of your components, you should call application.pack() (where application is your JFrame object) instead of setting size manually.
If your panel is too big to be displayed enterily on your screen, you might add it to a JScrollPane, and then add the scrollpane to your jframe (not the panel itself).
The scrollpane will automatically use scroll bars if your panel can't be fully displayed on your screen.
So consider this small example, based on your code :
DrivePanel panel = new DrivePanel(aCar, coordinates);
JFrame application = new JFrame();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setPreferredSize(new Dimension(1000,1000)); // change 1000,1000 with the coordinates you need ...
JScrollPane scrollPane = new JScrollPane(panel);
application.add(scrollPane);
application.pack();
application.setVisible(true);
Hope this helps :)

JFrame.getComponentCount with 3 jPanels returns 1

I'm creating MTG game for school Java class and I run into problem. I'm currenty testing something... I have my game GUI (extends from JFrame), and when I doublclick on exile "pile" I create a new JFrame, then I add my cards into this JFrame (Card extends JPanel) and I tried to setsize based on frame.getComponentCount()
But problem is, it returns 1, whether a card was added or not.. like my card wasn't even counted as a component.
Code:
JFrame frame=new JFrame();
Image cardImage;
cardImage = ImageIO.read(new File("pics/background.png")).getScaledInstance(this.getWidth(),this.getHeight(),Image.SCALE_SMOOTH);
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.add(new Card(cardImage,false));
frame.add(new Card(cardImage,false));
frame.add(new Card(cardImage,false));
frame.setSize(frame.getComponentCount()*80, frame.getComponentCount()*80);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
So my frame.setSize doesn't work, because of wrong return.
...
Can anyone help me identify a problem? I don't get it, because frame.add input parameter is component (according to javadoc), but when I try getComponentCount is isn't counted as a component.
Just to be clear, everything else work, I get the new window, there are 3 cards as they supposed to be, I just have to manually resize it to see them.
You're getting back the count from the JFrame's single contentPane component. To get the sub components, call the method on the contentPane itself.
frame.getContentPane().getComponentCount()
But regardless, your design for setting size this way seems brittle, and is not how I would do things. Instead, have your components return their own best preferred sizes and call pack() on your JFrame after adding components.

AbsoluteLayoutDemo.java in Oracle Java Tutorials

Question 1:
I am writing a program to use an absolute layout because I need to reposition a JLabel at runtime. I have tried to understand the following java demo program (from Oracle Java Tutorials):
http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/layout/AbsoluteLayoutDemoProject/src/layout/AbsoluteLayoutDemo.java
It suggests the following:
//Size and display the window.
Insets insets = frame.getInsets();
frame.setSize(300 + insets.left + insets.right, 125 + insets.top + insets.bottom);
frame.setVisible(true);
The frame is not set to be visible before the insets values are calculated, so they are all 0 (zero), am I getting it right?
I have tried to use frame.setSize(300, 125); to replace the code and Eclipse gives me the same result (the same window size as I have observed). Is it wrong to have setSize before setVisible?
Question 2:
In my own program, I have the following code segment:
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JLabel moving_label = new JLabel();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
panel.setLayout(null);
frame.add(panel);
panel.add(moving_label);
moving_label.setBounds(150, 150, 50, 50);
moving_label.setIcon(img);
frame.setVisible(true);
frame.setBounds(100, 100, 300+frame.getInsets().left+frame.getInsets().right,
300+frame.getInsets().top+frame.getInsets().bottom);
//I don't need to have panel.validate() or panel.repaint(), right?
a) Is the above code segment in a correct order (I am a newbie of java's Swing GUI)?
b) I am using Eclipse on my MacBook Pro to run Java programs. I have 40 JLabels (each with an ImageIcon) in a JPanel in a JFrame. If I use the above codes to generate the GUI, it takes half a second to show all JLabels after the window is displayed. Why aren't the JLabels shown at the same time the window is displayed?
Any help will be appreciated. Thank you so much for your time :)
The frame is not set to be visible before the insets values are calculated, so they are all 0 (zero), am I getting it right?
Correct, components do not have a size until the frame has been realized, which means the native objects on the OS you are running on have been created. This is done when you pack() the frame or make the frame visible.
If you are trying to make the panel that contains the label a size of (300, 125) then the easier approach would be to do:
JPanel panel = new JPanel();
panel.setPreferredSize( new Dimension(300, 125) );
panel.add(...);
frame.add( panel );
frame.pack();
//frame.setLocation(...);
//frame.setLocationByPlatform();
frame.setVisible( true );
Now the pack() method will determine the size of the frame based on the preferred size of all the components added to the frame. No need to worry about insets.
Note: as a general rule you should not invoke setPreferredSize() on a component, since each component is responsible for determining its own size.

How do I use JFrame.pack()?

How I can use pack() and what is it doing?
For example:
//1. Create the frame.
JFrame frame = new JFrame("FrameDemo");
//2. Optional: What happens when the frame closes?
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//3. Create components and put them in the frame.
//...create emptyLabel...
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
//4. Size the frame. (How does pack() set the frame size???)
frame.pack();
//5. Show it.
frame.setVisible(true);
The behaviour of the method is specified in the documentation (the javadoc).
Here's the documentation for 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.
pack changes the size of your JFrame to the smallest possible size that will still hold all the elements you've put in it.

Why doesnt frame show up with its title?

i have created FlowLayoutEx with some operations.then tried to put them into frame in standart way.
public static void main(String args[]){
FlowLayoutEx applet=new FlowLayoutEx();
JFrame frame=new JFrame("HW2LayoutSettings");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(applet,BorderLayout.PAGE_END);
applet.init();
applet.start();
frame.setSize(400,300);
frame.pack();
frame.setVisible(true);
}
It probably does show up, but maybe it's too small or an Exception was thrown before, it's impossible to know without knowing what your FlowLayoutEx class is.
But when you call pack() you set the window to a size corresponding to the preferred size of its components. It means:
Your previous call to setSize is useless since the size is set again by the call to pack
Your custom component should be set a preferred size.
What is the preferred size (re getPreferredSize()) of applet? Could it be (0, 0)? Is the JFrame using a BorderLayout? I think that is the default, but I'm not sure. Try setting it yourself: frame.setLayout(new BorderLayout()). Not sure about BorderLayout.PAGE_END - I always use BorderLayout.CENTER (for the main, or only, component in a JFrame).

Categories

Resources