JavaFX - show graph in pane on checkbox click - javafx

I have FXML file that has Pane as one of it's entries, used to show graph. It is not not visible by default. When checkbox is checked, the Pane is visible. Now I have to add graph in Pane and show graph when pane is visible. How to achieve this? I have created graph using this link. JavaFX real-time LineChart with time axis

pane.visibleProperty().addListener((obs, wasVisible, isVisible) -> {
if(isVisible) { // pane became visible, add the graph
LineChart<X, Y> chart = ...;
pane.getChildren().add(chart);
} else { // pane became hidden, remove the graph
pane.getChildren().clear();
}
});

Related

How to replace down arrow with text in a combobox in JavaFX

I'm trying to remove the down arrow from a combobox. All the solutions I have found just make the arrow disappear, for example this one.
Is there a way to remove completely the space where the arrow appears and fill the box just with the text of the selected choice?
If you want to completely elimnate the arrow & arrow button space, you can try with the below custom ComboBox.
The below code is setting the arrow button and arrow nodes size to 0 and asking to rerender the comboBox. The null check is to let this changes apply only once.
public class MyComboBox<T> extends ComboBox<T>{
Region arrowBtn ;
#Override
protected void layoutChildren() {
super.layoutChildren();
if(arrowBtn==null){
arrowBtn= (Region)lookup(".arrow-button");
arrowBtn.setMaxSize(0,0);
arrowBtn.setMinSize(0,0);
arrowBtn.setPadding(new Insets(0));
Region arrow= (Region)lookup(".arrow");
arrow.setMaxSize(0,0);
arrow.setMinSize(0,0);
arrow.setPadding(new Insets(0));
// Call again the super method to relayout with the new bounds.
super.layoutChildren();
}
}
}
UPDATE :
Based on the suggestion of #kleopatra, we can get the same behaviour using css as well (without the need to create a new class for ComboBox).
.combo-box .arrow-button,
.combo-box .arrow{
-fx-max-width:0px;
-fx-min-width:0px;
-fx-padding:0px;
}
The below image will tell you the difference of a normal combox box and this custom combo box. The left one is the normal comboBox, you can see the list cell when inspecting with ScenicView. The right one is the custom one. The list cell is completely occupied suppressing the arrow space.

Get visible elements from AnchorPane

I'm developing a JavaFX application where the user is able to zoom and drag the elements (all contained in a AnchorPane). Some of those elements are simple lines and I need to have something like a ruler that has different parent to stick on the screen on the same position even if the user zooms or drags the mentioned AnchorPane. I got almost everything working but I have one problem, I need to know which lines from the AnchorPane are visible to the user (as if the user zooms and drags the AnchorPane, some of the lines are not visible anymore). Here's what I tried (not working...)
private List<Double> getVisibleVerticalLinesXCoordonate() {
List<Double> xCoordonatesOfVisibleVerticalLines = new ArrayList<>();
List<Node> visibleNodes = new ArrayList<>();
Bounds bounds = rulerParent.getBoundsInLocal();
Bounds paneBounds = rulerParent.localToScene(bounds);
for (Node n : gridVerticalLines) {
Bounds nodeBounds = n.getBoundsInParent();
if (paneBounds.intersects(nodeBounds)) {
visibleNodes.add(n);
}
}
for (Node node : visibleNodes) {
Bounds newBounds = getRelative(node);
xCoordonatesOfVisibleVerticalLines.add(newBounds.getMinX());
}
System.out.println(Arrays.asList(xCoordonatesOfVisibleVerticalLines));
return xCoordonatesOfVisibleVerticalLines;
}
private Bounds getRelative(Node node) {
return rulerParent.sceneToLocal(node.localToScene(node.getBoundsInLocal()));
}
So, the rulerParent is what is fixed on the screen (is not zooming or dragging at all). After I have the visible lines from the AnchorPane I get the x coordinates of the lines relative to rulerParent - so I can align the ruler lines with the lines in the AnchorPane.
The problem is that this is not returning the actual visible lines...
I don't need to be able to see the whole line to consider it visible, that's why I'm using intersect...if any part of a line is visible, I need it.
It's about how you handle the zoom and dragging actions try to use ViewPort instead of scaling as with ViewPort you can know the translation X and Y coordinates of the ViewPort which is the visible X and Y coordinates of the AnchorPane

JavaFx Change position or Location of a button manually?

I have a problem with changing the location of my Button i want to set a Specific location where to put it but when i use .setLayoutX and .setLayoutY i even tried using .relocate and it is still not working am i doing something wrong? here is my sample code.
CODE
Button b1 = new Button();
b1.setText("Press");
b1.setLayoutX(100);
b1.setLayoutY(120); // this are just some randome numbers for this sample
StackPane pane = new StackPane();
pane.getChildren().add(b1);
Scene scene = new Scene(pane,500,500);
stage.setScenes(scene);
stage.show();
when i run the program the button is still in the center of the screen am doing something wrong?
Use Anchor pane or a pane to align nodes as per your layout.
StackPane, GridPane and many panes they come with inbuilt layout methods, you dont have control on locating the nodes based on x and y values.

Showing one widget over other panel

I'm working with GWT and I have a problem trying to show one VerticalPanel over other panel. I have two panels in my app: one as a menu bar and the other one as a main content. The VerticalPanel is the menu bar and when you move the mouse over it, it is opened showing you more options(widgets). The problem is that this new widgets are hidden by the panel which is below the menu bar. I want to show the whole widget even if it's partially "invading" another panel. I've tried the z-index property with CSS but I can't get it.
public class Example2 implements EntryPoint {
public void onModuleLoad()
{
//Creating the layout which one will contain two simplePanels
DockLayoutPanel dockLayoutPanel = new DockLayoutPanel(Unit.EM);
dockLayoutPanel.setStylePrimaryName("layout");
//Creating the two Simplepanels
SimplePanel menuPanel = new SimplePanel();
menuPanel.setStylePrimaryName("menuPanel");
SimplePanel mainContentPanel = new SimplePanel();
mainContentPanel.setStylePrimaryName("mainContentPanel");
//Adding panels to the DockLayoutPanel
dockLayoutPanel.addNorth(menuPanel, 10);
dockLayoutPanel.add(mainContentPanel);
VerticalPanel vPanel = new VerticalPanel();
//Creating some buttons
Button a = new Button("Button");
vPanel.add(a);
Button b = new Button("MoreButton");
vPanel.add(b);
Button c = new Button("AnotherButton");
vPanel.add(c);
Button d = new Button("ButtonButton");
vPanel.add(d);
Button e = new Button("LastButton");
vPanel.add(e);
//Adding the buttons to the menuPanel(simplePanel)
menuPanel.add(vPanel);
RootLayoutPanel rp = RootLayoutPanel.get();
rp.add(dockLayoutPanel);
}
}
And the .css
.layout{
font-size:10px;
}
.menuPanel{
background:#e8f8ff;
}
.mainContentPanel{
background:#eee9e9;
}
With this example you might see my point. I know it's a dumb one, but it shows my problem. The buttons that are added to the vPanel are shown as long as they occupy space in the menuPanel, the rest are hidden under the mainContentPanel.
It can be as simple as changing the order in which you add menu panel and main panel to their parent widget. Add main panel first, then menu panel. Obviously, it's hard to be sure without seeing your code.

How to Force a Flex Chart to Redraw During Runtime

I have a column chart that uses a label function to format the vertical axis. I added a button and want the axis (or chart) to redraw when the button is clicked. Right now, the chart axis renders OK only when initially added to the view state.
I have a function that sets various properties after the chart is initially created. In there, I have tried all these:
myChart.verticalAxis.dataChanged();
myChart.validateNow();
myChart.validateDisplayList();
myChart.validateProperties();
myChart.invalidateDisplayList();
myChart.invalidateProperties();
But they do not change the axis formatting. How can I do this?
The MXML code for the axis is:
< mx:LinearAxis id="verticalAxis" labelFunction="vAxisFormat"/>
The label function is:
private function vAxisFormat(labelValue:Number, previousValue:Object, axis:IAxis):String {
axis.getLabelEstimate();
if (_scale == 1){
return currencyFormatter.format(labelValue/_scale);
}else {
return numberFormatter.format(labelValue/_scale);
}
}
Try updating the data provider. This redraws the graph, so all the components.
Example:
ArrayCollection
arr.refresh ();
XML
char.dataprovider = xmlData
regars

Resources