How to develop multi-touch applications in ubuntu 16.04 through javafx? - javafx

The javafx application:
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.RotateEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.input.SwipeEvent;
import javafx.scene.input.TouchEvent;
import javafx.scene.input.ZoomEvent;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
Application.launch(args);
}
#Override
public void start(Stage primaryStage) {
Group root = new Group();
Scene scene = new Scene(root, 300, 250);
Rectangle rect = new Rectangle();
rect.setWidth(100);
rect.setHeight(100);
root.getChildren().add(rect);
rect.setOnScroll(new EventHandler<ScrollEvent>() {
#Override
public void handle(ScrollEvent event) {
if (!event.isInertia()) {
rect.setTranslateX(rect.getTranslateX() + event.getDeltaX());
rect.setTranslateY(rect.getTranslateY() + event.getDeltaY());
}
System.out.println("Rectangle: Scroll event" + ", inertia: "
+ event.isInertia() + ", direct: " + event.isDirect());
event.consume();
}
});
rect.setOnScrollStarted(new EventHandler<ScrollEvent>() {
#Override
public void handle(ScrollEvent event) {
System.out.println("Rectangle: Scroll started event");
event.consume();
}
});
rect.setOnScrollFinished(new EventHandler<ScrollEvent>() {
#Override
public void handle(ScrollEvent event) {
System.out.println("Rectangle: Scroll finished event");
event.consume();
}
});
rect.setOnZoom(new EventHandler<ZoomEvent>() {
#Override
public void handle(ZoomEvent event) {
rect.setScaleX(rect.getScaleX() * event.getZoomFactor());
rect.setScaleY(rect.getScaleY() * event.getZoomFactor());
System.out.println("Rectangle: Zoom event" + ", inertia: "
+ event.isInertia() + ", direct: " + event.isDirect());
event.consume();
}
});
rect.setOnZoomStarted(new EventHandler<ZoomEvent>() {
#Override
public void handle(ZoomEvent event) {
System.out.println("Rectangle: Zoom event started");
event.consume();
}
});
rect.setOnZoomFinished(new EventHandler<ZoomEvent>() {
#Override
public void handle(ZoomEvent event) {
System.out.println("Rectangle: Zoom event finished");
event.consume();
}
});
rect.setOnRotate(new EventHandler<RotateEvent>() {
#Override
public void handle(RotateEvent event) {
rect.setRotate(rect.getRotate() + event.getAngle());
System.out.println("Rectangle: Rotate event" + ", inertia: "
+ event.isInertia() + ", direct: " + event.isDirect());
event.consume();
}
});
rect.setOnRotationStarted(new EventHandler<RotateEvent>() {
#Override
public void handle(RotateEvent event) {
System.out.println("Rectangle: Rotate event started");
event.consume();
}
});
rect.setOnRotationFinished(new EventHandler<RotateEvent>() {
#Override
public void handle(RotateEvent event) {
System.out.println("Rectangle: Rotate event finished");
event.consume();
}
});
rect.setOnSwipeRight(new EventHandler<SwipeEvent>() {
#Override
public void handle(SwipeEvent event) {
System.out.println("Rectangle: Swipe right event");
event.consume();
}
});
rect.setOnSwipeLeft(new EventHandler<SwipeEvent>() {
#Override
public void handle(SwipeEvent event) {
System.out.println("Rectangle: Swipe left event");
event.consume();
}
});
rect.setOnTouchPressed(new EventHandler<TouchEvent>() {
#Override
public void handle(TouchEvent event) {
System.out.println("Rectangle: Touch pressed event");
event.consume();
}
});
rect.setOnTouchReleased(new EventHandler<TouchEvent>() {
#Override
public void handle(TouchEvent event) {
System.out.println("Rectangle: Touch released event");
event.consume();
}
});
rect.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if (event.isSynthesized()) {
System.out.println("Ellipse: Mouse pressed event from touch"
+ ", synthesized: " + event.isSynthesized());
}
event.consume();
}
});
rect.setOnMouseReleased(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if (event.isSynthesized()) {
System.out.println("Ellipse: Mouse released event from touch"
+ ", synthesized: " + event.isSynthesized());
}
event.consume();
}
});
primaryStage.setScene(scene);
primaryStage.show();
}
}
I'm trying to implement an JavaFX application which are able to make
use of touch events and multi-touch gestures. I'm working with a
capacitive touch screen-eGalax Touch device, with a EETI eGTouch
driver (eGTouchD version: 2.5).It connected to a Ubuntu 16.04 LTS PC
via USB and created multi-touch events (ABS_MT_SLOT,
ABS_MT_TRACKING_ID, ABS_MT_POSITION_X, ABS_MT_POSITION_Y) which can
be read from /dev/input/eventX in the terminal. I have tried a lot
of methods(include MT4j) to realize it but none of them can
recognize the touch events.
But under Windows 7, the same code the touch events are recognized, I can swipe, pinch,zoom, etc.
Now,The zoom in and zoom out events also can realized through mouse scroll event in Ubuntu and Wondows 7.
By contrast, I am found that in Windows 7 the java 8 JDK supply a
jar called java-access-bridge.jar,but in Ubuntu`s JDK it not
included. Is this the key to the problem?
Since I just started to get acquainted with this stuff, I appreciate
any hint or help.

Related

javafx: Progress bar to show the progress of the process?

I want to show progress bar while a functionality is running. What is the best way to show it? Basically I am building a program to send multiple mails on a single click. While sending the mail I want to show progress bar while sending the mails.
The best solution in this case is using a Task.
Example:
Task<Parent> yourTaskName = new Task<Parent>() {
#Override
public Parent call() {
// DO YOUR WORK
//method to set progress
updateProgress(workDone, max);
//method to set labeltext
updateMessage(message);
}
};
//ProgressBar
ProgressBar pBar = new ProgressBar();
//Load Value from Task
pBar.progressProperty().bind(yourTaskName.progressProperty());
//New Loading Label
Label statusLabel = new Label();
//Get Text
statusLabel.setText("Loading...");
//Layout
VBox root = new VBox(statusLabel, pBar);
//SetFill Width TRUE
root.setFillWidth(true);
//Center Items
root.setAlignment(Pos.CENTER);
//SetOnSucceeded methode
yourTaskName.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
#Override
public void handle(WorkerStateEvent event) {
System.out.println("Finish");
}
});
//Start Thread
Thread loadingThread = new Thread(yourTaskName);
loadingThread.start();
Hope this helps you.
P.S.: The code in the task run as a Thread...
I implemented what you want last time ,If you want to show progressIndicator or progressBar when sending is running ,try this part of code
senderThreadlive = new Thread(new Runnable() {
#Override
public void run() {
try {
Platform.runLater(new Runnable() {
#Override
public void run() {
ProgressIndicator WaitingSend=new ProgressIndicator();
WaitingSend.setProgress(ProgressIndicator.INDETERMINATE_PROGRESS);
WaitingBox.getChildren().add(WaitingSend);//this is an HBOX
SendMailButton.setDisable(true);
SendMailButton.setText("sending in progress");
}
});
//call Your method of sending
SimpleMail.EmailSender.sendEmail(MailSenderTxt.getText(), MotMailTxt.getText(), DestMailTxt.getText(), ObjetMailTxt.getText(), org.jsoup.Jsoup.parse(ContentMail.getHtmlText()).text());
Platform.runLater(new Runnable() {
#Override
public void run() {
WaitingSend.setProgress(0);
WaitingSend.setVisible(false);
SendMailButton.setDisable(false);
SendMailButton.setText("Send");
}
});
} catch (AuthenticationFailedException e) {
Platform.runLater(new Runnable() {
#Override
public void run() {
//Your popUp here
}
});
} catch (SendFailedException e) {
Platform.runLater(new Runnable() {
#Override
public void run() {
//Your popUp here
}
});
} catch (MessagingException e) {
Platform.runLater(new Runnable() {
#Override
public void run() {
//Your popUp here
}
});
} catch (Exception ex) {
Platform.runLater(new Runnable() {
#Override
public void run() {
//Your popUp here
}
});
}
}
});
senderThreadlive.start();
Just use javafx.scene.control.ProgressBar
Documentation:
http://docs.oracle.com/javafx/2/ui_controls/progress.htm

javafx catch more then one key press event at the same time

I want to catch the event when we press in 2 keys of keyboard (ctrl + here)
to zoom in tabview here's my code, so far i can just catch only the ctrl, i dont know how to catch the event when we hold ctrl then click on + ( or at least click on ctrl then c every time to zoom) , i had idea of key combination:
final KeyCombination keyCtrlPlus = new KeyCodeCombination(KeyCode.PLUS, KeyCombination.CONTROL_ANY);
but i don't know how to do it in the addEventFilter(). Any help please?
m_TabView.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent e)
{
if (keyCtrlPlus.match(e))
{
//function to zoom tabview
zoomOut(e);
}
}
});
This is how I did it. It's a little simpler, in my opinion. Not to mention, this specifically answers Coeur's question; which was, how to check KeyCombination inside of an addEventFilter method ...
This is in my controller class...
#FXML private TextField textField;
final KeyCombination keyShiftTab = new KeyCodeCombination(KeyCode.TAB, KeyCombination.SHIFT_ANY);
#FXML
public void initialize()
{
textField.addEventFilter(KeyEvent.KEY_RELEASED, new EventHandler<KeyEvent>()
{
#Override
public void handle(KeyEvent e)
{
if (keyShiftTab.match(e))
{
doSomthing();
e.consume();
}
}
});
}
Works like a champ for me. I hope this helps.
You could add a listener, register all key presses of the key pressed event in a bitset and evaluate and unregister them in the key released event.
Something like this, supports multiple keys including modifiers:
import java.util.BitSet;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Main extends Application {
private BitSet keyboardBitSet = new BitSet();
Scene scene;
Label label;
#Override
public void start(Stage primaryStage) {
HBox root = new HBox();
label = new Label();
root.getChildren().add(label);
scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
scene.addEventFilter(KeyEvent.KEY_PRESSED, keyPressedEventHandler);
scene.addEventFilter(KeyEvent.KEY_RELEASED, keyReleasedEventHandler);
// init label text
updateKeyboardStatus();
}
/**
* "Key Pressed" handler for all input events: register pressed key in the bitset
*/
private EventHandler<KeyEvent> keyPressedEventHandler = new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
// register key down
keyboardBitSet.set(event.getCode().ordinal(), true);
updateKeyboardStatus();
}
};
/**
* "Key Released" handler for all input events: unregister released key in the bitset
*/
private EventHandler<KeyEvent> keyReleasedEventHandler = new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
// register key up
keyboardBitSet.set(event.getCode().ordinal(), false);
updateKeyboardStatus();
}
};
/**
* Detect all keys and show them in the label
*/
private void updateKeyboardStatus() {
StringBuilder sb = new StringBuilder();
sb.append("Current key combination: ");
int count = 0;
for( KeyCode keyCode: KeyCode.values()) {
if( keyboardBitSet.get(keyCode.ordinal())) {
if( count > 0) {
sb.append( " ");
}
sb.append(keyCode.toString());
count++;
}
}
label.setText(sb.toString());
}
public static void main(String[] args) {
launch(args);
}
}
Maybe this could help you:
this.scene.addEventHandler(KeyEvent.KEY_RELEASED, new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
if(key.length()>0){
if(key.toString().equals(keyCombination1)){
System.out.println("Key Combination 1 pressed");
}else if(key.toString().equals(keyCombination2)){
System.out.println("Key Combination 2 pressed");
}else if(key.toString().equals(keyCombination3)){
System.out.println("Key Combination 3 pressed");
}
key.setLength(0);
}
}
});
Taken from:
https://code.google.com/p/javafx-demos/source/browse/trunk/javafx-demos/src/main/java/com/ezest/javafx/demogallery/KeyCombinationDemo.java

JavaFX contextmenu not hiding

I'm trying to show a context menu only when there is something selected in the list view.
So I called hide in its on showing event. However, this is not working. The context menu still shows up. Is this a bug, or not its intended use? Because JavaFX api seems to suggest hide() is suppose to do this.
Anyway this is the code.
ContextMenu menu = new ContextMenu();
menu.setOnShowing(new EventHandler<WindowEvent>() {
#Override
public void handle(final WindowEvent event) {
menu.hide();
}
});
It will probably work if you do
public void handle(final WindowEvent event) {
Platform.runLater(new Runnable() {
#Override
public void run() {
menu.hide();
}
});
}
but that really seems like a horrible hack.
Why not just set the context menu only if something is selected?
final ListView<T> listView = ... ;
final ContextMenu menu = new ContextMenu();
listView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<T>() {
#Override
public void changed(ObservableValue<? extends T> obs, T oldValue, T newValue) {
if (newValue == null) {
listView.setContextMenu(null);
} else {
listView.setContextMenu(menu);
}
}
});
(obviously replace T with whatever type your ListView is displaying).
private ContextMenu menu; private MenuItem deleteItem;
table.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {
#Override
public void handle(ContextMenuEvent event) {
if (table.getSelectionModel().getSelectedIndex() != -1) {
deleteItem.setVisible(true);
deleteItem.setText("delete: " + table.getSelectionModel().getSelectedItem().getName());
contextMenu.show(table, event.getScreenX(), event.getScreenY());
} else {
deleteItem.setVisible(false);
}
event.consume();
}
});
primaryStage.getScene().addEventHandler(MouseEvent.MOUSE_ENTERED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) { menu.hide(); }});

Context Menu in JavaFX Task

Following this topic Context menu insight JavaFX Task I want to create Context Menu in JavaFX Task. I tested this code:
static private StringBuilder stringBuilder = new StringBuilder();
private static ContextMenu contextMenu;
private static CountDownLatch menuCreated = new CountDownLatch(1);
static synchronized void writeString(String s)
{
stringBuilder.append(s).append("\n");
}
public static BorderPane init(BorderPane bp) throws Exception
{
System.out.println("***** CALLED");
Task task = new Task()
{
#Override
protected Void call() throws Exception
{
writeString("Task started");
writeString(Thread.currentThread().getName() + " is fx thread: "
+ Platform.isFxApplicationThread());
Platform.runLater(new Runnable()
{
#Override
public void run()
{
writeString(Thread.currentThread().getName() + " is fx thread: "
+ Platform.isFxApplicationThread());
try
{
contextMenu = new ContextMenu();
contextMenu.setId("Test ID");
writeString("Created context menu");
menuCreated.countDown();
}
catch (Exception ex)
{
writeString(ex.getMessage());
ex.printStackTrace();
}
finally
{
writeString("Test");
}
}
});
writeString("Task finished");
return null;
}
};
new Thread(task).start();
MenuItem item1 = new MenuItem("About");
item1.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
System.out.println("About");
}
});
MenuItem item2 = new MenuItem("Preferences");
item2.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
System.out.println("Preferences");
}
});
MenuItem item3 = new MenuItem("Close");
item3.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
//flow.getChildren().remove(bp);
}
});
contextMenu.getItems().addAll(item1, item2, item3);
bp.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>()
{
#Override
public void handle(ContextMenuEvent event)
{
//contextMenu.hide();
System.out.println("*********************** Shown Context Menu ***!!!!!!!");
contextMenu.show(bp, event.getScreenX(), event.getScreenY());
event.consume();
}
});
bp.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>()
{
#Override
public void handle(MouseEvent event)
{
contextMenu.hide();
}
});
menuCreated.await();
return bp;
}
With this code I set Context Menu for BorderPane. When I click with the right mouse button I see the debug message *********************** Shown Context Menu ***!!!!!!! but there is no context menu. Can you help me to fix this code?

Label is not displayed properly when I exit Pie chart

I want to create Pie chart with caption. When I move the mouse over the Pie chart the text is displayed very smoothly.
public void naiveAddData(String name, double value)
{
pieChartData.add(new Data(name, value));
caption = new Label();
caption.setTextFill(Color.DARKORANGE);
caption.setStyle("-fx-font: 24 arial;");
for (final Data data : chart.getData())
{
data.getNode().addEventHandler(MouseEvent.MOUSE_MOVED,
new EventHandler<MouseEvent>()
{
#Override
public void handle(MouseEvent e)
{
caption.setTranslateX(e.getSceneX() + 15);
caption.setTranslateY(e.getSceneY());
caption.setText(String.valueOf(data.getPieValue()) + "%");
}
});
}
}
But when I move the mouse outside the Pie Chart the caption is still there. It's not removed. How I can fix this?
P.S I managed to solve the problem this way:
data.getNode().addEventHandler(MouseEvent.MOUSE_MOVED,
new EventHandler<MouseEvent>()
{
#Override
public void handle(MouseEvent e)
{
caption.setTranslateX(e.getSceneX() + 15);
caption.setTranslateY(e.getSceneY());
caption.setText(String.valueOf(data.getPieValue()) + "%");
caption.setVisible(true);
}
});
data.getNode().addEventHandler(MouseEvent.MOUSE_EXITED,
new EventHandler<MouseEvent>()
{
#Override
public void handle(MouseEvent e)
{
caption.setVisible(false);
}
});
}
Is there any better solution?
If you have the parent of the caption you can remove it from its parent on mouse exit and add it again on mouse enter.
Parent captionparent = ...
...
data.getNode().addEventHandler(MouseEvent.MOUSE_EXITED,
new EventHandler<MouseEvent>()
{
#Override
public void handle(MouseEvent e)
{
captionparent.getChildren().remove(caption);
}
});
Here is a complete example using a Tooltip.
public class ChartingMain extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
PieChart pc = new PieChart();
pc.setTitle("Most popular programming languages");
ObservableList<PieChart.Data> pcdata = FXCollections.<PieChart.Data>observableArrayList();
pcdata.add(new PieChart.Data("C#", 20d));
pcdata.add(new PieChart.Data("Java",800d));
pcdata.add(new PieChart.Data("C/C++", 400d));
final double total = 20d + 800d + 400d;
pc.dataProperty().set(pcdata);
stage.setScene(new Scene(pc));
stage.show();
for(PieChart.Data data : pc.getData()){
final Tooltip t = new Tooltip(data.getName() + " : " + new DecimalFormat("#.##").format((data.getPieValue() / total) * 100) + " %");
data.getNode().setOnMouseEntered(new EventHandler<MouseEvent>(){
#Override
public void handle(MouseEvent me) {
if(me.getSource() instanceof Node){
Node sender = (Node) me.getSource();
Tooltip.install(sender, t);
sender.setEffect(new Glow(0.5));
}
}});
data.getNode().setOnMouseExited(new EventHandler<MouseEvent>(){
#Override
public void handle(MouseEvent me) {
if(me.getSource() instanceof Node){
Node sender = (Node) me.getSource();
sender.setEffect(null);
t.hide();
}
}});
}
}
}

Resources