In JavaFX how do i set label alignment inside a tab? - javafx

It is graphic+text inside tabs.
The TabPane is made in the fxml :
<TabPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" rotateGraphic="true" side="LEFT" stylesheets="#Scene1.css" tabClosingPolicy="UNAVAILABLE" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="vues.Scene1Controller">
<tabs>
<Tab fx:id="tab111" text="Text1">
<graphic>
<ImageView>
<image>
<Image url="#face.png" />
</image>
</ImageView>
</graphic>
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Button fx:id="but111" layoutX="91.0" layoutY="64.0" mnemonicParsing="false" onAction="#but111Action" text="Button" />
</children></AnchorPane>
</content>
</Tab>
<Tab text="Text222222222222222222">
<graphic>
<ImageView>
<image>
<Image url="#face.png" />
</image>
</ImageView>
</graphic>
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
</tabs>
</TabPane>
and some css :
.tab-pane {
-fx-tab-min-width:300px;
-fx-tab-max-width:300px;
-fx-tab-min-height:300px;
-fx-tab-max-height:300px;
}
.tab *.tab-label {
-fx-rotate: 90;
/* -fx-content-display: left; */
/* -fx-padding: 0 0 0 -60; */
/* -fx-translate-y: -100; */
/* -fx-graphic-text-gap: 30px; */
/* -fx-layout-y: 150; */
}
.tab-pane > .tab-header-area > .headers-region > .tab:hover > .tab-container > .tab-label {
-fx-text-fill: red;
-fx-graphic: url("face33.png");
}
And the result is this :
Now my problem is that i want that aligned left, but the pick point of this, lets say, label, is center, so if i translate this to left with fixed value, they will not be aligned to left, but from center (using "-fx-translate-y: -150;" (height of 300 / 2)), and here is the result :
I cant use a custom translate value for each, because the text will be variable.

Related

Object reference not set to an instance of an object when using css styling with xamarin forms

I tried to set style to my page using css but i got this exception
Object reference not set to an instance of an object
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="XRetailMobile.Views.MainPage"
xmlns:local="clr-namespace:XRetailMobile.Views.ContentViews"
NavigationPage.HasNavigationBar="False">
<ContentPage.Resources>
<StyleSheet Source="/Styles/style.css"/>
</ContentPage.Resources>
<ContentPage.Content>
<Grid HorizontalOptions="Fill" VerticalOptions="Fill">
<local:ProductBackToolbar StyleClass="toolbarStyle" VerticalOptions="Start">
</local:ProductBackToolbar>
</Grid>
</ContentPage.Content>
</ContentPage>
ProductBackToolbar.xaml(contentview)
<ContentView.Content>
<StackLayout HorizontalOptions="Fill" Orientation="Horizontal">
<Image Source="back" HeightRequest="35" VerticalOptions="Center" WidthRequest="35" HorizontalOptions="StartAndExpand"></Image>
<Image Source="share" HeightRequest="35" VerticalOptions="Center" WidthRequest="35" HorizontalOptions="End"></Image>
<Image Source="shoppingbag" HeightRequest="35" WidthRequest="35" VerticalOptions="Center" HorizontalOptions="End"></Image>
</StackLayout>
</ContentView.Content>
style.css Located in Style Folder and its build action is EmbeddedResources
^contentpage {
background-color: greenyellow;
}
.toolbarStyle {
background-color: blue;
margin: 20px;
padding:20px;
}
if i comment margin and padding it's work fine
what's the wrong ?

Scrollbar doesn't appear when scrolling

I'm using HiddenSidesPane to show and hide scrollbar when needed. Scrollbar appears when I move mouse to the edge of the ScrollPane and hides when I move back. However, I have a problem to show scrollbar when the scrolling is detected. You can find my code below:
FXML:
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.VBox?>
<?import org.controlsfx.control.HiddenSidesPane?>
<?import java.net.URL?>
<ScrollPane xmlns:fx="http://javafx.com/fxml"
fx:controller="hypermap.components.featurespanel.view.properties.PropertiesTab" fx:id="propertyTabRoot"
fitToWidth="true" hbarPolicy="NEVER" vbarPolicy="NEVER">
<stylesheets>
<URL value="#Scrollbar.css"/>
</stylesheets>
<HiddenSidesPane fx:id="hiddenSidesPane" minWidth="280">
<content>
<VBox fx:id="vboxInsideScroll">
<VBox fx:id="groupsContainer"/>
<VBox fx:id="messageContainer"/>
</VBox>
</content>
</HiddenSidesPane>
</ScrollPane>
CSS file:
.scroll-pane{
-fx-focus-color: none;
-fx-background-color: transparent;
}
.scroll-bar > .increment-button, .decrement-button, .increment-arrow, .decrement-arrow{
-fx-pref-height: 0;
}
.scroll-bar:vertical{
-fx-background-color: transparent;
-fx-border-color: transparent;
-fx-pref-width: 10;
-fx-padding: 0;
}
.scroll-bar:vertical .thumb{
-fx-background-color: color-dark-slate;
-fx-border-radius: 100;
-fx-padding: 0 4 0 0;
-fx-opacity: 0.2
}
.scroll-bar:vertical .track{
-fx-background-color: transparent;
}
Controller file with method using HiddenSidesPane:
#FXML
private VBox groupsContainer;
#FXML
private VBox messageContainer;
#FXML
private ScrollPane propertyTabRoot;
#FXML
private HiddenSidesPane hiddenSidesPane;
private void makeScrollable(ScrollPane scrollPane) {
final Node scrollbar = scrollPane.lookup(".scroll-bar:vertical");
hiddenSidesPane.setRight(scrollbar);
hiddenSidesPane.setTriggerDistance(40);
hiddenSidesPane.setAnimationDuration(Duration.millis(500));
scrollPane.setOnScroll((event -> {
hiddenSidesPane.setRight(scrollbar);
event.consume();
}));
I would be very grateful for any help. Also, I don't know why the scrollbar's thumb has the same hight all the time, even if the content is so huge that it should be smaller.
Thank you!

CSS and JavaFX - CSS in a TableView

I want to apply css in all of my upper side of the tableview, the following photo show the part I want to apply the css (The part at the side of the column "Estado")
enter image description here
here's my css:
.table-view{
-fx-background-color: transparent;
}
.table-view:focused{
-fx-background-color: transparent;
}
.table-view .column-header-background{
-fx-background-color: linear-gradient(#101010 0%, #424141 100%);
}
.table-view .column-header-background .label{
-fx-background-color: transparent;
-fx-text-fill: white;
}
.table-view .column-header {
-fx-background-color: transparent;
}
.table-view .table-cell{
-fx-text-fill: white;
}
.table-row-cell{
-fx-background-color: -fx-table-cell-border-color, #616161;
-fx-background-insets: 0, 0 0 1 0;
-fx-padding: 0.0em;
}
.table-row-cell:odd{
-fx-background-color: -fx-table-cell-border-color, #424242;
-fx-background-insets: 0, 0 0 1 0;
-fx-padding: 0.0em;
}
.table-row-cell:selected {
-fx-background-color: #005797;
-fx-background-insets: 0;
-fx-background-radius: 1;
}
The part is white because it isn't a column of my tableview, so I want to know how to apply CSS in empty parts (without columns) in the table.
FXML code:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<HBox prefHeight="300.0" prefWidth="773.0"
stylesheets="/iftorrent/gui/menuPrincipal/MenuPrincipalCSS.css"
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="iftorrent.gui.menuPrincipal.MenuPrincipalControlador">
<children>
<TableView fx:id="tabela" prefWidth="773.0">
<columns>
<TableColumn fx:id="numero" prefWidth="35.0" text="NÂș " />
<TableColumn fx:id="nome" prefWidth="183.0" text="Nome" />
<TableColumn fx:id="tamanho" prefWidth="76.0" text="Tamanho" />
<TableColumn fx:id="velocidade" prefWidth="76.0" text="Velocidade" />
<TableColumn fx:id="tempo_estimado" prefWidth="120.0" text="Tempo Estimado" />
<TableColumn fx:id="estado" prefWidth="283.0" text="Estado" />
<TableColumn fx:id="progresso" prefWidth="283.0" text="Progresso" />
</columns>
</TableView>
</children>
</HBox>
You are looking for the .filler inside the .column-header-background. Add this rule:
.table-view .column-header-background .filler {
-fx-background-color: linear-gradient(#101010 0%, #424141 100%);
}
For reference, you can use the JavaFX CSS Reference, or use the CSS Analyzer tool built into SceneBuilder.

JavaFx2 OutOfMemoryError:Java heap space

I creat a simple JavaFx application with FXML and CSS in order to change UI Theme at runtime.
There are three Button and one Label in the scene. I define different CSS for Label and "-fx-background-image"(PNG format, size 1.23M) for BorderPane in the CSS file.
Application can switch UI Theme by click "Style" Button.
But Mem usage will raise and don't free when I click one of "Style" Button. Application will OutOfMemoryError after switch Theme about 30 times. I don't know how to fix it.Someone can help me?
java.lang.OutOfMemoryError: Java heap space
at java.awt.image.DataBufferInt.<init>(DataBufferInt.java:75)
at java.awt.image.Raster.createPackedRaster(Raster.java:467)
at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1032)
at java.awt.image.BufferedImage.<init>(BufferedImage.java:359)
at com.sun.prism.j2d.J2DTexture.<init>(J2DTexture.java:46)
at com.sun.prism.j2d.J2DResourceFactory.createTexture(J2DResourceFactory.java:72)
at com.sun.prism.impl.BaseResourceFactory.createTexture(BaseResourceFactory.java:127)
My environment: jdk1.7.0_51, Windows Server 2003 x86, 3G RAM
I am sorry that I don't know how to post IMG file(background.png) to forum.
Click "AutoSwitch" Button for auto Switching Theme.
Source Code:
Main.Java
public class Main extends Application {
private Stage primaryStage;
public Stage getPrimaryStage() {
return primaryStage;
}
public void setPrimaryStage(Stage primaryStage) {
this.primaryStage = primaryStage;
}
private BorderPane rootPane;
public BorderPane getRootPane() {
return rootPane;
}
#Override
public void start(Stage primaryStage) {
try {
setPrimaryStage(primaryStage);
URL url = new URL(getClass().getResource("AppFrame.fxml").toExternalForm());
FXMLLoader loader =new FXMLLoader(url);
rootPane = (BorderPane)loader.load();
Scene scene = new Scene(rootPane,400,400);
primaryStage.setScene(scene);
//MAX screen
Screen screen = Screen.getPrimary();
Rectangle2D bounds = screen.getVisualBounds();
primaryStage.setX(bounds.getMinX());
primaryStage.setY(bounds.getMinY());
primaryStage.setWidth(bounds.getWidth());
primaryStage.setHeight(bounds.getHeight());
primaryStage.show();
AppFrameController appFrameController = (AppFrameController)loader.getController();
appFrameController.setAppMain(this);
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
AppFrameController.java
public class AppFrameController {
private String style1Url = getClass().getResource("FxStyle1.css").toExternalForm();
private String style2Url = getClass().getResource("FxStyle2.css").toExternalForm();
#FXML private Label lblSceneTitle;
private Main appMain;
public Main getAppMain() {
return appMain;
}
public void setAppMain(Main appMain) {
this.appMain = appMain;
}
private int switchCount=0;
public AppFrameController()
{
}
public void handleClickStyle1()
{
ObservableList<String> stylesheets = getAppMain().getRootPane().getStylesheets();
stylesheets.remove(style2Url);
if(!stylesheets.contains(style1Url))
{
stylesheets.add(style1Url);
}
}
public void handleClickStyle2()
{
ObservableList<String> stylesheets = getAppMain().getRootPane().getStylesheets();
stylesheets.remove(style1Url);
if(!stylesheets.contains(style2Url))
{
stylesheets.add(style2Url);
}
}
public void handleClickSwitch()
{
SwitchCssTask handleTask = new SwitchCssTask();
new Thread(handleTask).start();
}
private class SwitchCssTask implements Runnable {
#Override
public void run() {
while (true) {
if (switchCount % 2 == 0) {
handleClickStyle1();
} else {
handleClickStyle2();
}
switchCount++;
System.out.println("Switch count=" + switchCount);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
AppFrame.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane prefHeight="362.0" prefWidth="749.3739013671875" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="application.AppFrameController">
<!-- TODO Add Nodes -->
<center>
<BorderPane id="center-pane" prefHeight="-1.0" prefWidth="-1.0" BorderPane.alignment="CENTER">
<center>
<VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" spacing="20.0">
<children>
<Button mnemonicParsing="false" onAction="#handleClickStyle1" prefHeight="70.0" prefWidth="148.0" text="Style1" />
<Button mnemonicParsing="false" onAction="#handleClickStyle2" prefHeight="70.0" prefWidth="148.0" text="Style2" />
<Button mnemonicParsing="false" onAction="#handleClickSwitch" prefHeight="70.0" prefWidth="148.0" text="AutoSwitch" />
</children>
</VBox>
</center>
</BorderPane>
</center>
<stylesheets>
<URL value="#FxStyle2.css" />
</stylesheets>
<top>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" spacing="10.0" BorderPane.alignment="CENTER">
<children>
<Label id="scene-title" fx:id="lblSceneTitle" alignment="TOP_CENTER" prefWidth="371.0" text="Test FxStyle" />
</children>
<padding>
<Insets bottom="15.0" left="12.0" right="12.0" top="15.0" />
</padding>
</HBox>
</top>
</BorderPane>
FxStyle1.css
#center-pane {
-fx-background-image: url("background.png");
}
#scene-title {
-fx-font-family: "Tahoma";
-fx-background-insets: 0;
-fx-text-fill: green;
-fx-font-weight: bold;
-fx-font-size: 4em;
-fx-effect: dropshadow( one-pass-box , rgba(0,0,0,0.9) , 1, 0.0 , 0 , 1 );
}
FxStyle2.css
#center-pane {
-fx-background-image: url("background.png");
}
#scene-title {
-fx-font-family: "Courier New";
-fx-background-insets: 0;
-fx-text-fill: red;
-fx-font-weight: bold;
-fx-font-size: 4em;
-fx-effect: dropshadow( one-pass-box , rgba(0,0,0,0.9) , 1, 0.0 , 0 , 1 );
}
I get answer from Oracle Communtiy.
please see here.

Using both CSS and setStyle

I have an object where I have css stylizing the object. In my program I would like to change specific elements in the styling such as background color. The problem that I have run into was that when I call setStyle(-fx-background-color:red) it will override all the styling elements. Even if I just change background color.
#header {
-fx-background-color: #4d4d4d;
-fx-border-color: #545454;
-fx-border-width: 1px;
-fx-border-style: solid;
}
#header:hover {
-fx-background-color: #9ACD32;
}
You should define your colors in your CSS using "looked up colors".
See the JavaFX CSS reference for details.
Looked-up Colors
With looked-up colors you can refer to any other color property that is set on the current node or any of its parents. This is a very powerful feature, as it allows a generic palette of colors to be specified on the scene then used thoughout the application. If you want to change one of those palette colors you can do so at any level in the scene tree and it will affect that node and all its decendents. Looked-up colors are not looked up until they are applied, so they are live and react to any style changes that might occur, such as replacing a palette color at runtime with the "style" property on a node.
In the following example, all background color of all buttons uses the looked up color "abc".
.root { abc: #f00 }
.button { -fx-background-color: abc }
Sample Application
In the sample application, the key code is the setting of the looked up color value dynamically in code:
headerColorToggleGroup.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
#Override
public void changed(
ObservableValue<? extends Toggle> observableValue,
Toggle oldToggle,
Toggle newToggle
) {
RadioButton radio = (RadioButton) newToggle;
header.setStyle("header-base-color: " + radio.getText());
}
});
Together with the definition of the header-base-color lookup id for the color in the css below:
header-color.css
.root {
header-base-color: #4d4d4d;
}
#header {
-fx-background-color: header-base-color;
-fx-border-color: #545454;
-fx-border-width: 1px;
-fx-border-style: solid;
}
#header:hover {
-fx-background-color: #9ACD32;
}
header-color.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?scenebuilder-stylesheet header-color.css?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import java.net.URL?>
<BorderPane id="BorderPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="-1.0" prefWidth="-1.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="HeaderColorController">
<top>
<HBox fx:id="header" prefHeight="-1.0" prefWidth="-1.0">
<children>
<Label text="A really hard to read label" />
</children>
</HBox>
</top>
<center>
<VBox prefHeight="-1.0" prefWidth="-1.0" spacing="10.0" style="-fx-background-color: azure;">
<children>
<RadioButton mnemonicParsing="false" style="-fx-text-fill: forestgreen;" text="forestgreen">
<toggleGroup>
<ToggleGroup fx:id="headerColorToggleGroup" />
</toggleGroup>
</RadioButton>
<RadioButton mnemonicParsing="false" style="-fx-text-fill: firebrick;" text="firebrick" toggleGroup="$headerColorToggleGroup" />
<RadioButton mnemonicParsing="false" selected="true" style="-fx-text-fill: #4d4d4d;" text="#4d4d4d" toggleGroup="$headerColorToggleGroup" />
</children>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</VBox>
</center>
<stylesheets>
<URL value="#header-color.css" />
</stylesheets>
</BorderPane>
HeaderColorApp.java
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class HeaderColorApp extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("header-color.fxml"));
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) { launch(args); }
}
HeaderColorController.java
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.value.*;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
public class HeaderColorController {
#FXML
private ResourceBundle resources;
#FXML
private URL location;
#FXML
private HBox header;
#FXML
private ToggleGroup headerColorToggleGroup;
#FXML
void initialize() {
assert header != null : "fx:id=\"header\" was not injected: check your FXML file 'header-color.fxml'.";
assert headerColorToggleGroup != null : "fx:id=\"headerColorToggleGroup\" was not injected: check your FXML file 'header-color.fxml'.";
headerColorToggleGroup.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
#Override
public void changed(ObservableValue<? extends Toggle> observableValue, Toggle oldToggle, Toggle newToggle) {
RadioButton radio = (RadioButton) newToggle;
header.setStyle("header-base-color: " + radio.getText());
}
});
}
}

Resources