How to load css file in javafx8
You need an URL and call toExternalForm in order to load a css file into your project.
Use the ClassLoader for that:
scene.getStylesheets().add(getClass().getResource("/style.css").toExternalForm());
JavaFX doesn't load css as it does in fxml
Alright, so the issue with this particular problem is the nature of TableColumn. It doesn't appear that TableColumn has any real binding with .css, and more than that, even if you access it via' it's css ID, the property -fx-pref-width doesn't seem to exist for it. Which is why you're not seeing any sort of change. Also, due to the potentially buggy nature of tableView in javafx at the moment, you MUST set a pref-width, or things can get a little strange. see
This topic for an example of some of the strangeness.
Now, the solution to this is to create an fxml controller class and set the value of the width programmatically. I realize this might not be completely ideal for your solution, but it does seem to work just fine. for example:
my FXML.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import java.net.URL?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<VBox prefHeight="100.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller = "filechooserexample.fxmlController" >
<children>
<HBox prefHeight="20.0" />
<TableView id="table">
<columns>
<TableColumn id="tablec_h_from" maxWidth="5000.0" minWidth="10.0" prefWidth="50.0" text="H_FROM" fx:id="myTableColumn" />
<!-- other columns -->
</columns>
<stylesheets>
<URL value="@application.css" />
</stylesheets>
</TableView>
</children>
<stylesheets>
<URL value="@application.css" />
</stylesheets>
</VBox>
my main class is identical, and now for my controller:
fxmlController.java:
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
/**
*
* @author William
*/
public class fxmlController implements Initializable{
@FXML TableColumn myTableColumn;
@Override
public void initialize(URL url, ResourceBundle rb) {
myTableColumn.setPrefWidth(200);
}
}
pardon the weird 'filechooserexample' package tag in the fxml controller setter field, I'm reusing a mostly blank project I had already setup and everything. So, the code to set the controller would be: `fx:controller = "packageName.NameOfJavaClassNoExtension"'
with that code I was able to successfully set the pref width. Now, you can use css to style your table column, you just can't use -fx-pref-width, since I'm fairly certain that property doesn't exist. If you want a good guess as to what's allowed see:
caspian.cssleview-resize-after-been-moved Which is the default .css stylesheet for all of javafx. You'll likely find one or two properties outside of that, but not tons and tons that you'll see all the time.
Also worth noting, to properly access the table column, .table-column won't work, I don't think. You might have to add a style with getStyleClass().add("myCustomStyle");
which you can then access with .myCustomStyle
Good luck! I hope that helps.
Javafx css file fails to load
The exclamation in a jar url is normal, see the Javadoc for information.
But you can't load jar resources using the file protocol. The file protocol is only for loading files from the file system, not for loading resources from jars.
So instead use getResourceAsStream to get a stream for the resource which you then manipulate however you need.
Some JavaFX apis take simple strings as parameters rather than streams or urls, so you can convert the jar resource url to a string using the following pattern:
MyApplication.class.getResource("mystyles.css").toExternalForm()
Related Topics
Navigation Menu on The Left Side with Close and Open Button in ASP.NET Core Razor Pages Application
Overlaping Effect on Cards with Pure CSS
CSS Hoverable Dropdown Menu Doesn't Close on Mobile
CSS - Successive Indenting of Siblings After Headings
CSS or HTML5 for First and Last Elements
Using CSS How to Change Only The 2Nd Column of a Table
How to Center Font Awesome Icons Horizontally
Zoom in and Out on Mouse Click with CSS
How to Float Two Divs Side-By-Side Without Specifying a Width
Nesting Element+Element Selector in SCSS
Colors in Bootstrap Custom Download
Vuetify Without Vue-Cli Sass/Scss Conflict
Display Content Only on Mobile Devices
CSS Attribute Selector Class Starts with But Not Equals To
Less CSS Compiler. Unable to Use Darken Property
HTML Formatted Email Not Showing Up at All in Gmail But Is in Other Mail Clients