Dynamically change ListCell's width of ListView in JavaFX - css

this is the interface of a chat app using javaFX. The chat window is a ListView component and I'm using CSS to polish it a little bit. Here are the CSS and the screenshot:
.list-cell {
display: inline-block;
-fx-min-width: 50px;
-fx-background-color: lightyellow;
-fx-background-radius: 30px;
-fx-border-radius: 20px;
-fx-border-width: 2px;
-fx-border-style: solid;
-fx-border-color: #666666;
}
.list-cell:empty {
-fx-background-color: transparent;
-fx-border-width: 0px;
}
The problem is that I want to change the width of each listCell depending on the length of text. I've tried to set minWidth prefWidth (for example 50px) in the CSS file or using listView.setCellFactory but nothing happens. The length does not change at all. I wonder that can I change the length of each cell in a listView like that or not (or in order to do that I must use something like an HBox/VBox/seperator...)

Use a cell factory that sets the graphic to a Label, and style the label. E.g.:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.stage.Stage;
public class FormattedListCell extends Application {
#Override
public void start(Stage primaryStage) {
ListView<String> listView = new ListView<>();
listView.getItems().addAll("One", "Two", "Three", "Four");
listView.setCellFactory(lv -> new ListCell<String>() {
private final Label label = new Label();
#Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
label.setText(item);
setGraphic(label);
}
}
});
Scene scene = new Scene(listView, 400, 400);
scene.getStylesheets().add("formatted-list-cell.css");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
and modify the stylesheet:
.list-cell .label {
display: inline-block;
-fx-min-width: 50px;
-fx-background-color: lightyellow;
-fx-background-radius: 30px;
-fx-border-radius: 20px;
-fx-border-width: 2px;
-fx-border-style: solid;
-fx-border-color: #666666;
}
.list-cell:empty .label {
-fx-background-color: transparent;
-fx-border-width: 0px;
}
You may need to actually style the list cell (as well as the label inside it) to get the exact style you want, but this should get you started.
Here is a more complete CSS file, which uses -fx-background so that the text color will automatically adjust, manages the selection color, and also adds some styles to the list cell itself:
.list-cell {
-fx-background-color: transparent ;
-fx-padding: 0 ;
}
.list-cell .label {
display: inline-block;
-fx-background: lightyellow;
-fx-background-color: -fx-background ;
-fx-background-radius: 30px;
-fx-border-radius: 20px;
-fx-border-width: 2px;
-fx-border-style: solid;
-fx-border-color: #666666;
-fx-padding: 12px ;
}
.list-cell:empty .label {
-fx-background-color: transparent;
-fx-border-width: 0px;
}
.list-cell:selected .label {
-fx-background: -fx-selection-bar ;
}

Related

How to set space between rows in a JavaFX TreeTableView?

I'm trying to allow 5 pixels of space between rows on a JavaFX TreeTableView. I figured that changing the margin, padding, or border width on one of the CSS elements would fix it, but either nothing happens or the text in the table cell is moved around.
I see that there might be solutions for TreeViews and TableViews, like the StackOverflow link below, but they don't appear to apply to TreeTableViews.
How to set space between TableRows in JavaFX TableView?
My Current CSS:
.tree-table-view {
-fx-table-cell-border-color: transparent;
-fx-background-color: black;
}
/* TreeTableView */
.tree-table-view .column-header,
.tree-table-view .column-header-background .filler {
-fx-background-color: black;
-fx-border-width: 100;
}
.tree-table-cell{
-fx-text-fill: white;
-fx-wrap-text: true;
}
.tree-table-row-cell .tree-disclosure-node .arrow {
-fx-shape: null;
-fx-background-color: null;
-fx-min-width: 0;
-fx-pref-width: 0;
-fx-max-width: 0;
-fx-min-height: 0;
-fx-pref-height: 0;
-fx-max-height: 0;
}
.tree-table-row-cell:expanded .tree-disclosure-node .arrow {
-fx-shape: null;
-fx-background-color: null;
-fx-max-width: 0;
-fx-max-height: 0;
}
.tree-table-row-cell{
-fx-background-color: black;
-fx-font-size: 12px;
-fx-indent: 0;
-fx-cell-size: 50px;
}
.group-tree-table-row-cell{
-fx-background-color: darkgray;
}
.entry-tree-table-row-cell{
-fx-background-color: darkslategray;
}
.tree-table-row-cell:selected{
-fx-background-color: darkblue;
-fx-table-cell-border-color: transparent;
}
You will want to apply your style to the tree-table-cell style class (Documentation can be found here: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/doc-files/cssref.html#treetablecell)
Applying a border width of 5 at the bottom of each cell in a column can give you the desired effect without shifting the text. This can be done by adding the following to your style sheet.
.tree-table-cell {
-fx-border-width: 0 0 5 0;
}
This can also be done by setting a custom cell factory for columns of the TreeTableView and providing the cells with the desired settings. See TreeTableColumn#setCellFactory(Callback<TreeTableColumn<S,T>,TreeTableCell<S,T>> valuehttps://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TreeTableColumn.html#setCellFactory-javafx.util.Callback-).

JavaFx context menu styling with css - background color on full width

I want to style my context menu but I can't get rid off white area between menu-item and context-menu border, see example.
rendered context menu with css styles
I want that color fils whole area up to context menu border like this
ecpected styling
My css styles
.context-menu {
-fx-background-color: #FFFFFF;
-fx-effect: null;
-fx-border-color: #D6D9DF;
-fx-border-width: 0.5;
-fx-border-radius: 4;
-fx-background-radius: 4;
-fx-background-insets: 0, 1;
}
.menu-item {
-fx-padding: 0;
-fx-pref-height: 28px;
}
.context-menu .separator {
-fx-padding: 0;
}
.menu-item .label {
-fx-font-size: 12px;
-fx-padding: 6 16 8 12;
-fx-text-fill: #2D3845;
}
.menu-item:focused {
-fx-background-color: transparent;
}
.menu-item:hover {
-fx-background-color: #EAECEF;
}
.menu-item:pressed {
-fx-background-color: #D6D9DF;
}
have you tried adding
yourcontextmenuname.getStyleClass().remove("context-menu");
and then change your CSS to refer yourcontextmenuname by ID. If that does not work do the same with menu-item.

JavaFX TableView rectangle between row and scrollbar

I have an issue with styling my TableView, because when I open new window with TableView it has something like on the picture below:
I don't know what causes it. When I click on another row in the table it's gone:
I also attach my CSS for table-view below:
.table-view{
-fx-background-color: -fx-color-navyBlue;
}
.table-view .filler{
-fx-background-color: -fx-color-navyBlue;
}
.table-view:focused{
-fx-background-color: -fx-color-navyBlue;
}
.table-view .column-header-background{
-fx-background-color: -fx-color-navyBlue;
-fx-border-color: -fx-color-navyBlue;
}
.table-view .column-header-background .label{
-fx-background-color: -fx-color-darkGray;
-fx-text-fill: -fx-color-orange;
}
.table-view .column-header {
-fx-background-color: -fx-color-darkGray;
-fx-border-width: 2;
-fx-border-color: -fx-color-navyBlue;
}
.table-view .table-cell{
-fx-text-fill: -fx-color-lightGray;
-fx-alignment: center;
-fx-border-width: 2;
-fx-border-color: -fx-color-navyBlue;
}
.table-view .table-row-cell{
-fx-background-color: -fx-color-navyBlue, -fx-color-darkGray;
-fx-table-cell-border-color: -fx-color-navyBlue;
}
.table-view .table-row-cell:selected * {
-fx-background-color: -fx-color-orange;
-fx-text-fill: -fx-color-navyBlue;
}
Also my TableView Column Resize Policy is constrained-resize and maybe it causes it? But when it's unconstrained-resize it looks worse because of additional "empty" column like below:
Any tip how to get rid of it on window load?
I did what I could to get make this runnable so css doesnt look good because I was using the one from the question but had to make some edits to get it to work. If you uncomment the one line it gets rid of the third column I know you said you set it to resize I cannot replicate the issue your having if you take this modify it so you get the issue and post it I may be able to help otherwise probably not I would check where you set the table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY); and make sure its executing correctly if you do it in fxml try setting it on initialization as well and see if that fixes the problem
public class Main extends Application {
public static void main(String[] args) { launch(args); }
#Override
public void start(Stage stage) {
ObservableList<Person> data = FXCollections.observableArrayList(new Person("First", "Last"));
TableColumn firstNameCol = new TableColumn("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName"));
TableColumn lastNameCol = new TableColumn("Last Name");
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lastName"));
TableView<Person> table = new TableView<>();
table.setItems(data);
table.getColumns().addAll(firstNameCol, lastNameCol);
//table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
Button addButton = new Button("Add");
addButton.setOnAction(event -> data.add(new Person("First","Last")));
VBox vbox = new VBox();
vbox.getChildren().addAll(table, addButton);
Scene scene = new Scene(new Group(vbox));
scene.getStylesheets().add(String.valueOf(getClass().getClassLoader().getResource("tableview.css")));
stage.setScene(scene);
stage.show();
}
public static class Person {
private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;
private Person(String fName, String lName) {
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
}
public String getFirstName() { return firstName.get(); }
public void setFirstName(String fName) { firstName.set(fName); }
public String getLastName() { return lastName.get(); }
public void setLastName(String fName) { lastName.set(fName); }
}
}
tableview.css
.table-view{
-fx-background-color: navy;
}
.table-view .filler{
-fx-background-color: navy;
}
.table-view:focused{
-fx-background-color: navy;
}
.table-view .column-header-background{
-fx-background-color: navy;
-fx-border-color: navy;
}
.table-view .column-header-background .label{
-fx-background-color: darkGray;
-fx-text-fill: orange;
}
.table-view .column-header {
-fx-background-color: darkGray;
-fx-border-width: 2;
-fx-border-color: navy;
}
.table-view .table-cell {
-fx-text-fill: lightGray;
-fx-alignment: center;
-fx-border-width: 2;
-fx-border-color: navy;
}
.table-view .table-row-cell{
-fx-background-color: navy, darkGray;
-fx-table-cell-border-color: navy;
}
.table-view .table-row-cell:selected * {
-fx-background-color: orange;
-fx-text-fill: navy;
}

Date picker popup window

I need an advice with the styling of date picker module in Java FX with an assistance of CSS classes.
It is specifically the popup window in which I used also the scenic view to look up the CSS code. But doesn't work in the popup window. I need a help how to style popup in the calendar view.
(using intelijIdea)
This is my code and the attached picture.
.date-picker .combo-box {
-fx-background-insets:0px ;
-fx-border-width: 0px;
}
.date-picker .text-field{
-fx-background-radius:0px ;
-fx-border-color: transparent;
}
.date-picker .arrow-button{
-fx-background-radius:0px ;
-fx-background-color: #232323;
}
.date-picker .arrow-button .arrow{
-fx-background-radius:0px ;
-fx-background-color: #1783CC;
}
.date-picker .arrow-button:hover .arrow{
-fx-background-radius:0px ;
-fx-background-color: #40a9ef;
}
.date-picker .button {
-fx-background-color: transparent;
-fx-border-color:transparent;
-fx-pref-height: 35px;
-fx-pref-width: 35px;
}
.date-picker .button:hover {
-fx-border-color:transparent;
-fx-pref-height: 35px;
-fx-pref-width: 35px;
}
.date-picker .button .arrow:pressed {
-fx-border-color:transparent;
-fx-pref-height: 35px;
-fx-pref-width: 35px;
}
.date-picker .cell {
-fx-background-color: #232323;
-fx-pref-width:20px ;
-fx-pref-height:25px ;
}
.date-picker .cell:hover {
-fx-background-color: #1783CC;
-fx-pref-width:20px ;
-fx-pref-height:25px ;
}
.date-picker .cell:focused {
-fx-background-color:#11659e;
-fx-pref-width:20px ;
-fx-pref-height:25px ;
}
.date-picker-popup {
-fx-border-color: #1783CC;
-fx-background-color: black;
}
Most style classes of the DatePicker popup can be found in the DatePickerContent.java or in one of the JavaFX's theme stylesheet.
For instance, if you're using the modena theme then you will find the these styleclasses in modena.css (from line 2934)
A quick workaround when using ScenicView would be to prevent the popup from being closed:
datePicker.setOnHidden(new EventHandler<Event>() {
#Override
public void handle(Event event) {
datePicker.show();
}
});

Javafx 2.0 : How to change the size of the radio-buttons circle with CSS?

I try to change the radio-buttons size in my app build with FXML and CSS.
I use the sceneBuilder.
Thanks for your help !
Here is my actual CSS code for the radio-buttons :
.radio-button .radio{
-fx-border-width : 1px ;
-fx-border-color : #000 ;
-fx-background-color : white ;
-fx-background-image : null ;
-fx-border-radius : 15px ;
-fx-height : 15px ; /* Not working */
height : 5px ; /* Not working */
}
.radio-button .radio:selected{
-fx-background-color : white ;
-fx-background-image : null ;
}
.radio-button -radio:armed{
-fx-background-color : white ;
-fx-background-image : null ;
}
.radio-button -radio:determinate{
-fx-background-color : white ;
-fx-background-image : null ;
}
.radio-button -radio:indeterminate{
-fx-background-color : white ;
-fx-background-image : null ;
}
-fx-padding: 10px;
A single padding value means that all padding are the same, if a set of four padding values is specified, they are used for the top, right, bottom, and left edges of the region.
from the JavaFX CSS Reference Guide
Example:
CssTest.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class CssTest extends Application
{
public void start(Stage stage) throws Exception
{
BorderPane root = new BorderPane();
RadioButton radio = new RadioButton("radio-text");
root.setCenter(radio);
root.getStylesheets().add(getClass().getResource("/radio.css").toExternalForm());
stage.setScene(new Scene(root));
stage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
radio.css
.radio-button .radio {
-fx-border-width: 1px;
-fx-border-color: #000;
-fx-background-color: white;
-fx-background-image: null;
-fx-border-radius: 15px;
-fx-padding: 4px;
}
.radio-button .radio:selected {
-fx-background-color: white;
-fx-background-image: null;
}
.radio-button -radio:armed {
-fx-background-color: white;
-fx-background-image: null;
}
.radio-button -radio:determinate {
-fx-background-color: white;
-fx-background-image: null;
}
.radio-button -radio:indeterminate {
-fx-background-color: white;
-fx-background-image: null;
}
.radio-button .dot {
-fx-background-radius: 15px;
-fx-padding: 8px;
}
result
For more inspirational JavaFX CSS themes, check win7glass.css from GreggSetzer/JavaFX-CSS-Themes

Resources