Is it possible to set instance of XYChart.Series to act on setOnMouseEntered? It seems to me that one precondition to make it work would be to implement EventTarget interface. As for JavaFX XYChart.Series I would like to highlight the following data series when cursor touches the yellow line (instance of XYChart.Series):http://docs.oracle.com/javafx/2.0/charts/img/line-series.png
To be more precise I would like to do the following but for instance of XYChart.Series not Button:
public class Temp {
/*code removed*/
Button btn = new Button("Button to touch");
btn.setOnMouseEntered(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent e) {
System.out.println("Cursor just touched the button!");
}
});
/*code removed*/
}
Lookup the appropriate nodes and add the event handlers you want to them.
Here is an example.
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.*;
import javafx.scene.chart.*;
import javafx.scene.effect.Glow;
import javafx.scene.input.MouseEvent;
import javafx.scene.shape.Path;
import javafx.stage.Stage;
public class LineChartSample extends Application {
#Override public void start(Stage stage) {
//create the chart
final NumberAxis xAxis = new NumberAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Number of Month");
final LineChart<Number, Number> lineChart = new LineChart<>(xAxis, yAxis);
lineChart.setTitle("Stock Monitoring, 2010");
XYChart.Series series = new XYChart.Series();
series.setName("My portfolio");
series.getData().addAll(new XYChart.Data(1, 23),new XYChart.Data(2, 14),new XYChart.Data(3, 15),new XYChart.Data(4, 24),new XYChart.Data(5, 34),new XYChart.Data(6, 36),new XYChart.Data(7, 22),new XYChart.Data(8, 45),new XYChart.Data(9, 43),new XYChart.Data(10, 17),new XYChart.Data(11, 29),new XYChart.Data(12, 25));
lineChart.getData().add(series);
// show the scene.
Scene scene = new Scene(lineChart, 800, 600);
stage.setScene(scene);
stage.show();
// make the first series in the chart glow when you mouse over it.
Node n = lineChart.lookup(".chart-series-line.series0");
if (n != null && n instanceof Path) {
final Path path = (Path) n;
final Glow glow = new Glow(.8);
path.setEffect(null);
path.setOnMouseEntered(new EventHandler<MouseEvent>() {
#Override public void handle(MouseEvent e) {
path.setEffect(glow);
}
});
path.setOnMouseExited(new EventHandler<MouseEvent>() {
#Override public void handle(MouseEvent e) {
path.setEffect(null);
}
});
}
}
public static void main(String[] args) { launch(args); }
}
Related
I am attempting to bind a progress bar to the progress of a service. I have created the progress bar in scene builder, and have attempted the below code. But the progress bar continually runs, and does not represent the service it is connected to. It should be running while the service is running, and representing the data being downloaded. How do I bind the progress bar to represent the service I have created.
#FXML
private ProgressBar ProgressBar;
service.start();
ProgressBar.progressProperty().bind(service.workDoneProperty());
You should bind ProgressBar's progressProperty() to Service's progressProperty() and not to its workDoneProperty i.e.
import javafx.application.Application;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
ProgressBar progressBar = new ProgressBar();
StackPane root = new StackPane(progressBar);
Scene scene = new Scene(root, 200, 200);
primaryStage.setScene(scene);
primaryStage.show();
Service service = new Service() {
#Override
protected Task createTask() {
return new Task() {
#Override
protected Object call() throws Exception {
for(int i=0; i<100; i++){
updateProgress(i, 100);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
};
}
};
progressBar.progressProperty().bind(service.progressProperty());
service.start();
}
public static void main(String[] args) {
Application.launch(args);
}
}
This program shall paste an image from clipboard into an ImageView (on Windows 10). Unfortunately the image is not correctly displayed.
public class PasteImageFromClipboard extends Application {
ImageView imageView = new ImageView();
Button bnPaste = new Button("Paste");
public static void main(String[] args) {
Application.launch(args);
}
#Override
public void start(Stage stage) throws Exception {
bnPaste.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
Clipboard cb = Clipboard.getSystemClipboard();
if (cb.hasImage()) {
Image image = cb.getImage();
imageView.setImage(image);
}
}
});
VBox vbox = new VBox();
vbox.getChildren().addAll(bnPaste, imageView);
Scene scene = new Scene(vbox);
stage.setScene(scene);
stage.setWidth(400);
stage.setHeight(400);
stage.show();
}
}
Steps to reproduce:
Start cmd.exe
Press ALT-Print to copy the cmd window into the clipboard
Start program PasteImageFromClipboard
Press "Paste" button in PasteImageFromClipboard
This result is displayed on my computer:
It should be like this:
Is there more code required to draw the image correctly?
found this solution by the help of
https://community.oracle.com/thread/2238566
package com.wilutions.jiraddin;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import javax.imageio.ImageIO;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class PasteImageFromClipboard extends Application {
ImageView imageView = new ImageView();
Button bnPaste = new Button("Paste");
public static void main(String[] args) {
Application.launch(args);
}
#Override
public void start(Stage stage) throws Exception {
bnPaste.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
try {
java.awt.Image image = getImageFromClipboard();
if (image != null) {
javafx.scene.image.Image fimage = awtImageToFX(image);
imageView.setImage(fimage);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
});
VBox vbox = new VBox();
vbox.getChildren().addAll(bnPaste, imageView);
Scene scene = new Scene(vbox);
stage.setScene(scene);
stage.setWidth(400);
stage.setHeight(400);
stage.show();
}
private java.awt.Image getImageFromClipboard() {
Transferable transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
if (transferable != null && transferable.isDataFlavorSupported(DataFlavor.imageFlavor)) {
try {
return (java.awt.Image) transferable.getTransferData(DataFlavor.imageFlavor);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
private static javafx.scene.image.Image awtImageToFX(java.awt.Image image) throws Exception {
if (!(image instanceof RenderedImage)) {
BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null),
BufferedImage.TYPE_INT_ARGB);
Graphics g = bufferedImage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
image = bufferedImage;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write((RenderedImage) image, "png", out);
out.flush();
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
return new javafx.scene.image.Image(in);
}
}
I want to add Tooltip when I move cursor over a chart line. I found this example:
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.AreaChart;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;
public class AnimatedLineChart extends Application {
private static final int MAX_DATA_POINTS = 50;
private int xSeriesData = 0;
private XYChart.Series series1;
private XYChart.Series series2;
private XYChart.Series series3;
private ExecutorService executor;
private AddToQueue addToQueue;
private ConcurrentLinkedQueue<Number> dataQ1 = new ConcurrentLinkedQueue<Number>();
private ConcurrentLinkedQueue<Number> dataQ2 = new ConcurrentLinkedQueue<Number>();
private ConcurrentLinkedQueue<Number> dataQ3 = new ConcurrentLinkedQueue<Number>();
private NumberAxis xAxis;
private void init(Stage primaryStage) {
xAxis = new NumberAxis(0,MAX_DATA_POINTS,MAX_DATA_POINTS/10);
xAxis.setForceZeroInRange(false);
xAxis.setAutoRanging(false);
xAxis.setTickLabelsVisible(false);
xAxis.setTickMarkVisible(false);
xAxis.setMinorTickVisible(false);
NumberAxis yAxis = new NumberAxis();
yAxis.setAutoRanging(true);
//-- Chart
final LineChart<Number, Number> sc = new LineChart<Number, Number>(xAxis, yAxis) {
// Override to remove symbols on each data point
#Override protected void dataItemAdded(Series<Number, Number> series, int itemIndex, Data<Number, Number> item) {}
};
sc.setAnimated(false);
sc.setId("liveLineeChart");
sc.setTitle("Animated Line Chart");
//-- Chart Series
series1 = new XYChart.Series<Number, Number>();
series2 = new XYChart.Series<Number, Number>();
series3 = new XYChart.Series<Number, Number>();
sc.getData().addAll(series1, series2, series3);
primaryStage.setScene(new Scene(sc));
}
#Override public void start(Stage stage) {
stage.setTitle("Animated Line Chart Sample");
init(stage);
stage.show();
executor = Executors.newCachedThreadPool(new ThreadFactory() {
#Override public Thread newThread(Runnable r) {
Thread thread = new Thread(r);
thread.setDaemon(true);
return thread;
}
});
addToQueue = new AddToQueue();
executor.execute(addToQueue);
//-- Prepare Timeline
prepareTimeline();
}
private class AddToQueue implements Runnable {
public void run() {
try {
// add a item of random data to queue
dataQ1.add(Math.random());
dataQ2.add(Math.random());
dataQ3.add(Math.random());
Thread.sleep(500);
executor.execute(this);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
//-- Timeline gets called in the JavaFX Main thread
private void prepareTimeline() {
// Every frame to take any data from queue and add to chart
new AnimationTimer() {
#Override public void handle(long now) {
addDataToSeries();
}
}.start();
}
private void addDataToSeries() {
for (int i = 0; i < 20; i++) { //-- add 20 numbers to the plot+
if (dataQ1.isEmpty()) break;
series1.getData().add(new AreaChart.Data(xSeriesData++, dataQ1.remove()));
series2.getData().add(new AreaChart.Data(xSeriesData++, dataQ2.remove()));
series3.getData().add(new AreaChart.Data(xSeriesData++, dataQ3.remove()));
}
// remove points to keep us at no more than MAX_DATA_POINTS
if (series1.getData().size() > MAX_DATA_POINTS) {
series1.getData().remove(0, series1.getData().size() - MAX_DATA_POINTS);
}
if (series2.getData().size() > MAX_DATA_POINTS) {
series2.getData().remove(0, series2.getData().size() - MAX_DATA_POINTS);
}
if (series3.getData().size() > MAX_DATA_POINTS) {
series3.getData().remove(0, series3.getData().size() - MAX_DATA_POINTS);
}
// update
xAxis.setLowerBound(xSeriesData-MAX_DATA_POINTS);
xAxis.setUpperBound(xSeriesData-1);
}
public static void main(String[] args) {
launch(args);
}
}
For example I would like to display something like this:
Is this possible with JavaFX 8? On mouse hover it is showing Date.
Is there any similar example that I can use for my case?
I have managed to pull something very close to what you want. Have a look at the image below
I have used DateAxis for populating date on X-axis, along with events on Y-axis. Once the data is populated in the LineChart, iterate through its data and apply a tooltip on each node.
I have also used a styleclass on mouseEntered to apply the effect as shown on the image. This styleclass is removed on mouseExit
Have a look at the code below :
ToolTipOnLineChart.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Tooltip;
import javafx.stage.Stage;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ToolTipOnLineChart extends Application {
#SuppressWarnings({ "unchecked", "rawtypes" })
#Override
public void start(Stage stage) throws ParseException {
stage.setTitle("Line Chart Sample");
final DateAxis xAxis = new DateAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Date");
yAxis.setLabel("Events");
final LineChart<Date,Number> lineChart = new LineChart<>(xAxis, yAxis);
lineChart.setTitle("Events");
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MMM/yyyy");
XYChart.Series<Date,Number> series = new XYChart.Series<>();
series.setName("Events this Year");
series.getData().add(new XYChart.Data(dateFormat.parse("11/Jan/2014"), 23));
series.getData().add(new XYChart.Data(dateFormat.parse("09/Feb/2014"), 14));
series.getData().add(new XYChart.Data(dateFormat.parse("22/Mar/2014"), 15));
series.getData().add(new XYChart.Data(dateFormat.parse("14/Apr/2014"), 24));
series.getData().add(new XYChart.Data(dateFormat.parse("22/May/2014"), 34));
series.getData().add(new XYChart.Data(dateFormat.parse("07/Jun/2014"), 36));
series.getData().add(new XYChart.Data(dateFormat.parse("22/Jul/2014"), 22));
series.getData().add(new XYChart.Data(dateFormat.parse("21/Aug/2014"), 45));
series.getData().add(new XYChart.Data(dateFormat.parse("04/Sep/2014"), 43));
series.getData().add(new XYChart.Data(dateFormat.parse("22/Oct/2014"), 17));
series.getData().add(new XYChart.Data(dateFormat.parse("30/Nov/2014"), 29));
series.getData().add(new XYChart.Data(dateFormat.parse("10/Dec/2014"), 25));
Scene scene = new Scene(lineChart,800,600);
scene.getStylesheets().add(getClass().getResource("chart.css").toExternalForm());
lineChart.getData().add(series);
stage.setScene(scene);
stage.show();
/**
* Browsing through the Data and applying ToolTip
* as well as the class on hover
*/
for (XYChart.Series<Date, Number> s : lineChart.getData()) {
for (XYChart.Data<Date, Number> d : s.getData()) {
Tooltip.install(d.getNode(), new Tooltip(
d.getXValue().toString() + "\n" +
"Number Of Events : " + d.getYValue()));
//Adding class on hover
d.getNode().setOnMouseEntered(event -> d.getNode().getStyleClass().add("onHover"));
//Removing class on exit
d.getNode().setOnMouseExited(event -> d.getNode().getStyleClass().remove("onHover"));
}
}
}
public static void main(String[] args) {
launch(args);
}
}
chart.css
.onHover{
-fx-background-color: ORANGE;
}
I am having a small application in GWT , the main page is having the initWidget as VerticalPanel and inside there are just other VerticalPanel and horizontalPanels.
But I am not able to see browser scrolling. The verticalpanels are going beyond browser height but there is scrollbars appearing .
Any idea what could be the reason please.
thanks
code::
private Button btnLoad = new Button("load more results");
private CheckBox check_byPakag= new CheckBox("by package");
private CheckBox check_Inheritance= new CheckBox("by inheritance");
private CheckBox check_byComposition= new CheckBox("by composition");
private CheckBox check_byDependency= new CheckBox("by dependency");
private VerticalPanel vpnl_Main = new VerticalPanel();
private FlowPanel flowpanelImages = new FlowPanel();
private VerticalPanel scrollImages;
private IndexServiceAsync rpcService = GWT.create(IndexService.class);
private SuggestBox text_Wild_Card;
private MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
private int start = 0;
private int finish = 10; // Total number of results to display at first time
private int totalNumberOfResultsToShow = 10; //Total number of results to display on every call(when load more results button is pressed)
private int scrollPosition = 0;
private VerticalPanel vpnlScroll = new VerticalPanel();
public MainPageView(){
check_byPakag.setChecked(true);
check_Inheritance.setChecked(true);
check_byComposition.setChecked(true);
check_byDependency.setChecked(true);
initWidget(vpnl_Main);
getSuggestions();
scrollImages = new VerticalPanel();
scrollImages.add(vpnlScroll);
vpnlScroll.add(flowpanelImages);
btnLoad.setEnabled(false);
vpnl_Main.setSpacing(4);
Window.addResizeHandler(new ResizeHandler(){
#Override
public void onResize(ResizeEvent event) {
}});
}
public void getSuggestions(){
rpcService.getAllSuggestions(new AsyncCallback<List<String>>(){
#Override
public void onFailure(Throwable caught) {
System.out.println(caught.getMessage());
}
#Override
public void onSuccess(List<String> result) {
for(int i=0;i<result.size(); i++){
oracle.add(result.get(i));
}
text_Wild_Card = new SuggestBox(createWildCardOracle());
text_Wild_Card.setText("*");
layout(vpnl_Main);
getIndexData();
}});
}
#SuppressWarnings("deprecation")
public void getIndexData(){
final DecoratedPopupPanel popup = new DecoratedPopupPanel();
popup.setWidget(new Label("Loading.."));
popup.center();
rpcService.getIndexData(text_Wild_Card.getText(), check_byPakag.isChecked(), check_Inheritance.isChecked(), check_byComposition.isChecked(), check_byDependency.isChecked(), start, finish, new AsyncCallback<IndexDataSet>(){
#Override
public void onFailure(Throwable caught) {
Window.alert("getIndexData failed"+ caught.getMessage());
}
#Override
public void onSuccess(IndexDataSet result) {
flowpanelImages.clear();
if(result.hasMore()){
btnLoad.setEnabled(true);
}else{
btnLoad.setEnabled(false);
}
for(int i=0; i< result.getResults().size(); i++){
final Image image = new Image(result.getResults().get(i).stampImageURL);
//////////////
final Label lbl = new Label("dummy");
lbl.setStyleName("invisibleImageLabel");
VerticalPanel vpnlImage = new VerticalPanel();
vpnlImage.add(image);
vpnlImage.add(lbl);
//////////
final DataImage dataImage = new DataImage();
dataImage.setUrl(result.getResults().get(i).stampImageURL);
dataImage.setDescription(result.getResults().get(i).description);
image.addMouseOverHandler(new MouseOverHandler(){
#Override
public void onMouseOver(MouseOverEvent event) {
lbl.setText(dataImage.getDescription());
lbl.setStyleName("imageLabel");
}});
image.addMouseOutHandler(new MouseOutHandler(){
#Override
public void onMouseOut(MouseOutEvent event) {
lbl.setStyleName("invisibleImageLabel");
}});
image.addClickHandler(new ClickHandler(){
#Override
public void onClick(ClickEvent event) {
RootPanel.get("bodyContainer").clear();
History.newItem("diagramView / "+dataImage.getUrl());
}});
vpnlImage.addStyleName("imageStyle");
vpnlImage.addStyleName("paddedFlowPanel");
flowpanelImages.add(vpnlImage);
}
//
HorizontalPanel hpnlBtn = new HorizontalPanel();
hpnlBtn.setWidth("100%");
hpnlBtn.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
hpnlBtn.add(btnLoad);
btnLoad.setStyleName("nextLine");
flowpanelImages.add(hpnlBtn);
popup.removeFromParent();
}});
}
private SuggestOracle createWildCardOracle() {
return oracle;
}
private void layout(VerticalPanel vpnl_Main) {
FlexTable flexWildCard = new FlexTable();
VerticalPanel vpnlCheckBoxes = new VerticalPanel();
HorizontalPanel hpnlWildCard = new HorizontalPanel();
flexWildCard.setWidget(0,0,new Label("Show only class diagrams containing classes named: (use * for wildcard)"));
flexWildCard.setWidget(0,1,text_Wild_Card);
text_Wild_Card.setWidth("200px");
vpnlCheckBoxes.setWidth("200px");
vpnlCheckBoxes.add(check_byPakag);
vpnlCheckBoxes.add(check_Inheritance);
vpnlCheckBoxes.add(check_byComposition);
vpnlCheckBoxes.add(check_byDependency);
hpnlWildCard.add(vpnlCheckBoxes);
hpnlWildCard.add(flexWildCard);
vpnl_Main.add(hpnlWildCard);
}
Does anybody know how or if you can place a smaller composite inside a larger composite.
For example I want the smaller composite to be in the centre of the large composite and visible and when a button is pressed in the larger composite a picture appears in the smaller composite?
Would be extremely glad of your help.
Ann.
I'm not sure if I understand your question, you meant something like this..?
import java.net.URL;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class CompositeInComposite {
private Display display = null;
private Shell shell = null;
private Composite composite = null;
private Image img = null;
private URL dog = null;
private URL cat = null;
public CompositeInComposite() {
display = new Display();
shell = new Shell(display);
shell.setLayout(new FillLayout(SWT.VERTICAL));
shell.setSize(300, 300);
Button btn = new Button(shell, SWT.PUSH);
btn.setText("show cat");
btn.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
try {
img = new Image(display, cat.openStream());
composite.redraw();
} catch(Exception ex) {
ex.printStackTrace();
}
}
});
try {
cat = new URL("http://upload.wikimedia.org/wikipedia/commons/thumb/6/64/Collage_of_Six_Cats-02.jpg/250px-Collage_of_Six_Cats-02.jpg");
dog = new URL("http://upload.wikimedia.org/wikipedia/commons/thumb/2/26/YellowLabradorLooking_new.jpg/260px-YellowLabradorLooking_new.jpg");
img = new Image(display, dog.openStream());
} catch (Exception e) {
e.printStackTrace();
}
composite = new Composite(shell, SWT.BORDER);
composite.addPaintListener(new PaintListener() {
#Override
public void paintControl(PaintEvent e) {
e.gc.drawImage(img, 0, 0);
}
});
// shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
public static void main(String[] args) {
new CompositeInComposite();
}
}
The alignment of the button, it's size, etc. is just a proper configuration of layout manager, I would recommend MigLayout as IMO best layout manager that exists.