Image priority Java - java

I'm putting images on my JFrame using ImageIcon and then, to display the images:
getContentPane().add(label[3]);
getContentPane().add(label[2]);
getContentPane().add(label[1]);
Now Image 3 will have priority over 2 and 2 over 1 etc
Is there anyway to set the priority or reverse it so the last image I put on screen will have priority over the other images?

Your question is not clear at all, if you want to put the images along a third dimension axis, you should take a look at this layered panes tutorial.
From the official documentation:
A layered pane is a Swing container that provides a third dimension
for positioning components: depth, also known as Z order. When adding
a component to a layered pane, you specify its depth as an integer.
The higher the number, closer the component is to the "top" position
within the container. If components overlap, the "closer" components
are drawn on top of components at a lower depth. The relationship
between components at the same depth is determined by their positions
within the depth.
As shown in this image, you should be able to position your components on the top of the others.
If this does not meet your requirement, please edit your question to be more specific.

Related

JavaFX Canvas vs. Node+Binding performance

I have a use case where I need to draw a component consisting of around 100 lines and up to 1000 other elements (rectangles and text, mostly).
The content of the component should resize when the Parent node changes its size (e.g. when the application window changes its size).
I can think of two approaches how to deal with this:
Use JavaFX Line, Text, Rectange etc. Nodes and bind the widths and heights to the parent Node.
As in 1. but, instead of binding, develop a Pane which layouts the elements accordingly
Use the Canvas node and draw everything like with Graphics2D in Swing.
Which approach would be faster? How large is the impact of using bindings to change the size of this number of Nodes?
Is it possible to ensure, using the Node+Binding approach, that the lines are crisp? AFAIK the lines need to be drawn subpixel to be crisp, so adding 0.5px to the position is necessary. But is it possible to do this conditionally in a Binding? I already tested adding 20 Lines to the Pane and Binding their y-position to the height of the Parent, but some are drawn crisp and some are blurry.
If creating a new container which layouts its children (like VBox and so on) is the best option, how complex would be the task?

Java swing GridLayout adjusting cellsize

After starting a JPanel using GridLayout(4,4) i insert a JLabel (and attach an imageicon to it) inside every grid cell with size of (150,150).
when i resize the JLabel to size (100,100) the image get cropped (which is perfectly fine by me), but i get a wierd looking grid (imaged added at the end).
if this helps: i dont actually resize the window, i just need to make sure the the size of the JLabel is set to (100,100) always, no metter what is the original image size.
before:
http://postimg.org/image/iolyeb8e7/
after:
http://postimg.org/image/5j6g87ein/
thanks
Unfortunately you did not say what you expect the grid to look like. I assume you don't want the cells to be so far apart from each other.
The GridLayout documentation states that...
The container is divided into equal-sized rectangles, and one component is placed in each rectangle.
If you shrink the size of each JLabel (i.e. the components in each of those rectangles) you just do that. You shrink the size of the component, not that of the rectangle. The grid does not care if the component is to small to fill the whole rectangle. At the moment you add the component to the grid1, the grid tries to set the components size to best fit the available space. But if you later change the labels size, the grid does not care.
What you probably want is to change the size of the whole grid: If you set the grids size to 400 by 400 it should evenly divide it to all 4 rows and 4 columns, so you get rectangles of size 100 by 100. All labels should automatically be sized accordingly.
1 Probably it is not exactly while adding the labels but while validating the container, but I don't know all the internal details about how and when layouts do there magic.

How can I add buttons on a picture?

I am currently creating a GUI for a game. I am currently loading an image using JLabel onto my JPanel, I was wondering whether it would be possible to add buttons on various parts of the image e.g. (5,7) and (12,12).
If you wish to have JButtons appear over your image, a better approach would be to override the paintComponent of the JPanel and use drawImage to draw the image similar to this example . This will allow you to add components to the container.
Absolute positioning(null layout) is generally frowned upon for setting component locations, however. This DragLayout should be a better substitute taking care of component sizing.
DragLayout was designed to replace a null layout. It will respect the location of a component. By default it will use the preferred size of the component to determines its size. Finally, it will automatically calculate the preferred size of the Container.

Fixed percentage based grid with MiGLayout

I am trying to create a grid with MiGLayout that is enforced on its children. This means that if I insert a child into grid position (1,1) and the grid's size is [10%!] that this child must NOT be bigger and overlap other cells. The child must be shrunk to fit the Grid cell.
This is what I have so far:
new MigPane("", "[5%!][20%!][5%!][65%!][5%!]", "[45%!][50%!][5%!]");
Now, I insert a big component (a picture that I have no control over) in Grid 1,1, like this:
migPane.add(myImageView, "cell 1 1, width 100%!");
However, that does not seem to restrict the ImageView at all.
How do I tell MiGLayout that I want "myImageView" to be put in grid 1,1 and size it to fit? Is there a "fit" keyword? :)
Note that specifying anything with pixels/points/mm/cm/inches is NOT what I want. My app always runs full-screen and must scale seamlessly (it is not a traditional form app, it is a video system using JavaFX).
It looks like percentages are supported, according to the docs:
Overrides the default size of the component that is set by the UI
delegate or by the developer explicitly on the component. The size is
specified as a BoundSize. See the Common Argument Types section above
for an explanation. Note that expressions is supported and you can for
instance set the size for a component with "width pref+10px" to make
it 10 pixels larger than normal or "width max(100, 10%)" to make it
10% of the container's width, but a maximum of 100 pixels.
Maybe try something like: "width max(100%, 100%)".

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()

Categories