A small code to present, how can we work with buttons in JavaFx:
package javafxapplication_button;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
/**
*
* @author Sipos Lehel
*/
public class JavaFXApplication_Button extends Application {
Button btn = null;
DropShadow shadow = null;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
//button icon
Image imageDecline = new Image(getClass().getResourceAsStream("desktop.gif"));
btn = new Button("OK");
btn.setGraphic(new ImageView(imageDecline));
//effects at mouse moving upon the button
shadow = new DropShadow();
//Adding the shadow when the mouse cursor is on and zooming in
btn.addEventHandler(MouseEvent.MOUSE_ENTERED, new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent e) {
btn.setEffect(shadow);
btn.setScaleX(1.5);
btn.setScaleY(1.5);
}
});
//Removing the shadow when the mouse cursor is off and zooming out
btn.addEventHandler(MouseEvent.MOUSE_EXITED, new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent e) {
btn.setEffect(null);
btn.setScaleX(1);
btn.setScaleY(1);
}
});
//on click event - fire a Dialog Box message
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
Stage dialogStage = new Stage();
dialogStage.initModality(Modality.WINDOW_MODAL);
StackPane panel = new StackPane();
panel.getChildren().add(new Text("This is a message for you: Hello world!"));
Scene scene_message = new Scene(panel);
dialogStage.setScene(scene_message);
dialogStage.setHeight(100);
dialogStage.setWidth(300);
dialogStage.show();
}
});
//styling the button
btn.setStyle("-fx-font: 22 arial; -fx-text-base-color: blue; -fx-base: #b6e7c9");
StackPane root = new StackPane();
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}
Thursday, May 10, 2012
Wednesday, May 9, 2012
Vaadin - Form Data Saving into a second form
Add the following unit into your vaadin application (it runs under the lehel.sipos.vaadin package):
/*
* MyApplication.java
*
* Created on 2012. május 9., 12:16
*/
package lehel.sipos.vaadin;
import com.vaadin.Application;
import com.vaadin.data.Validator.EmptyValueException;
import com.vaadin.data.util.ObjectProperty;
import com.vaadin.data.util.PropertysetItem;
import com.vaadin.data.validator.StringLengthValidator;
import com.vaadin.ui.*;
import com.vaadin.ui.themes.Reindeer;
/**
*
* @author Sipos Lehel
* @version
*/
public class MyApplication extends Application {
@Override
public void init() {
Window mainWindow = new Window("MyApplication");
// Some data source
PropertysetItem item = new PropertysetItem();
item.addItemProperty("firstName", new ObjectProperty<String>(""));
item.addItemProperty("lastName", new ObjectProperty<String>(""));
// A buffered form bound to a data source - main form for input data
final Form form = new Form();
form.setItemDataSource(item);
form.setWriteThrough(false);
//caption of text fields in main form
form.getField("firstName").setCaption("Keresztnév");
form.getField("lastName").setCaption("Vezetéknév");
// A required field
form.getField("firstName").setRequired(true);
// A required field with an error message
TextField lastName = (TextField) form.getField("lastName");
lastName.setRequired(true);
lastName.setRequiredError("Add meg a vezetéknevet");
lastName.setImmediate(true);
lastName.setValidationVisible(true);
lastName.addValidator(new StringLengthValidator("Nem lehet üres", 1, 100, false));
// Commit the buffered form data to the data source
form.getFooter().addComponent(new Button("Mentés", new Button.ClickListener() {
public void buttonClick(Button.ClickEvent event) {
try {
if (!form.isValid())
form.setValidationVisible(true);
else
form.commit();
} catch (EmptyValueException e) {
// A required value was missing
}
}
}));
// Have a read-only form that displays the content of the first form after a data commit
Form roform = new Form();
roform.setItemDataSource(item);
roform.addStyleName(Reindeer.LAYOUT_WHITE);
roform.setReadOnly(true);
//caption of text fields in the readonly form form
roform.getField("firstName").setCaption("Elmentett keresztnév");
roform.getField("lastName").setCaption("Elmentett vezetéknév");
//putting the input form near the readonly form
HorizontalLayout hlayout = new HorizontalLayout();
hlayout.setSpacing(true);
hlayout.setWidth("600px");
hlayout.addComponent(form);
hlayout.addComponent(roform);
mainWindow.addComponent(hlayout);
setMainWindow(mainWindow);
}
}
/*
* MyApplication.java
*
* Created on 2012. május 9., 12:16
*/
package lehel.sipos.vaadin;
import com.vaadin.Application;
import com.vaadin.data.Validator.EmptyValueException;
import com.vaadin.data.util.ObjectProperty;
import com.vaadin.data.util.PropertysetItem;
import com.vaadin.data.validator.StringLengthValidator;
import com.vaadin.ui.*;
import com.vaadin.ui.themes.Reindeer;
/**
*
* @author Sipos Lehel
* @version
*/
public class MyApplication extends Application {
@Override
public void init() {
Window mainWindow = new Window("MyApplication");
// Some data source
PropertysetItem item = new PropertysetItem();
item.addItemProperty("firstName", new ObjectProperty<String>(""));
item.addItemProperty("lastName", new ObjectProperty<String>(""));
// A buffered form bound to a data source - main form for input data
final Form form = new Form();
form.setItemDataSource(item);
form.setWriteThrough(false);
//caption of text fields in main form
form.getField("firstName").setCaption("Keresztnév");
form.getField("lastName").setCaption("Vezetéknév");
// A required field
form.getField("firstName").setRequired(true);
// A required field with an error message
TextField lastName = (TextField) form.getField("lastName");
lastName.setRequired(true);
lastName.setRequiredError("Add meg a vezetéknevet");
lastName.setImmediate(true);
lastName.setValidationVisible(true);
lastName.addValidator(new StringLengthValidator("Nem lehet üres", 1, 100, false));
// Commit the buffered form data to the data source
form.getFooter().addComponent(new Button("Mentés", new Button.ClickListener() {
public void buttonClick(Button.ClickEvent event) {
try {
if (!form.isValid())
form.setValidationVisible(true);
else
form.commit();
} catch (EmptyValueException e) {
// A required value was missing
}
}
}));
// Have a read-only form that displays the content of the first form after a data commit
Form roform = new Form();
roform.setItemDataSource(item);
roform.addStyleName(Reindeer.LAYOUT_WHITE);
roform.setReadOnly(true);
//caption of text fields in the readonly form form
roform.getField("firstName").setCaption("Elmentett keresztnév");
roform.getField("lastName").setCaption("Elmentett vezetéknév");
//putting the input form near the readonly form
HorizontalLayout hlayout = new HorizontalLayout();
hlayout.setSpacing(true);
hlayout.setWidth("600px");
hlayout.addComponent(form);
hlayout.addComponent(roform);
mainWindow.addComponent(hlayout);
setMainWindow(mainWindow);
}
}
Tuesday, May 8, 2012
Vaadin
Vaadin is a web application framework for Rich Internet Applications (RIA).
In contrast to Javascript libraries and browser-plugin based solutions,
it features a robust server-side architecture. This means that the
largest part of the application logic runs securely on the server. Google Web Toolkit (GWT) is used on the browser side to ensure a rich and fluent user experience.
JavaFX
JavaFX is the next step in the
evolution of Java as a rich client platform. It is designed to provide a
lightweight, hardware-accelerated Java UI platform for enterprise
business applications. With JavaFX, developers can preserve existing
investments by reusing Java libraries in their applications. They can
even access native system capabilities, or seamlessly connect to
server-based middleware applications.
Subscribe to:
Posts (Atom)