How one can adjust minimum width of the JavaFX 8 (8_45) Label control item according to its content? To this very moment I had to adjust size of my GUI components manually, in order to be sure, that their content will be visible no matter what will happen with the size of its parent (eg. HBox, Scene, Stage or whatever), ie.:
Label label = new Label("Foo foo foo");
label.setMinWidth(someMinValue);
Is there a way to make Label or any other JavaFX control item to "listen" its content and adjust its width to it automatically? Thank you in advance.
If you want to make sure your label stays big enough for the text it holds, you probably want to set its minimum size to track its preferred size:
label.setMinWidth(Region.USE_PREF_SIZE);
Related
I have a frame with a border layout that includes a panel with a border layout at its center that has a label in its center that presents images.
Another thread is accessing that label to portray images that can be seen as a video - this works.
However, the frame itself is sized this way every time:
https://prnt.sc/116grry
I have to resize it manually to show the image every time I run the program.
SetPreferredSize doesn't change or do anything to achieve what I want.
The code:
https://gist.github.com/IshayKom/d06f40980fe42f96e28acdf1422b9e4f
Is there any way to initiate the frame at a specific size before it gets the images?
You might be looking for .setMinimumSize instead. setPreferredSize does not provide constraints and can thus be changed by other components such as the container that is used to show the image.
I ave tried the following code:
Label label = new Label(reallyLongString, skin);
label.setWrap(true);
label.setWidth(100); // or even as low as 10
table.add(label);
...
and yet all I get is a very wide line that draws off the screen. How to get a Label with wrapped text?
This is the same issue as seen in slider always has default width or "Slider always has default width".
You need to put that label into a table and add the right size to the cell of the table where the label is.
UI widgets do not set their own size and position. Instead, the parent widget sets the size and position of each child. Widgets provide a minimum, preferred, and maximum size that the parent can use as hints. Some parent widgets, such as Table, can be given constraints on how to size and position the children. To give a widget a specific size in a layout, the widget's minimum, preferred, and maximum size are left alone and size constraints are set in the parent.
Source: From the libgdx wiki Scene2D
The solution:
Label label = new Label(reallyLongString, skin);
label.setWrap(true);
label.setWidth(100); // or even as low as 10
table.add(label).width(10f);// <--- here you define the width
I've found that the following code can solve the issue without any table or wrapping container (for libgdx-1.9.6):
label = new Label("Some label", skin);
label.setPosition(600, 50);
label.setWidth(200);
label.setHeight(50);
label.setWrap(true);
stage.addActor(label);
If you just want to specify the preferred size for a single widget, wrap it with a Container.
https://github.com/libgdx/libgdx/wiki/Scene2d.ui#layout-widgets
https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/ui/Container.html
Something like:
Container<Label> labelContainer = new Container(myLabel);
labelContainer.prefWidth(200.0f);
Keep in mind that the actual size will vary depending on the container hierarchy - for example, the labelContainer above will display differently if placed in another layout object.
The size will also vary depending on the viewport, etc.
How would one test if a JLabel, with set size, wouldn't be able to display all the text that it set to display?
Make another label that you do not set the size of.
Add the same text to the 2nd label.
Call for the preferred size.
If it is larger than the set size, your text will be truncated.
But the most sensible solution is not to set the sizes of labels in the first place.
See also this example.
I want my JTextArea to resize itself (expand vertically) when the last line (that the text area's height can offer) is reached and the user wants to start a new line. You know, like the textbox in MSWord.
I have an idea to use getLineCount() and determine (if necessary) the new height of the JTextArea. Do you have, or know of better approaches for implementing this?
Actually, the JTextArea always has the correct size so all lines of text are visible. What you experience is probably that you wrapped the text area in a JScrollPane. Just omit the scroll pane and make the text area a direct child of the container.
Another solution is to listen to resize events of the text area and size the scroll pane accordingly. This way, you can grow to a certain size and then start to display scroll bars (for example, when someone pastes 500KB of text into the text area).
I had the same problem. From my tests, I do not believe that the JTextArea sets its size dynamically. Instead, its size seems to be limited by its container (a JPanel in my case). However, the JTextArea does change its preferred size based on the text it contains. From the documentation:
java.awt.TextArea has two properties rows and columns that are used to determine the preferred size. JTextArea uses these properties to indicate the preferred size of the viewport when placed inside a JScrollPane to match the functionality provided by java.awt.TextArea. JTextArea has a preferred size of what is needed to display all of the text, so that it functions properly inside of a JScrollPane. If the value for rows or columns is equal to zero, the preferred size along that axis is used for the viewport preferred size along the same axis.
Go to JTextArea "Properties" - checklist "lineWrap".
I had the same problem,I put the JTextArea into a JScrollPane and set the preferred size of JTextArea, and I believe that's the cause of the problem.
So the right solution is to put the JTextArea into a JScrollPane, and don't touch the preferred size of JTextArea, set JScrollPane's instead.
I'm writing a custom file selection component. In my UI, first the user clicks a button, which pops a JFileChooser; when it is closed, the absolute path of the selected file is written to a JTextField.
The problem is, absolute paths are usually long, which causes the text field to enlarge, making its container too wide.
I've tried this, but it didn't do anything, the text field is still too wide:
fileNameTextField.setMaximumSize(new java.awt.Dimension(450, 2147483647));
Currently, when it is empty, it is already 400px long, because of GridBagConstraints attached to it.
I'd like it to be like text fields in HTML pages, which have a fixed size and do not enlarge when the input is too long.
So, how do I set the max size for a JTextField ?
It may depend on the layout manager your text field is in. Some layout managers expand and some do not. Some expand only in some cases, others always.
I'm assuming you're doing
filedNameTextField = new JTextField(80); // 80 == columns
If so, for most reasonable layouts, the field should not change size (at least, it shouldn't grow). Often layout managers behave badly when put into JScrollPanes.
In my experience, trying to control the sizes via setMaximumSize and setPreferredWidth and so on are precarious at best. Swing decided on its own with the layout manager and there's little you can do about it.
All that being said, I have no had the problem you are experiencing, which leads me to believe that some judicious use of a layout manager will solve the problem.
I solved this by setting the maximum width on the container of the text field, using setMaximumSize.
According to davetron's answer, this is a fragile solution, because the layout manager might disregard that property. In my case, the container is the top-most, and in a first test it worked.
Don't set any of the sizes on the text field. Instead set the column size to a non-zero value via setColumns or using the constructor with the column argument.
What is happening is that the preferred size reported by the JTextComponent when columns is zero is the entire amount of space needed to render the text. When columns is set to a non-zero value the preferred size is the needed size to show that many standard column widths. (for a variable pitch font it is usually close to the size of the lower case 'm'). With columns set to zero the text field is requesting as much space as it can get and stretching out the whole container.
Since you already have it in a GridBagLayout with a fill, you could probably just set the columns to 1 and let the fill stretch it out based on the other components, or some other suitably low number.