JavaFX Getting TreeView's prefWidth - java

I've been trying to determine a TreeView's preferred width but all I get when I call getPrefWidth() is -1. Is there a reason for that? For me the preferred width would be the width of the widest cell. That value must be stored somewhere since scrollbars are displayed when the TreeView doesn't have enough space to show all of it's content.

Related

How do I resize labels, text fields, etc. in the IntelliJ Java Swing form?

So, for example, I created a text field.
Tried to resize it
But once I release the mouse, it gets back to its original size
So how do I do it? How do I resize elements? And yes, I tried to change the minimum size, maximum size, preferred size but it does not work either, nothing happens.
You can do setLayout(null) on the parent or setPreferredSize on the components, either of which will allow you to resize your Components. But the best answer is to set your Font size to a larger size (setFont(...)) which will cause them to become bigger (have larger values in getPreferredSize) automatically.
JTextField tf = ...;
tf.setFont(tf.getFont().deriveFont(tf.getFont().getSize() * 2));

javafx - Scale horizontal length according to horizontal length of scrollpane

So I have this FXML code here (generated via SceneBuilder):
http://pastebin.com/0mjBh9s7
The problem is that I want the content inside the GridPane to scale horizontally according to the horizontal size of the Scroll Pane, but the scaling does not work.
Any solution to this problem?
You do not scale anything in that fxml, nor does scaling work, since it does not influence the layoutBounds property that ScrollPane uses to determine the size of the content.
For this reason you'd need to wrap the GridPane in a Group for the ScrollPane to see the size of GridPane including scale transformations.
If you do not want to scale the content, but simply resize it to the viewport size of the ScrollPane, setting the fitToWidth property to true would be the appropriate way to handle this.

max size of JLabels being overridden by text width?

I have a JTable in which one column represents the size occupied by a folder on disk. Each row represents one folder, and this column has a list of rectangles; the width of each rectangle represents a percentage of the size occupied by one type of file. Here's a picture:
I do this by creating a JLabel for each file type in a row, then, in the getTableCellRenderer() method, I have an array of JLabels, one per file type. That method sets the preferred and maximum sizes of the JLabel to be the column height and the percentage of the column width for each label.
My problem comes when the picture is narrowed; the following was made by narrowing the frame of the program illustrated above:
As you can see, the percentage representation is gone, and the width of the JLabels now appear based on the width of the text, not on the preferred/max sizes set. If I narrow the window further, the percentage calculations again appear to take precedence; it is only in a narrow width range that I see this behavior.
Is there another way to do this? Do I have to draw rectangles and use drawText() instead of using the FlowLayout and text elision that already exists?
Here's the code that sets the characteristics of the JLabels:
sizeBlockDimensions[i].setSize(newWidth, rowHeight);
sizeBlockLabels[i].setPreferredSize(sizeBlockDimensions[i]);
sizeBlockLabels[i].setMaximumSize(sizeBlockDimensions[i]);
sizeBlockLabels[i].setBackground(color);
sizeBlockLabels[i].setText(labelText);
sizeBlockLabels[i].setVisible(true);
i++;
instead of using the FlowLayout
You should be able to use the Relative Layout. It was specifically designed for relative sizes and should adjust automatically as the space available changes.

Java FX TextArea ScrollLeft Precision with Backspace

In the TextArea widget of Java FX 2.2, the method getScrollLeft is described in the documentation as returning
The number of pixels by which the content is horizontally scrolled.
More precisely, it accesses the value of the property ScrollLeft which has the above description.
When I slide either the horizontal or vertical scrollbar, this variable is adjusted, as expected. However when I press "backspace" on a line that is longer than the screen width such that the scroll amount decreases, or at the first character of a row, the property is not adjusted even though the scroll position changes.
A hacky way to fix this is to access the scrollbar directly, get how much of it is being scrolled, and calculate the appropriate value based on that information. I'd prefer not to do this.
Is there an accepted way to solve this problem, or am I misusing the scrollbar information?

GridBagLayout manager and resizing controls

I'm not sure if GridBagLayoutManager is the only layout manager that does this, but here is my problem. I have 4 controls layed out horizontally in a GridBagLayout. To keep things simple for this example, each control get's an equal 1/4 of the form, and each control resizes with the same ratio as the other controls. The four controls are JTextField's where each text field maps to a column in a record from a ResultSet. Additional controls on the form allow one to navigate through the records.
If I navigate from one record to the next, then the text fields update their text to show the new data. However, the text fields also get automatically resized in proportion to the amount of text they are showing... text fields with a large amount of data expand in size, and text fields with a smaller amount of data get squished. If I run through 10 records in a ResultSet, then the controls are always resizing themselves and it looks quite bizarre to say the least.
What I would like to do is prevent these controls from resizing, unless (and until) the underlying container gets resized. So if I resize the window, then I would like the controls to resize (according to the "weight x" variable in the Layout), but I don't want the controls to resize just because the amount of text they are showing becomes more or less.
Anybody have any ideas here?
For JTextField (as mentioned in the contents) call setColumns(int) to set a preferred size on the text.
For JComboBox, call setPrototypeDisplayValue(Object) which will cause that value to be rendered and the preferred size of the JComboBox will be set based on that value.
In general, you can call setPreferredSize(Dimension) on any component directly to get the same behavior. General if not set the value is calculated based on some defaults on the component. What is happening with JTextField, JComboBox, and most JTextComponent derivatives. is that the preferred size on those components is driven by values the user is capable of changing (the text values, the combobox selection). Whereas with most other component (JButton, JCheckBox, etc) the content size doesn't really change when the user acts on it. Setting the columns and rows and the prototype display value fixes the value used to calculate the preferred size.

Categories

Resources