I'm creating a custom Codenameone component that has variable size. The component displays a fixed amount of text, spanning multiple lines. Thus the width and height are related. If the width is small less text fits on a line and more lines are needed. I've managed to set preferred width and preferred height. The trouble is that when the actual painting occurs the width is apparently given by the graphics object graphics.getClipWidth() and doesn't match my preferred width. Thus my preferred height (which appears equal to graphics.getClipHeight() when painting) is incorrect as the clip width is unequal to my preferred width, which was used to calculate the preferred height. How can I get graphics.getClipHeight() to have the correct height? In other words, is it possible to specify the height of the component using the width the component will actually have when rendering?
Best wishes
Marc K
That's the exact problem we have with the TextArea and I'm afraid there are no easy answers. E.g. if the component provides a preferred size of X the layout manager might decide to allocate size differently thus requiring a different layout.
This was a conscious design choice we made when building Codename One, in order to accomplish this you will need to "reflow" effectively recursing thru the hierarchy recalculating sizes/dimensions until you get the best fit. These algorithms are by definition slow and harder to benchmark (since its hard to predict how reflow will behave on the device with varying fonts, paddings etc.).
In the TextArea we have a "poor mans reflow" that triggers revalidation if the width doesn't seem right. This has been a source of many problems in TextArea.
You can see this in the TextArea source, search for the setShouldCalcPreferredSize calls.
Related
As part of a project, I have a custom JComponent that implements a piece of a user interface. It's a rather strange control involving moving around a set of x-y points, but that shouldn't really matter, because that has been implemented and works properly.
This component has a usable area which is square -- any excess area is letterboxed away.
How can I tell a layout manager that I want this component to be square, of whatever size the layout manager decides is appropriate? It doesn't break functionality of extra area is allocated, but it does waste space that could better be used by other components.
You have only three knobs to tell the LayoutManager what your component wants:
getPreferredSize();
getMinimumSize();
getMaximumSize();
Note that not all LayoutManagers honor (especially the latter two) those hints. Apart from that the client using the component in a Layout has much more options with some LayoutManagers (e.g. GridBagLayout) to hint if and how the control is to be resized.
So in short, return a proper preferred size and leave the rest to the layout using the component.
This is a little crude but may be required. You can readjust the size according to what the user changed it to. For example, if the user adjusts the size to 500x600 set it to 500x500.
This doesn't seem like it should be very hard but I can't figure out how to do this:
I have a subclass of JPanel. It has a fixed height, but can be any width. The subclasses, on construction, set their preferred size using setPreferredSize(), which means I have to provide a width in addition to the height.
I would like to make a scrolling list of some number of my subclass, where the subclasses all fill the available horizontal space.
Right now I have a scroll view containing a JPanel containing my subclasses. The containing JPanel uses a BoxLayout with a vertical orientation.
Vertically, it looks great. Horizontally, my custom panels are just stuck at the preferred size. What would be the easiest way to make my panels fill the available horizontal space? I tried writing some layout listeners for them, but the performance was flakey (it seems that sometimes the event messages get dropped?) and the code looked hacky. Other views, such as the JList, JTree, and scroll views seem to resize automatically to fill the available space in a BoxLayout, so I feel like there must be something I can do in my JPanel subclass that I haven't thought of.
I'm open to using another Layout Manager if something else is better suited for this. I looked at GridBagLayout, but that seemed more geared towards static layouts where components aren't added and removed at runtime.
Edit: I found this on Oracle's Documentation for BoxLayout which has an example that looks exactly like what I want:
What if none of the components has a maximum width? In this case, if all the components have identical X alignment, then all components are made as wide as their container. If the X alignments are different, then any component with an X alignment of 0.0 (left) or 1.0 (right) will be smaller. All components with an intermediate X alignment (such as center) will be as wide as their container. Here are two examples:
Could someone show me the code that will produce the same results? The example code in the documentation doesn't look like it covers this particular picture.
When you put your component in a JScrollPane, your component may implement the Scrollable interface to adjust the scrollpane’s behaviour. By doing this you can implement the method getScrollableTracksViewportWidth() to return true so your component will always have the available width and be scrollable in vertical direction only.
This is how JList, JTable, JTree, and all the text components of Swing do it.
Alright - I found the problem. It appears that the default implementation of getMinimumSize() and getMaximumSize() will simply return the value of getPreferredSize() if it is set. So by setting a preferred size without a maximum size, my maximum size was the preferred size. By overriding getMaximumSize() to return (99999, preferredHeight), it now works exactly as I want.
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%)".
I have a JTree on a JScrollPane as part of my GUI. I've set up an AncestorListener where I respond to the ancestorAdded event. In the event, I would like to automatically expand as many visible nodes as possible. By visible, I mean that I want to expand as many nodes as possible such that the total height of the tree does not exceed the maximum height of the JScrollPane's viewport. If my viewport had 500px of visible space, I want the tree expanded until the total expanded height is less than or equal to 500px.
I've tried to achieve this a few different ways. It boils down to this: I can't seem to retrieve an updated height for the tree after programatically expanding a node. How can I recalculate the height of the tree? tree.getHeight(), for example, always returns the same value even after I expand a node. Invoking tree.invalidate() between calculations didn't help either.
How can I recalculate the height of the tree?
Maybe getVisibleRowCount()*getRowHeight() will give you the correct size.
The documentation for getRowHeight() states that it may return <= 0 and leave the row height up to the renderer, which may be the case for you as you're using a JTreeTable.
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()