How to display Button or a JButton at a particular coordinates say (x,y) of a window ?
Though you can use setLocation(),
I would strongly suggest using a Layout Manager, because while using a layout manager, though you can provide size and alignment hints, a container's layout manager has the final say on the size and position of the components within the container, believe me this can save you at times!
Assuming you have a reason that you don't want to use any of the existing LayoutManagers in the JDK, you might think about creating your own LayoutManager, specific to your application
It is pretty easy - just subclass FlowLayout, and in the doLayout method, call the setBounds for your button (and any other components).
That way you can specify the exact position assuming your expected container size, yet retain control to reposition the button in case the container is reduced or enlarged,
You can use setBounds() to specify x,y position and width and height.
You will have to set Containers' layout to null, then you will have total control of positioning components within that.
See http://zetcode.com/tutorials/javaswingtutorial/firstprograms/ for examples
Related
Not sure if what I need is possible.
I have a container (JPanel) that contains some internal elements.
I was wondering if it is possible to force internal elements to fit into the container's size.
I need them to be fully visible i.e., resize to fit inside the Panel's size and not cut some parts of the internal elements.
Scrolling is not an option.
Is this possible by using a Layout or something?
EDIT: Important clarification:
The thing is that I do not have access to the internal elements neither to their properties so I would say that a Layoutmanager capable of resizing child elements to fit to its size is needed. I tested BorderLayout and GridBagLayout but the result is always the same, the internal elements are cut out.
It's for exactly that reason that LayoutManagers exist. All the LayoutManagers work for simple containers directly, excluding GridBagLayout which is to able to handle most complete GUIs directly.
For most complete GUI's you have some choices as follows:
Look for a 3rd party layout such as MigLayout or here
Use GridBagLayout
Very easy way is use nested layout, where there is more than one JPanel and each has child JPanels with the same or different LayoutManager
Or custom layout, should be hard..., but same as using GridBagLayout
You could set the JPanel layout to border layout, then add the single child to the center. If there are multiple children, this approach becomes less useful since components added to the the NORTH, SOUTH, EAST, and WEST will remain statically sized while the centre resizes to fill the remainder.
In short, this isn't an ideal solution. All layouting in Swing is made all the more complex by the fact that different components behave in different ways, so you really need to provide further details of the child components you wish to add to your panel, and any behaviour that has been overridden on those components.
The best way is to try a couple of simple examples to see what mileage you get and whether subtle redesign of your child component nesting could help.
you can use a layout, like GridBagLayout, or BorderLayout depending on the situation. With proper weights it is possible.
this sounds to me like you should just peek an appropriate layout manager and use it. For example, look at BorderLayout - put your component in the CENTER and it will occupy all the area. Its up to each concrete layout manager to decide what will be the size of the components.
Mark
I was using a JInternalFrame inside JDesktopPane. I wanted the internal_frame to auto resize as desktop pane is resized, so I had to implement the AncestorResized event for the internal frame where I placed the following code:
this.setPreferredSize(this.getParent().getPreferredSize());
this.pack();
I would hide a JButton in a JApplet. I'm using setVisible() method but I've a problem: it works but my GUI is shifted because of the absence of the component. Is there a way to hide a component and make its space occupied???
I know that is possible in Android, but in Java?
ps. To insert component in my JPanel I'm using GridBagLayout!
There are several ways to achieve this in general.
Most proper way is to layout other components in a way that they remain correctly attached at their current positions.
Since for complex layouts the proper way can be hard to get and especially hard to change afterwards, you can apply some layout 'hacks'. For example, instead of adding the button to the panel directly, you could add the button to a separate panel of its own (let's name it buttonPanel), and then add that panel together with the button to the panel containing the other components. That way when you remove the button, buttonPanel will stay to fill the gap.
However, depending on the way how you specified constraints, buttonPanel may shrink when you remove the button. To prevent this, just before removing the button, take the buttonPanel's width and set it as its minimum/preferred width; most LayoutManagers will respect this property.
Of course, you can always resort to hardcoding dimensions to avoid dynamic size calculations, but keep in mind issues with L&F and i18n.
Try using the setOpaque() method. Just do button.setOpaque(false); and that should do the trick. Does that work?
Im trying to make a fullscreen application.
I would like to know how to make the component that I add on the JFrame to occupy a part of the screen on every resolution.
Design tab mode:
When I run on fullscreen i get this:
how can I make this following interface adjust to full screen?
Here's an idea of the some of the basic layout managers and which ones respect preferred sizes (meaning if preferred size is respected, as container expands, component will not expand with it, and vice versa)
That being said, you may want to use a BorderLayout, and place your top component at the BorderLayout.PAGE_START. Ad the screen increases, so will the component contained in that layout position. (Note: the example's main container uses a BorderLayout)
As far as the image, if you want to stretch, I'd take a look at StretchIcon from Darryl Burke. This will keep the image to a relative size.
Also a common practice is to nest panels with different layout managers, to get your desired result. You can see an example here by Andrew Thompson
Also see more about layout managers at Visual Guide to Layout Managers
I got a JViewport correctly scrolling using the default layout manager (using viewport.setViewPosition), but when I call setLayout(null) this no longer works.
Any ideas?
I can give code if needs be, but it's pretty messy at the moment.
Thanks!
Edit: Thanks for the info so far. I've been using a null layout so I can absolutely position components I draw later, is there a way I can do with whilst using a layout manager?
Scroll panes only work when the preferred size of the component added to the scroll pane is greater than the size of the scroll pane.
When you use a null layout, the preferred size is 0, so the scroll pane doesn't work. That is why you should use a layout manager. It will determine the preferred size for you.
Don't call setLayout(null) for any reason.
Every Component should have a LayoutManager, that decide about children's size and position. So use a suitable LayoutManager or implement your own.
If you call setLayout(null) then you need to
Call the Component class's setbounds method for each children.
also Call repaint method.
I'm using a borderLayout to arrange my components in a JFrame and a Box(BoxLayout.X_AXIS) to put buttons next to each other. But it creates two problems:
I want the buttons to have the same size, but it automatically resizes them to fit the text within them (especially annoying when I change the text inside a button at runtime)
I want the buttons to have a little bit of space between them (let's say 10 px)
Is this possible using the borderLayout, or do I need to use the setLayout to null? And if so, wouldn't this screw up the original placement of the buttons in the frame? Or would this still be dealt with by the Box which is placed with the borderLayout?
A couple of suggestions
Try setting the preferredSize to a suitable Dimension value
If that doesn't work, try also setting the maximumSize and minimumSize to this same Dimension value
If that still doesn't work, change the buttons' layout manager to a GridBagLayout. The advantage of this layout manager is that it lets you control the layout's behaviour in minute detail. The disadvantage is that you usually need to configure a large number of properties on the GridBagLayout in order to get the desired behaviour. I'd advise checking out a GridBagLayout tutorial first, as it's a reasonably complex beast.
If you want them to have the same size then just add the buttons to a GridLayout and they will automatically be sized to the largest text string. You can also specify a gap between components.