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:
Related
Am new to javafx. I wanted to write a simple javafx program that when clicking a button for the first time, the color of a rectangle should change to something else, e.g green, and when clicking it for the second time, the color should change back to the original color.
But the button is only allowing a single click only?
Help if there is any solution on this.
Here is a code.
You can try using simple boolean value to check if color has been changed before:
boolean flag = false;
#FXML
private void onButtonClick(){
if (flag){
//change to e.g. red
} else {
//change to e.g. green
}
flag = !flag;
}
This code is simple and if you want only to change between two colors it will be enough :)
I have a single column TableView display Labels with an icon. What I'd like to do is add an animated icon to the label or cell so users know it's loading. I'm not sure how to go about this. I've looked around the internet and haven't come up with anything. I haven't had any luck trying to extend the label and customize the drawing, it's not quite as straight forward as overriding a paint method. So if anyone could point me in the right direction on how I could achieve this effect.
I think you cannot do that since the cell renderer will travel across cells and invoke paint for each cell. So I don't think you can do that and show animated icons. What you can do is to change some property of the row that is loading and invoke transition.
You can get some ideas from this code and get some similar effect:
https://github.com/james-d/Animated-Table-Row/blob/master/src/animatedtablerow/AnimatedTableRow.java
Thought I already posted this but in case this comes up for anyone else looking to do something similar, here was my solution:
sourceColumn.setCellFactory(new Callback<TableColumn<SourceItem, SourceItem>,TableCell<SourceItem, SourceItem>>(){
public TableCell<SourceItem, SourceItem> call(TableColumn<SourceItem, SourceItem> param){
TableCell<SourceItem, SourceItem> cell = new TableCell<SourceItem, SourceItem>(){
#Override
public void updateItem(SourceItem item, boolean empty) {
if (!empty) {
HBox box = new HBox();
box.setAlignment(Pos.CENTER_LEFT);
Region spacer = new Region();
HBox.setHgrow(spacer, Priority.ALWAYS);
box.getChildren().addAll(item.getLabel(), spacer);
if (item instanceof ShareSourceItem) {
if (((ShareSourceItem)item).isResolving()) {
box.getChildren().addAll(loadImage(mediaPath+"loader2.gif"));
}
}
setGraphic(box);
}
}
};
return cell;
}
});
I have made a few buttons(each represents a city) on SceneBuilder. I need to set their color according to the number of houses in each city. Darker indicating more properties and lighter less (red).
I have assigned each button a fx:id on scene builder and called it in my code but I am not sure how to change it's color by using javafx code.
Can someone help me out, I am very new to Javafx.
#FXML private Button b1 = new Button();
First i was trying to test wether or not color would actually change but it does not change
#FXML
private void test() {
for (House s: list) {
if(s.getHouse().equals("Manchester") > 10000) {
DropShadow s = new DropShadow();
b1.setEffect(s);
b1.setStyle("fx-background-color: #FF0000");
}
}
}
As c0oder pointed out, it was a simple mistake.
Change b1.setStyle("fx-background-color: #FF0000"); to b1.setStyle("-fx-background-color: #FF0000");
This did the trick.
You can change the buttons color directly through a property rather than manipulating the style.
Here's an example of changing the text in the button to red:
button.setTextFill(Color.RED);
This would be changing the background color:
button.setBackground(new Background(new BackgroundFill(Color.RED, null, null);
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 am trying to build an SWT Tree that has icons at the top level but not at the next level.
Is there any way to avoid the blank space which seems to have been left for the image which I'm not using? I tried using the following code snippets but neither did what I wanted.
SWT.MeasureItem:
tree.addListener(SWT.MeasureItem, new Listener()
{
#Override
public void handleEvent(Event event)
{
TreeItem item = (TreeItem)event.item;
Image image = item.getImage();
if (image == null)
{
event.x -= 40;
}
}
});
SWT.PaintItem:
tree.addListener(SWT.PaintItem, new Listener()
{
#Override
public void handleEvent(Event event) {
TreeItem item = (TreeItem)event.item;
Image image = item.getImage();
if (image == null)
{
event.x -= 40;
}
}
});
In both cases I was just hoping that the text could be drawn a bit further to the left.
This behavior comes from the native controls and is OS-specific (AFAIR, on Macs you won't see this problem). Alas, no easy fix but add some generic icon (or not adding icons at all).
I have done some more investigation myself. As per Eugene's answer this seems to be native behaviour. There are a couple of things worth noting.
If no items in the Tree have an icon then no space is left for icons. However, even a single item with an icon will cause all items to leave space for icons.
A hacky solution can be implemented as follows:
Use no icons so that the native control leaves no icon space
For items where you want an icon prefix some spaces to the text e.g. " " + text
Add a PaintItem listener that draws the icon you want into the space left by the text
This probably doesn't work well across platforms and across system fonts so I've just decided to live with having icons.