How to add a context menu on unused column header in TableView - java

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.

Related

How to click on elements which are located in cell of the table Selenium

I wrote method that gets text value from cell ​​by row number and column name.
public String getValueFromCell(int rowNumber, String columnName){
List<Map<String, WebElement>> rowsWithColumnsByHeadings = getRowsWithColumnsByHeadings();
return rowsWithColumnsByHeadings.get(rowNumber - 1).get(columnName).getText();
}
But there are icons in cells(as you can see in the screenshot, there can be one,two, or four icons)
And now i want to click on all of them. Or click on specific icon
And i must admit that this table is located on many pages with different count of icons in cell, and i want to use this method for all of them.
How can i modify this method? Or what should i do?
Please check below solution:
If you want to click on specific icon element then you can use rowNumber index to click on it, Just check otherwise use moveToElement using Action class before while clicking onto the element:
public String getValueFromCell(int rowNumber, String columnName){
List<Map<String, WebElement>> rowsWithColumnsByHeadings = getRowsWithColumnsByHeadings();
List<WebElement> elements = driver.findElements(By.className("mat-icon.mat-icon.notranslate.material-icon.mat-icon-n0-color"))
elements.get(rowNumber).click()
return rowsWithColumnsByHeadings.get(rowNumber - 1).get(columnName).getText();
}
or if you want to click all of them one by one :
List<WebElement> elements = driver.findElements(By.className("mat-icon.mat-icon.notranslate.material-icon.mat-icon-n0-color"));
elements.forEach(e -> {
e.click();
});

display red dot on textfield javafx

The searchField is a TextField that only accept numbers. I did it with the code below.
searchField.textProperty().addListener((observable, oldValue, newValue) -> {
if (!newValue.matches("\\d*")) {
searchField.setText(newValue.replaceAll("[^\\d]", ""));
}
});
Now I want the TextField to show a red warning when user tries to input a letter. Like this..example
ControlsFX has a class called CustomTextField that allows you to set a Node on the left or right side of the TextField.
As for the logic (taking some from a previous answer), here's what you would do:
ImageView errorView = new ImageView();
errorView.setImage(new Image("error.png"));
CustomTextField searchField = new CustomTextField();
searchField.setRight(errorView);
searchField.textProperty().addListener((observable, oldValue, newValue) -> {
if (!newValue.matches("\\d*")) {
searchField.setText(newValue.replaceAll("[^\\d]", ""));
searchField.setStyle("-fx-text-box-border: red; -fx-focus-color: red ;");
errorView.setVisible(true);
}else if(!searchField.getStyle().equals("")){
searchField.setStyle("");
errorView.setVisible(false);
}
});
Here is what your looking for if you set the style of the border to red when its incorrect it will simulate what you want to see and set it back to normal when the key event is correct
searchField.textProperty().addListener((observable, oldValue, newValue) -> {
if (!newValue.matches("\\d*")) {
searchField.setText(newValue.replaceAll("[^\\d]", ""));
searchField.setStyle("-fx-text-box-border: red; -fx-focus-color: red ;");
//stopIcon.setVisible(true)
}else if(!searchField.getStyle().equals("")){
searchField.setStyle("");
//stopIcon.setVisible(false)
}
});
If you want the stop sign like in your picture you can probably load an icon in and set it to visible and not visible with the code stiling
I think you should create your own Control as following :
-First you need to put your TextField in a Layout.
-Then add a Label with the exclamation sign aligned to the center.
-After that you need to edit the shape of the Label.
-And finally make a condition to the visibility of the this one to appear according to the input.

Set tooltip on TableColumn(JavaFX) without side effect

There is a way to set tooltip like that
if (tooltipText != null) {
Label lb = new Label(column.getText());
lb.setTooltip(new Tooltip(tooltipText));
column.setText(null);
column.setGraphic(lb);
}
Unfortunately then will be exists ugly side-effect.
We set null to text of column but menuItems invoke column.getText(). If we don't do this, there will be double name in header.How to solve it? Suppose by means of css..
This answer doesn't erase side-effect
How to add a tooltip to a TableView header cell in JavaFX 8
Note: The following code relies on internals of the TableView skin. These could be subject to change, since they reside in the com.sun packages.
CSS lookup can be used to get access to the TableColumnHeader nodes after the first layout pass on the TableView. Those can be used to retrieve the Label that is used to display the TableColumn's text property.
#Override
public void start(Stage primaryStage) {
TableView tv = createTableView();
Scene scene = new Scene(tv);
// generate layout pass
tv.applyCss();
tv.layout();
// assign tooltips to headers
tv.lookupAll(".column-header").stream().forEach(n -> {
TableColumnHeader header = (TableColumnHeader) n;
Tooltip tooltip = new Tooltip("Tooltip: " + header.getTableColumn().getText());
((Control) header.lookup(".label")).setTooltip(tooltip);
});
primaryStage.setScene(scene);
primaryStage.show();
}

TableViewer how to set caret position for the cell with Editing Support and remove FULL_SELECTION

I have a TableViewer with last column which has EditingSupport to edit the cell's value. The viewer has style set to FULL_SELECTION.
Once I click on the cell, the text value is fully selected:
I can start editing, it will clear off the current value "max value" and start new value.
However, If i want to move the mouse to a specific position to edit, I need to give a delay of a sec or so. If I click too fast right after the first click to select, the cell will be deselected.
Is there any way to avoid that? Can I make the cell not fully selected ? or always have the caret to the end of the text ? Thanks!
I assume you are using TextCellEditor as the cell editor.
Create your own class extending TextCellEditor and override the doSetFocus class to be something like:
#Override
protected void doSetFocus()
{
super.doSetFocus();
if (text != null) {
text.setSelection(0, 0);
}
}
to remove full selection and set caret to end of text, override doSetFocus and set text.setSelection( text.getText().length() );
to ignore double click event, override getDoubleClickTimeout to return 0

JavaFX BarChart bar color

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

Categories

Resources