How can you change the color of a bar in a JavaFX BarChart?
I couldn't find a way to change the color through the css setStyle Method.
you can set color of bar using css
.default-color0.chart-bar { -fx-bar-fill: ***** }
.default-color1.chart-bar { -fx-bar-fill: ***** }
...
Using setStyle Method :
use lookupAll method in Node class,
Finds all Nodes, including this one and any children, which match the
given CSS selector. If no matches are found, an empty unmodifiable set
is returned. The set is explicitly unordered.
code :
//set first bar color
for(Node n:barChart.lookupAll(".default-color0.chart-bar")) {
n.setStyle("-fx-bar-fill: red;");
}
//second bar color
for(Node n:barChart.lookupAll(".default-color1.chart-bar")) {
n.setStyle("-fx-bar-fill: green;");
}
From JavaFX8 you can simply use the .chart-bar selector:
.chart-bar {
-fx-bar-fill: red;
}
Click in the Category axis or Number axis (which one you want to change color) > go the the editor (right side of scene builder) > choose color in tick label fill
Related
Is there any way to change the color of a selected TreeView item, instead of the default red and blue color that shows when a TreeItem is selected
This is not a standard java TreeView, I noticed that you're using JFoenix's tree view JFXTreeView. You can change the color of this selection bar by applying:
.tree-cell .selection-bar {
-fx-background-color: green;
}
To change that background color:
.tree-cell:selected {
-fx-background-color: green;
}
I'd like to add a right click context menu for a TableView on the unused column space. Is there a way to do this? This is the space between the last used column and the end of the table.
The empty area of the TableView's header is called filler. CSS selector is:
.table-view > .column-header-background > .filler
You can add a ContextMenu to this region like this:
ContextMenu fillerContextMenu = new ContextMenu(new MenuItem("Do in filler"));
Region filler = (Region) tableView.lookup(".filler");
filler.setOnContextMenuRequested(event -> {
fillerContextMenu.show(filler, event.getScreenX(), event.getScreenY());
event.consume();
});
Note: In order for lookup(String selector) to return the acctual node not null, this method should be called after your TableView is shown.
I have tried the following with no success:
.tree-view {
-fx-skin: "com.sun.javafx.scene.control.skin.TreeViewSkin";
-fx-background-color: green, -fx-control-inner-background;
-fx-background-insets: 0, 1;
}
What I am trying to achieve is to set a blue background for an entire TreeView except the selected one. Setting the background on the selected tree item works fine, but changing for the entire tree view does not have any effect.
You can color the individual TreeCells by using the .tree-cell selector:
.tree-view {
-fx-background-color:lightsteelblue;
}
.tree-cell {
-fx-background-color:lightsteelblue; // Default color
}
.tree-view:focused .tree-cell:filled:selected {
-fx-background-color:indigo; // Focused color
}
.tree-cell:filled:selected{
-fx-background-color:lightcyan; // Unfocused color
}
Produces something like this:
Is there any way to use FontAwesome for buttons in Grid? I tried to make it as an html - but buttos dont parse html but I can only see it as text.
I also tried to make a custom renderer (from https://vaadin.com/forum#!/thread/9418390/9765924) but it does not work - no errors, it just wont change text no matter how i do it.
I would like to have 3 buttons with FontAwesome icons and tooltips in every row. Is there some simple way to do that?
I guess what you want is clickable icons that don't necessarily need to be buttons.
So instead of using ButtonRenderer to add buttons, for which you can only provide a caption, a simple solution is to use HtmlRenderer to add the FontAwesome icon (1 column for each icon), then use an ItemClickListener. Use ItemClickEvent#getPropertyId() to detect which column was clicked.
I've a simple workaround for the FontIcon issue.
I've used a normal ButtonRenderer and I've overridden the getValue() method of PropertyValueGenerator object to return the icon codepoint.
wrapperContainer.addGeneratedProperty("delete", new PropertyValueGenerator<String>() {
#Override
public String getValue(Item item, Object itemId, Object propertyId) {
return "\uF014"; //FontAwesome.TRASH_O
}
#Override
public Class<String> getType() {
return String.class;
}
});
I've put in scss the right font-family for button element:
button {
border: 0;
background-color: transparent;
background-size: 25px;
color: $v-blue-color;
font-family: FontAwesome;
height: 25px;
width: 25px;
}
Pure-Java solution for Vaadin 8:
final ButtonRenderer<Person> renderer = new ButtonRenderer<>(event -> onPersonClicked(event.getItem()));
renderer.setHtmlContentAllowed(true);
grid.addColumn(person -> VaadinIcons.EXTERNAL_BROWSER.getHtml(), renderer).setWidth(60);
There is lots of alternative
Like add with HtmlRenderer or CellStyleGenerator and anothers
But HtmlRenderer uploading for every row like this data
25=<span class="v-icon v-icon-caret_square_up_o" style="font-family: Vaadin-Icons;"></span>
This is big problem for network traffic
So I like to use
Grid.Column gridColumn = grid.addColumn(a -> (char) FontAwesome.BTC.getCodepoint());
gridColumn.setStyleGenerator(item -> "cssStyleName");
With this network usage is better as you see
27= //unshowing character is icon character code
but don't forget to add "cssStyleName" to css / for example
.cssStyleName {font-family: FontAwesome; }
And listen clicks with
grid.addItemClickListener(event -> {
if (!event.getMouseEventDetails().isDoubleClick()) {
//To Do
}
}
This is compatible with Vaadin 8
A button with just an icon and a tooltip can be created like this:
Button button = new Button(FontAwesome.ANDROID);
button.setDescription("Any tooltip");
To generate buttons for each row, just use any fitting loop (for or while).
I'm trying to change the highlight/focus/hover color of menu items.
I'm trying to change the blue background to another color, but nothing seems to work?
I've tried a few things with no luck from: How do you set the style for a JavaFX ContextMenu using css? and How to style menu button and menu items
.context-menu:focused {
-fx-background-color:white;
-fx-focus-color:white;
}
.menu-item:focused {
-fx-background-color:white;
-fx-focus-color:white;
}
.menu:focused {
-fx-background-color:white;
-fx-focus-color:white;
}
and many other variations...
Also some example code that's using the menu item's
// Menu
final ContextMenu contextMenu = new ContextMenu();
and construct a MenuItem:
maximizeMenuItem = new MenuItem(Config.getString("Maximize"));
maximizeMenuItem.setOnAction(new EventHandler<ActionEvent>() { /* do stuff */ }
I could try a:
contextMenu.setStyle("-fx-focus-color:white");
or
maximizeMenuItem.setStyle("-fx-focus-color:white");
but I can't seem to figure out which -fx- css tag controls that blue background color...
If possible, please post the FXML solution as well as the in-line code solution.
Ok, a little embarrassed. I had my layers messed up to where my stylesheet wasn't being applied like I thought it was.
So the correct way to change the menu-item's background color when focused is:
.menu-item:focused {
-fx-background-color: #969A9F;
}
Once I found and sorted out my css layering problem, it now works as expected as result it: