Is it possible to fire an event every time you click in week number and/or in weekday in javafx DatePicker?
I mean, I can set an actionListener to the datepicker and it will be fired when the user clicks over one specific day. But that's not what I'm looking for. I need to know when the user clicks over the name of the weekday itself, or the number of the week itself.
Is it even possible? Thanks in advance!
This should get you going in the right direction. This demos how to add an onMouseClicked event handler to the Day of the Week nodes. It also shows how to figure out the CSS for other nodes.
Keycode:
for (Node node : popupContent.lookupAll(".day-name-cell")) {
DateCell tempDateCell = (DateCell) node;
tempDateCell.setOnMouseClicked((event) -> {
System.out.println("You clicked: " + tempDateCell.getText());
});
}
Full code:
import com.sun.javafx.scene.control.skin.DatePickerSkin;
import java.time.LocalDate;
import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.DatePicker;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
*
* #author blj0011
*/
public class JavaFXApplication147 extends Application
{
#Override
public void start(Stage primaryStage)
{
DatePicker datePicker = new DatePicker(LocalDate.now());
DatePickerSkin datePickerSkin = new DatePickerSkin(datePicker);
Node popupContent = datePickerSkin.getPopupContent();
//To see day of the week nodes
for (Node node : popupContent.lookupAll(".day-name-cell")) {
node.setOnMouseClicked((event) -> {
DateCell dateCell = (DateCell) node;
System.out.println("You clicked: " + dateCell.getText());
});
}
//To see all nodes. Used to find out how to look up certain nodes
System.out.println("\nAll nodes:");
for (Node node : popupContent.lookupAll("*")) {
System.out.println("\t" + node);
}
StackPane root = new StackPane(popupContent);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
Output:
All nodes:
DatePickerContent#58c2582f[styleClass=date-picker-popup]
BorderPane#9a2c4e3[styleClass=month-year-pane]
HBox#6237f3e7[styleClass=spinner]
Button#5468474f[styleClass=button left-button]''
Label#4a04f04f[styleClass=label spinner-label]'March'
Button#68b04c02[styleClass=button right-button]''
HBox#6e61c6ba[styleClass=spinner]
Button#d7d5b3e[styleClass=button left-button]''
Label#508829bd[styleClass=label spinner-label]'2018'
Button#3837351b[styleClass=button right-button]''
Grid hgap=-1.0, vgap=-1.0, alignment=TOP_LEFT
DateCell#6a1b4345[styleClass=cell date-cell day-name-cell]'Sun'
DateCell#76997e3d[styleClass=cell date-cell day-name-cell]'Mon'
DateCell#c263fb4[styleClass=cell date-cell day-name-cell]'Tue'
DateCell#29bdab5e[styleClass=cell date-cell day-name-cell]'Wed'
DateCell#190cd447[styleClass=cell date-cell day-name-cell]'Thu'
DateCell#245929cd[styleClass=cell date-cell day-name-cell]'Fri'
DateCell#193f696d[styleClass=cell date-cell day-name-cell]'Sat'
DateCell#5d9cc64f[styleClass=cell date-cell day-cell previous-month]'25'
DateCell#224d9f57[styleClass=cell date-cell day-cell previous-month]'26'
DateCell#4fb7dd92[styleClass=cell date-cell day-cell previous-month]'27'
DateCell#2da1a108[styleClass=cell date-cell day-cell previous-month]'28'
DateCell#1634b44d[styleClass=cell date-cell day-cell]'1'
DateCell#584d26fb[styleClass=cell date-cell day-cell]'2'
DateCell#29e72b2d[styleClass=cell date-cell day-cell]'3'
DateCell#96e66f8[styleClass=cell date-cell day-cell]'4'
DateCell#3a4b2d44[styleClass=cell date-cell day-cell]'5'
DateCell#b80be08[styleClass=cell date-cell day-cell]'6'
DateCell#1b2e4216[styleClass=cell date-cell day-cell]'7'
DateCell#610dbe2d[styleClass=cell date-cell day-cell]'8'
DateCell#2cf9e45[styleClass=cell date-cell day-cell]'9'
DateCell#cb100ea[styleClass=cell date-cell day-cell]'10'
DateCell#65e6dd98[styleClass=cell date-cell day-cell]'11'
DateCell#6916ebdf[styleClass=cell date-cell day-cell]'12'
DateCell#17d57d0e[styleClass=cell date-cell day-cell]'13'
DateCell#1367994e[styleClass=cell date-cell day-cell]'14'
DateCell#40be8ca7[styleClass=cell date-cell day-cell]'15'
DateCell#1ea8cf7c[styleClass=cell date-cell day-cell]'16'
DateCell#2229ecaa[styleClass=cell date-cell day-cell]'17'
DateCell#dabac48[styleClass=cell date-cell day-cell]'18'
DateCell#3944c2ee[styleClass=cell date-cell day-cell]'19'
DateCell#614f3995[styleClass=cell date-cell day-cell]'20'
DateCell#c6ed5aa[styleClass=cell date-cell day-cell]'21'
DateCell#5cf7e183[styleClass=cell date-cell day-cell]'22'
DateCell#43bb1b41[styleClass=cell date-cell day-cell]'23'
DateCell#5ba01132[styleClass=cell date-cell day-cell]'24'
DateCell#21995c69[styleClass=cell date-cell day-cell]'25'
DateCell#42014a20[styleClass=cell date-cell day-cell]'26'
DateCell#597b2032[styleClass=cell date-cell day-cell]'27'
DateCell#20993ae[styleClass=cell date-cell day-cell today selected]'28'
DateCell#6918ed92[styleClass=cell date-cell day-cell]'29'
DateCell#2180eadb[styleClass=cell date-cell day-cell]'30'
DateCell#21fa2469[styleClass=cell date-cell day-cell]'31'
DateCell#6fa0571b[styleClass=cell date-cell day-cell next-month]'1'
DateCell#41fa4300[styleClass=cell date-cell day-cell next-month]'2'
DateCell#670abfd6[styleClass=cell date-cell day-cell next-month]'3'
DateCell#17231632[styleClass=cell date-cell day-cell next-month]'4'
DateCell#7ad68342[styleClass=cell date-cell day-cell next-month]'5'
DateCell#69273d5b[styleClass=cell date-cell day-cell next-month]'6'
DateCell#5b61dba6[styleClass=cell date-cell day-cell next-month]'7'
Related
i need to set hover over the bar in barchart.
I have code which show barchart and I need to show values from y-axis when crossing over bar on graphic.
XYChart.Series set1 = new XYChart.Series<>();
XYChart.Series series1 = new XYChart.Series();
for(int i = 0; i< suply.size(); i++)
{
set1.getData().add(new XYChart.Data(suply.get(i).getName(), Float.parseFloat(suply.get(i).getNum())));
final XYChart.Data<String, Number> data = new XYChart.Data(suply.get(i).getName(), Float.parseFloat(suply.get(i).getNum()));
data.nodeProperty().addListener(new ChangeListener<Node>() {
#Override public void changed(ObservableValue<? extends Node> ov, Node oldNode, Node newNode) {
if (newNode != null) {
}
}
});
series1.getData().add(data);
}
barChart.getData().add(series1);
//now you can get the nodes.
for (Series<String, Number> serie: barChart.getData()){
for (XYChart.Data<String, Number> item: serie.getData()){
item.getNode().setOnMousePressed((MouseEvent event) -> {
System.out.println("you clicked "+item.toString()+serie.toString());
});
}
}
barChart.setCursor(Cursor.CROSSHAIR);
Where to put code for hover on barchart and how?
I found solutions
for (final Series<String, Number> series : barChart.getData()) {
for (final XYChart.Data<String, Number> data : series.getData()) {
Tooltip tooltip = new Tooltip();
tooltip.setText(data.getXValue().toString() +" "+
data.getYValue().toString());
Tooltip.install(data.getNode(), tooltip);
}
}
I want to change the combobox by selecting item from another combobox. Means, I want to control one combobox using other combobox. For example, if I have a combobox containing names and other containing countries, then if I select Mumbai from names then other combobox should automatically display India.
Please help me out a little bit to solve this problem. A piece of code will work for me to explain.
I think this is what you mean where the second combobox is dependent on what is chosen in the first
public class Main extends Application {
#Override
public void start(Stage stage) {
ComboBox comboBox2 = new ComboBox();
ComboBox comboBox = new ComboBox();
comboBox.getItems().addAll("Option 1", "Option 2", "Option 3");
comboBox.setOnAction(event -> {
comboBox2.getItems().clear();
for (int i = 0; i < 5; i++) {
comboBox2.getItems().add(comboBox.getValue().toString());
}
});
VBox vBox = new VBox();
vBox.getChildren().addAll(comboBox, comboBox2);
Scene scene = new Scene(vBox);
stage = new Stage();
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) { launch(args); }
}
I have a small problem. I'm building an interface with JavaFX like this:
I wonder, how can I do to block those "lines" of the ScrollPane I indicated in the image? Practically it is not to be resizable but among its properties the ScrollPane does not allow me to put the check on that property:
How can I do to solve?
thanks to all in advance!
I think your problem is that you have not added a minimum value to your ScrollPanes here's an example :
SplitPane split = new SplitPane();
split.setPrefSize(400, 400);
//First ScrollPane
ScrollPane spA = new ScrollPane();
spA.setMinWidth(100); //Block the scrollPane width to 100
spA.setFitToHeight(true);
spA.setFitToWidth(true);
Pane paneA = new Pane();
paneA.setStyle("-fx-background-color:red;");
spA.setContent(paneA);
//Second ScrollPane
ScrollPane spB = new ScrollPane();
spB.setMinWidth(100); //Block the scrollPane width to 100
spB.setFitToHeight(true);
spB.setFitToWidth(true);
Pane paneB = new Pane();
paneB.setStyle("-fx-background-color:blue;");
spB.setContent(paneB);
split.getItems().addAll(spA,spB);
To be able to use your scrollPane as it grows, you can use the binding and bind the content's (width/height properties) of your ScrollPane to their parents (ScrollPane) example :
//set the (FitToHeight/FitToWidth) properties to false before !
spA.widthProperty().addListener(new ChangeListener<Number>() {
#Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
paneB.setMinWidth((double)newValue);
}
});
Good luck !
Trying to customize a floating TabPane by adding a CSS file just like in the example below. The new style does not appear for floating tabpane only - for a regular pane the style appears as expected.
public class FloatingTabPaneTest extends Application
{
public static void main(String[] args)
{
Application.launch(args);
}
#Override
public void start(Stage stage)
{
Parent root = createContentPane();
root.getStylesheets().add(getClass().getResource("/floatingTabPane.css").toExternalForm());
Scene scene = new Scene(root, 1000, 800);
stage.setScene(scene);
stage.setTitle(getClass().getSimpleName());
stage.show();
}
private Parent createContentPane()
{
TabPane tabPane = new TabPane();
tabPane.getStyleClass().add(TabPane.STYLE_CLASS_FLOATING);
addTab(tabPane, "Tab 1", new StackPane());
addTab(tabPane, "Tab 2", new StackPane());
addTab(tabPane, "Tab 3", new StackPane());
tabPane.getSelectionModel().selectLast();
return new BorderPane(tabPane);
}
private void addTab(TabPane tabPane, String name, Node content)
{
Tab tab = new Tab();
tab.setText(name);
tab.setContent(content);
tabPane.getTabs().add(tab);
}
}
FloatingTabPane.css:
.tab-pane.floating > .tab-header-area > .tab-header-background {
-fx-border-color: red;
-fx-border-width: 2;
-fx-background-color: cyan;
}
This is pretty interesting. The issue is because the headerBackground visibility is set to false, if you are on FLOATING style class.
If you search inside TabPaneSkin, you will find :
if (isFloatingStyleClass()) {
headerBackground.setVisible(false); // <---- Imp part
} else {
headerBackground.resize(snapSize(getWidth()), snapSize(getHeight()));
headerBackground.setVisible(true);
}
Since its visibility is set to false, your best shot is to do your changes on the tab-header-area instead of tab-header-background
.tab-pane.floating > .tab-header-area {
-fx-border-color: red;
-fx-border-width: 2;
-fx-background-color: cyan;
}
This will leave a thin red line on the tabs, but this is better than having no style at all ;)
Here is my code to generate 10 bars of different colors. I want to add legend respectively but it only shows yellow legend i can change its color but i want 3 legend.
I think it shows only 1 color because there is only 1 series. Is it possible to add more than 1 legend for a single series?
output:
or if i can add this image as legend to the middle left of my chart
i need how to display image in bar chart or how to create 3 different labels for a single series bar chart
import javafx.application.Application;
import javafx.beans.value.*;
import javafx.scene.*;
import javafx.scene.chart.*;
import javafx.stage.Stage;
public class DynamicallyColoredBarChart extends Application {
#Override
public void start(Stage stage) {
final CategoryAxis xAxis = new CategoryAxis();
xAxis.setLabel("Bars");
final NumberAxis yAxis = new NumberAxis();
yAxis.setLabel("Value");
final BarChart<String, Number> bc = new BarChart<>(xAxis, yAxis);
bc.setLegendVisible(false);
XYChart.Series series1 = new XYChart.Series();
for (int i = 0; i < 10; i++) {
// change color of bar if value of i is >5 than red if i>8 than blue
final XYChart.Data<String, Number> data = new XYChart.Data("Value " + i, i);
data.nodeProperty().addListener(new ChangeListener<Node>() {
#Override
public void changed(ObservableValue<? extends Node> ov, Node oldNode, Node newNode) {
if (newNode != null) {
if (data.getYValue().intValue() > 8) {
newNode.setStyle("-fx-bar-fill: navy;");
} else if (data.getYValue().intValue() > 5) {
newNode.setStyle("-fx-bar-fill: red;");
}
}
}
});
series1.getData().add(data);
}
bc.getData().add(series1);
stage.setScene(new Scene(bc));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
To put an image to the left you can just add image and chart to HBox:
HBox root = new HBox(5);
root.getChildren().addAll(image, bc);
stage.setScene(new Scene(root));