Is it possible to use USE_COMPUTED_SIZE for the prefered width of a TableColumn? I have tried but my column then disappear.
Maybe that it is nonsense?
<TableView id="my-table" fx:id="tagTable" editable="true" stylesheets="#My_Theme.css">
<columns>
<TableColumn fx:id="typeColumn" editable="false" maxWidth="-Infinity" minWidth="-Infinity" prefWidth="-1.0" resizable="false" sortable="false" text="Type" />
<TableColumn fx:id="contentColumn" editable="false" maxWidth="-Infinity" minWidth="-Infinity" prefWidth="-1.0" resizable="false" text="CONTENT" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
<placeholder>
<Label styleClass="label-dark" text="Nothing to display">
<padding>
<Insets bottom="200.0" />
</padding>
</Label>
</placeholder>
</TableView>
How I do it, which might not be "the" way you should or want to do it is:
I set all the TableColumns to have the Min and Pref- Width set to USE_COMPUTED_SIZE while the Max Width have the default value of 5000. (I tried setting Max Width to MAX_VALUE but that just ends up with the first column taking up the entire table, and I haven't looked up why that is yet since it works and that it good enough for me :) ).
Also, for the TableView I have the Column Resize Policy set to "constrained-resize" just as you have.
In the image below you can see the results I get, which is 5 evenly sized columns:
I hope that this might be of some help, and that others may comment if there is a better way or a way in which you can set the Max Width to MAX_VALUE.
Edit: I'm using Java 8 (1.8.181).
Related
I set the font size of upper label to 24px.
I don't know where I can check the real height of element (if it's possible in the Scene builder), but there is the extra space in upper (designated in the image) and lower part of box.
The line spacing is 0.
The parent VBox has no top padding.
All label sizes are computed.
I need the height of label equals to font size.
I mean FontSize = Gray space surrounded by blue markers height.
How to reach it?
I have faced with similar problem HTML/CSS.
There, not simple but working solution for most cases is negative margins and :before and :after pseudo elements.
Now which solutions are exists for JavaFX?
The repro
🌎 Repository
Open the resources/static/TasksOverview.fxml by Scene Builder.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="640.0" prefWidth="320.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<HBox alignment="CENTER_LEFT">
<children>
<CheckBox mnemonicParsing="false" />
<VBox>
<children>
<Label style="-fx-font-size: 24px;" text="Wake up">
<VBox.margin>
<Insets />
</VBox.margin>
</Label>
<Label style="-fx-font-size: 14px;" text="... and fly. I need more text to test multiline mode." wrapText="true" />
</children>
<HBox.margin>
<Insets left="12.0" />
</HBox.margin>
</VBox>
</children>
<padding>
<Insets left="12.0" right="12.0" />
</padding>
</HBox>
<HBox alignment="CENTER_LEFT" layoutX="10.0" layoutY="10.0">
<children>
<CheckBox mnemonicParsing="false" />
<VBox>
<children>
<Label style="-fx-font-size: 24px;" text="Wash face">
<VBox.margin>
<Insets />
</VBox.margin>
</Label>
<Label style="-fx-font-size: 14px;" text="... with cold water." wrapText="true" />
</children>
<HBox.margin>
<Insets left="12.0" />
</HBox.margin>
</VBox>
</children>
<padding>
<Insets left="12.0" right="12.0" />
</padding>
</HBox>
</children>
</VBox>
Labels are "smart" controls that include, within the "skin" code, stuff like some in-built padding, as well as various alignment and spacing options for text and graphics, and support for resizing by dynamically eliding text.
If you want some more control over how text layout happens, then you can use raw Text nodes instead of Labels and then do some of the additional layout yourself if needed. Of course you are going to lose a lot of in-built functionality that way (buyer beware...), but sometimes that might be what you want.
For example, a Text node with the following attributes:
A font size of 24.
No spacing on the top, left or right of the visual area of the text.
A margin spacing of 3 pixels below the visual area of the text when placed in a VBox.
can be defined in FXML as below:
<Text boundsType="VISUAL" text="Visual bounds != Logical bounds">
<font>
<Font size="24.0" />
</font>
<VBox.margin>
<Insets bottom="3.0" />
</VBox.margin>
</Text>
If you use this stuff on a lot of text, then you can set a lot of the values in a style sheet rather than FXML (which would be preferable). I won't describe how to do that here other than to say there is an undocumented -fx-bounds-type css attribute for text.
Sometimes rather than using a Text node, it might be better to just "hack" a Label similar to Matt's answer by using stuff like negative insets (that is usually my preferred option...).
Be careful, esoteric discussion follows, get ready to ignore it ;-)
A key part is setting the boundsType to VISUAL. By default the bounds of the text are usually a bit more than what you actually see, which eases alignment issues, because that means the bounds of the letter "a" and the letter "A" are the same. That kind of bounds is the default and is called LOGICAL bounds. VISUAL bounds makes the bounds of those two letters different, so "a"'s bounds is smaller than "A"'s bounds, which is what allows stuff to not take up any more space than it absolutely needs to.
For example, using Text with VISUAL bounds and instead of a Label, gets rid of two sets of the extra whitespace around the text which appears on your example screen shot. One set of whitespace is allocated by the Label control's internal layout algorithm, another set of whitespace is allocated for the LOGICAL bounds.
Setting bounds to VISUAL is possible within a label by applying an appropriate CSS stylesheet, but if you do that, then the label still won't shrink to fit just the VISUAL bounds because it seems to be coded to work based off of LOGICAL bounds, at least that is what some quick testing I tried seemed to show. So using VISUAL bounds in text in a label just seems to make it display incorrectly (which may even be classified as bug by some people). It might work if you set a graphic with Text inside the label, but if you do that, the label doesn't do much for you and may as well just use a Text node directly (unless you are using a control like a TitledPane which includes a Label in the title, in which case you must use a Label).
Just set add a Negative inset and it will shrink the space above your label for example:
<Insets top="-5.0"/>
Full FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="640.0" prefWidth="320.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<HBox alignment="CENTER_LEFT">
<children>
<CheckBox mnemonicParsing="false" />
<VBox>
<children>
<Label style="-fx-font-size: 24px;" text="Wake up">
<VBox.margin>
<Insets top="-5.0"/>
</VBox.margin>
</Label>
<Label style="-fx-font-size: 14px;" text="... and fly. I need more text to test multiline mode." wrapText="true" />
</children>
<HBox.margin>
<Insets left="12.0" />
</HBox.margin>
</VBox>
</children>
<padding>
<Insets left="12.0" right="12.0" />
</padding>
</HBox>
<HBox alignment="CENTER_LEFT" layoutX="10.0" layoutY="10.0">
<children>
<CheckBox mnemonicParsing="false" />
<VBox>
<children>
<Label style="-fx-font-size: 24px;" text="Wash face">
<VBox.margin>
<Insets />
</VBox.margin>
</Label>
<Label style="-fx-font-size: 14px;" text="... with cold water." wrapText="true" />
</children>
<HBox.margin>
<Insets left="12.0" />
</HBox.margin>
</VBox>
</children>
<padding>
<Insets left="12.0" right="12.0" />
</padding>
</HBox>
</children>
</VBox>
I have a TableView inside a JavaFX ScrollPane, together with some other Views parallel to it. Something like this here:
<ScrollPane maxHeight="Infinity" maxWidth="Infinity" VBox.vgrow="ALWAYS">
<GridPane maxHeight="Infinity" maxWidth="Infinity" prefWidth="1100">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="Infinity" minWidth="0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="Infinity" minWidth="0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="Infinity" minHeight="10.0" vgrow="ALWAYS" />
</rowConstraints>
<VBox GridPane.columnIndex="1" GridPane.rowSpan="2147483647">
<PieChart fx:id="amountPerState" legendVisible="false" minHeight="300" minWidth="300" />
<PieChart fx:id="amountPerComponent" legendVisible="false" minHeight="300" minWidth="300" />
</VBox>
<TableView fx:id="componentTable" maxHeight="Infinity" maxWidth="Infinity" GridPane.rowIndex="6">
<columns>
...
</columns>
</TableView>
</GridPane>
</ScrollPane>
Now this double scrolling from the ScrollPane and the TableView is ackward. When I try to scroll inside the TableView the ScrollPane also scrolls. I would love to have the TableView go as high as necessary for it's content, so that there is only the scrolling of the ScrollPane. Any Idea how I can do that? Or other ideas?
Edit: For extra clarification, I assume the TableView should have prefered size so high that all it's content is shown without any scrolling necessary. Then there are probably no scroll bars, or I can just not show them with the policy. This is how I think you would achieve this, but I don't know how to figure out the tables content hight to make it it's preferred size.
I think it will be a bit worse in performance, since TableView has this row reusing, but I know that there are not to many rows in the TableView, so that there will be no performance problems.
Edit: Both the charts and the table are to big for the windows, so they all need to be in the ScrollPane
im having issues with my code, i am a very new guy to javafx and im not quite sure of what im doing at all(I do ofc, just not enough)
I need to find the way to add data to my table (TableView is fxml) with my Controller Class, i have no idea on how to do this, suggest the best way.. i thought of maybe if i could create an object of that TableView based on the fxml table view ...
Help guys...
<TableView fx:id="tableView" prefHeight="200.0" prefWidth="200.0" StackPane.alignment="CENTER">
<columns>
<TableColumn editable="false" maxWidth="90.0" prefWidth="78.0" text="Fecha" />
<TableColumn editable="false" maxWidth="90.0" text="Hora" />
<TableColumn editable="false" maxWidth="90.0" prefWidth="79.0" text="Folio" />
<TableColumn editable="false" maxWidth="90.0" prefWidth="77.0" text="Estacion" />
<TableColumn editable="false" maxWidth="190.0" prefWidth="160.0" text="Circuito" />
<TableColumn editable="false" maxWidth="190.0" prefWidth="160.0" text="Receta" />
<TableColumn editable="false" maxWidth="190.0" prefWidth="160.0" text="Usuario" />
</columns>
<StackPane.margin>
<Insets bottom="8.0" left="8.0" top="8.0" />
</StackPane.margin>
</TableView>
I want to set the SelectionModel of the TableView from the FXML, but I can not find how to do this. I already tried the following:
1.Just set it as a property of the TableView:
<TableView selectionModel="MULTIPLE">
2.Set the property the same as the ListView works (see: https://community.oracle.com/thread/2315611?start=0&tstart=0):
<TableView multiSelect="true">
3.Set the property in a different way:
<TableView>
<selectionModel>
<TableView fx:constant="MULTIPLE" />
</selectionModel>
</TableView>
4.Another version:
<TableView>
<selectionModel>
<SelectionModel fx:constant="MULTIPLE" />
</selectionModel>
</TableView>
5.Selection model (different):
<TableView>
<selectionModel>
<SelectionModel selectionModel="MULTIPLE" />
</selectionModel>
</TableView>
None of this works.
Any help is greatly appreciated!
Should it be possible on FXML this should be the way:
<TableView fx:id="table" prefHeight="200.0" prefWidth="200.0" >
<columns>
<TableColumn prefWidth="75.0" text="C1" />
</columns>
<selectionModel>
<SelectionMode fx:constant="MULTIPLE"/>
</selectionModel>
</TableView>
Unfortunately, when you run it you get an exception:
java.lang.IllegalArgumentException: Unable to coerce SINGLE to class javafx.scene.control.TableView$TableViewSelectionModel.
at com.sun.javafx.fxml.BeanAdapter.coerce(BeanAdapter.java:495)
This is happening because the bean adapter tries reflexively to find in the class javafx.scene.control.TableView$TableViewSelectionModel the valueOf of javafx.scene.control.SelectionMode.MULTIPLE, but it doesn't find it.
There's an unresolved JIRA ticket for this here.
The only working solution I've found, based on that report, is using scripting capabilities:
...
<?language javascript?>
<TableView fx:id="table" prefHeight="200.0" prefWidth="200.0" >
<columns >
<TableColumn fx:id="col" prefWidth="75.0" text="C1" />
</columns>
</TableView>
<fx:script>
table.getSelectionModel().setSelectionMode(javafx.scene.control.SelectionMode.MULTIPLE);
</fx:script>
Which is the same as doing it by code...
I am just trying out JavaFX and am forcing my way into it because it is suppose to be the future. I am trying to create a table with 4 columns. The columns AND THE TABLE should resize to fill the parent pane. I cannot for the life of me get this to work. I have been trying for over 4 hours now and the tableview does not resize.
Here is my FXML file.
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.control.cell.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.collections.*?>
<?import t.cubed.fxml.*?>
<BorderPane styleClass="root" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="t.cubed.fxml.FXMLDocumentController">
<top>
<MenuBar fx:id="menuBar" styleClass="menu-bar">
<menus>
<Menu text="File">
<items>
<MenuItem onAction="#handleOpenAction" text="Open" />
<MenuItem onAction="#handleExitAction" text="Exit" />
</items>
</Menu>
<Menu text="Edit">
<items>
<MenuItem onAction="#handleWeightAction" text="Edit Weights" />
<MenuItem onAction="#handleFilterAction" text="Edit Filters" />
<MenuItem onAction="#handleOptionsAction" text="Options" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
<center>
<GridPane>
<!--
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
</padding>
-->
<TableView fx:id="testTable" GridPane.columnIndex="0" GridPane.rowIndex="0" GridPane.columnSpan="1">
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
<columns>
<TableColumn text="TEST NUMBER">
<cellValueFactory>
<PropertyValueFactory property="testNumber" />
</cellValueFactory>
</TableColumn>
<TableColumn text="TEST NAME">
<cellValueFactory>
<PropertyValueFactory property="testName" />
</cellValueFactory>
</TableColumn>
<TableColumn text="TEST TIME(ms)">
<cellValueFactory>
<PropertyValueFactory property="testTime" />
</cellValueFactory>
</TableColumn>
<TableColumn text="BEST MATCH">
<cellValueFactory>
<PropertyValueFactory property="bestMatch" />
</cellValueFactory>
</TableColumn>
</columns>
<items>
<!--
<FXCollections fx:factory="observableArrayList">
<TestTableModel testNumber="100" testName="Test1234" testTime="0.34" bestMatch="99"/>
</FXCollections>
-->
</items>
</TableView>
</GridPane>
</center>
<stylesheets>
<URL value="#t-cubed.css" />
</stylesheets>
</BorderPane>
How Resizable Layout Works
The size of resizable items in JavaFX is managed by the layout managers in which the items are placed.
What you need to do
For your particular issue, you need to set the GridPane sizing constraints to manage the TableView that you placed in the grid.
See the GridPane.hgrow and GridPane.vgrow constraints I added on the TableView:
<TableView fx:id="testTable"
GridPane.columnIndex="0"
GridPane.columnSpan="1"
GridPane.hgrow="ALWAYS"
GridPane.vgrow="ALWAYS"
GridPane.rowIndex="0">
FXML just reflects the Java API, so you could also do the same in Java source as well; i.e. GridPane.setHGrow(node, priority). Though, if you are using FXML for your layout, defining the layout constraints in FXML is recommended.
Refactored Sample
I loaded your FXML up in Scene Builder 2 and set the appropriate constraints and it appears to resize automatically fine when I use the preview functionality of Scene Builder.
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.control.cell.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.collections.*?>
<?import t.cubed.fxml.*?>
<BorderPane styleClass="root" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="t.cubed.fxml.FXMLDocumentController">
<top>
<MenuBar fx:id="menuBar" styleClass="menu-bar">
<menus>
<Menu text="File">
<items>
<MenuItem onAction="#handleOpenAction" text="Open" />
<MenuItem onAction="#handleExitAction" text="Exit" />
</items>
</Menu>
<Menu text="Edit">
<items>
<MenuItem onAction="#handleWeightAction" text="Edit Weights" />
<MenuItem onAction="#handleFilterAction" text="Edit Filters" />
<MenuItem onAction="#handleOptionsAction" text="Options" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
<center>
<GridPane>
<children>
<!--
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
</padding>
-->
<TableView fx:id="testTable" GridPane.columnIndex="0" GridPane.columnSpan="1" GridPane.hgrow="ALWAYS" GridPane.rowIndex="0" GridPane.vgrow="ALWAYS">
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
<columns>
<TableColumn text="TEST NUMBER">
<cellValueFactory>
<PropertyValueFactory property="testNumber" />
</cellValueFactory>
</TableColumn>
<TableColumn text="TEST NAME">
<cellValueFactory>
<PropertyValueFactory property="testName" />
</cellValueFactory>
</TableColumn>
<TableColumn text="TEST TIME(ms)">
<cellValueFactory>
<PropertyValueFactory property="testTime" />
</cellValueFactory>
</TableColumn>
<TableColumn text="BEST MATCH">
<cellValueFactory>
<PropertyValueFactory property="bestMatch" />
</cellValueFactory>
</TableColumn>
</columns>
<items>
<!--
<FXCollections fx:factory="observableArrayList">
<TestTableModel testNumber="100" testName="Test1234" testTime="0.34" bestMatch="99"/>
</FXCollections>
-->
</items>
</TableView>
</children>
<columnConstraints>
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
</rowConstraints>
</GridPane>
</center>
<stylesheets>
<URL value="#t-cubed.css" />
</stylesheets>
</BorderPane>
Some Advice
You are placing the TableView inside a GridPane. Using a GridPane in your case is a little strange as the GridPane you only place one node in the GridPane. Just placing the TableView directly in the center of your BorderPane or using a simpler layout parent such as a StackPane would have worked just fine. But perhaps this is just some cut down version of a more complex UI, so perhaps that is why you have used a GridPane.
Further Reading
If you have time, read up on layout in JavaFX in the Node, Pane, Group and GridPane javadoc and try this little layout bounds demo too.
Additional Questions for Comments
mention a StackPane would work, but StackPanes don't seem to have Hgrow and Vgrow?
StackPanes don't need a Hgrow or Vgrow constraints. Those constraints set Priorities, which are defined as:
Enumeration used to determine the grow (or shrink) priority of a given
node's layout area when its region has more (or less) space available
and multiple nodes are competing for that space.
With a StackPane, all children are layered on top of each other, they don't compete for space, so such growth priority settings for child nodes are not applicable.
After some homework, we came up with some qualifying information on FXML containers. Pretty much for the same reason given above by #user3403469. In the Scene Builder, there's a long list of 'containers'. Imho, only these 9 elements and Pane qualify as layout container, as they inherit from
<Pane> ... parent for all Pane classes. While both, Pane and Control inherit from Region only the Pane child-classes have layout properties.
* <AnchorPane>
* <BorderPane>
* <FlowPane>
* <GridPane>
* <StackPane>
* <TextFlow>
* <TilePane>
* <HBox>
* <VBox>
You can consider that Control child-classes more or less inherit Events and actions. Layout like things need to come from the encapsulating region, and the only thing with layout elements available to wrap a 'Control' is-a 'Pane'.
javafx.scene.layout.Pane
These FXML elements would be the bespoke 'layout managers' described in the #jewelsea's answer and elsewhere.
I guess 'Control' containers are still containers in FXML. It seems that they need to be 'inside' a panel to do good ;-)
Corrections and comment welcome.
related:
Percent width for TableColumn in JavaFX 2.x TableView
There seems to be a lot of code. My idea of dynamic resizing is a Zero-tolerance for code to do scaling and resize. Gee we left that behind in Windows 1.3 didn't we?
where is the: FXML Specification?
Reference and Guidelines for Dynamic Layout using JavaFX