limit JavaSwing application size to screen size? - java

My application is currently too large for the window(monitor). I've built a scrollbar into the application to accommodate it but it still extends over the window rather than use the scrollbar. Originally, the initialization of the outer frame calls pack(), then setVisible(true).
I've tried to set the size of the frame but it doesn't seem to have an effect. The outer frame is a Masthead, and I call getWindow() on it to set size. Calling mframe.getWindow().setSize(640, 480); doesn't do anything.
This is the current behavior:
This is what I'd like:
EDIT: what's a good way for setting the app to full screen on startup?

I've tried to set the size of the frame but it doesn't seem to have an effect.
That's because you called pack on the window. pack() sets the component's size to be the maximum of an components it contains, which makes your setSize() useless.
From the api:
pack() Causes this Window to be sized to fit the preferred size and layouts of its subcomponents.

Perhaps you want to use Toolkit#getScreenSize combined with setSize. Something like (for example, to set it to a specific percentage of screen size):
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
component.setSize((int)(screenSize.width * widthPercent),
(int)(screenSize.height * heightPercent));
If you want your app to start maximized, you might want to look at setExtendedState, like
myFrame.setExtendedState(Frame.MAXIMIZED_BOTH);

Related

How do I determine the minimum width of a JFrame to hide the horizontal scrollbar?

When I create a JFrame with child components, it opens to the smallest possible width a window can be, when I expected the width to be the minimum size to display the contents of the window without horizontal scroll bars.
How do I determine what that minimum size should be? Obviously something in the UI knows, because when made wide enough, the horizontal scroll bar disappears. This is the state I want the window when created. How do I fetch that number, which I suspect I would return as an override to getPreferredSize? As far as a preferred height, that's arbitrary.
As much as I know, when you use the pack() method, it automatically chooses the minimum size needed for all the components in-hand.
In another question, this subject is covered:
The pack method sizes the frame so that all its contents are at or
above their preferred sizes. An alternative to pack is to establish a
frame size explicitly by calling setSize or setBounds (which also sets
the frame location). In general, using pack is preferable to calling
setSize, since pack leaves the frame layout manager in charge of the
frame size, and layout managers are good at adjusting to platform
dependencies and other factors that affect component size.
From Java tutorial

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.

JPanel size by inner components

Is it possible to tell JPanel to set its size to fit all components that it contains? Something like pack() for JFrame.
edit: The trick with preferredSize didn't help. I've got JSplitPane, where in one part there is GridBagLayout with many labels (see screenshot) and labels overlap each other.
screenshot http://foto.darth.cz/pictures/screen.png
After looking at the source code for pack(), I came up with:
panel.setPreferredSize(panel.getPreferredSize());
This forces the panel to recalculate its preferred size based on the preferred sizes of its subcomponenents.
You may or may not have to call validate() afterward; in my tiny example, it seemed to make no difference, but the Javadoc says:
The validate method is used to cause a container to lay out its subcomponents again. It should be invoked when this container's subcomponents are modified (added to or removed from the container, or layout-related information changed) after the container has been displayed.
So I guess it depends on why you're having to repack your JPanel.
By default Containers have a preferred size that matches the preferred layout size given by the container. So literally all you have to do is:
panel.setSize(panel.getPreferredSize());
Presumably you are doing something odd with the parent to stop the parent's layout manager doing the equivalent of this.
maybe you can do something like that by removing from your panel
setResizable(false);
I would try:
panel.revalidate();
panel.repaint();
This will not necessarily set the panel to its preferred size, that is more dependent on what the layout manager decides to use.
This is useful in cases where you have added/removed components from a panel that is currently displayed and visible.
Update:
Based on your screenshot I can say the following:
1) Consider programatically changing the divider location.
2) Consider programatically resizing the window itself horizontally since it seems to be a little tight to show both sides of the split pane.
Or both.
You can set the divider location by doing
splitPane.setDividerLocation(newSize);
Keep in mind that there are two overloaded methods for this, one taking a float one taking an int. The float does a percentage of the size while the int is the size in pixels. The size is for the left hand panel (or top panel for that orientation).
I would consider possibly changing the divider location based on the preferred width of the panels.
The javax.swing mysteries reveal themselves only gradually, and only to those who are prepared to offer many libations (particularly torn out clumps of hair, hours burning the midnight oil, etc.) to the gods of Swing.
However, for this case in point I would suggest the following as a sort of Swiss army knife which usually does what you think the framework should do anyway:
myJPanel.getTopLevelAncestor().validate()
As the sacred text says, "Validates this container and all of its subcomponents." (Container.validate). NB getTopLevelAncestor() is a JComponent method.
Can't remember how JSplitPane fits into this: try it and you'll probably find that it validates both components (right and left, top and bottom), but I would be surprised if changing the divider doesn't do this for you anyway.
I had a similar issue using Netbeans GUI Builder. My inner panels were getting weird sizes; I was trying to adjust the minimum and preferred sizes manually, which was a frustrating exercise.
The problem was solved when I reset all the minimum and preferred sizes back to default (In Netbeans GUI Builder: right click JPanel component -> Properties -> preferredSize -> Reset to Default). When there is no imposed size, the jpanel takes the size of the inner component.
Note: GridBaLayout was used in my case
JSplitPanes are a bit fussy when it comes to its children's sizes, have a look at the Java tutorial. Are you using the GridBagLayout correctly? Looks like it's not setting the right JPanel's minimum size properly.
Here's an example of a panel which:
Resizes with it's parent.
Sets the width to the width of the parent.
Sets the height according to sum of the height of all of it's children.
JPanel panel = JPanel(new GridBagLayout())
panel.setMaximumSize(new Dimension(panel.getMaximumSize().width, panel.getPreferredSize().height))
panel.validate()
panel.repaint()

Netbeans GUI Designer & Fixed-Size Application Panels

I'm having a problem, creating a fixed-size overall panel for a touchscreen GUI application that has to take up the entire screen. In a nutshell, the touchscreen is 800 x 600 pixels, and therefore I want the main GUI panel to be that size.
When I start a new GUI project in NetBeans, I set the properties of the main panel for min/max/preferred size to 800 x 600, and the panel within the 'Design' view changes size. However, when I launch the app, it is resized to the original default size.
Adding this code after initComponents() does not help:
this.mainPanel.setSize(new Dimension(800,600));
this.mainPanel.setMaximumSize(new Dimension(800,600));
this.mainPanel.setMinimumSize(new Dimension(800,600));
this.mainPanel.repaint();
I've peeked into all of the resource files and cannot seem to find values that would override these (which seems somewhat impossible anyway, given that I'm setting them after initComponents() does its work). I'm using the FreeDesign layout, because I wanted complete control over where I put things.
I suspect the layout manager is resizing things based upon how many widgets I have on the screen, because different prototyped screens come in at differing sizes.
Help is appreciated!
Have you tried java full screen mode?
I use this method to complete the task, not sure if its the best and you need to wait calling it until the frame is actually on the screen.
protected void growBig()
{
int screenWidth = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
int screenHeight = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height;
Rectangle rectangle = getFrame().getBounds();
rectangle.setSize(screenWidth, screenHeight);
getFrame().setBounds(0, 0, screenWidth, screenHeight);
getFrame().setSize(screenWidth, screenHeight);
getFrame().doLayout();
getFrame().validate();
updateUI();
}
I'm not sure how your touchscreen device works. But if you use Netbeans preview your panel is put in some outer container like JFrame/JWindow. And just maybe you set the frame to 800x600.
If so, then the problem might be that the frame eats a few pixels for its own border, leaving your panel size < 800x600. And in that case your panel will be unable to use your min/max settings and revert to default sizes.

Categories