lwuit calendar with button event - java-me

i tried to show the calendar on button click using form but i'm unable to change the date and very much struggled to find where the focus .
...
Button mdate=new Button("change date");
mdate.addActionListener(this);
...
public void actionPerformed(ActionEvent ae) {
Form cal= new Form();
com.sun.lwuit.Calendar c =new com.sun.lwuit.Calendar();
c.setFocus(true);
c.addActionListener(this);
cal.addComponent(c);
cal.show();
}
how to show and hide calendar on button click in a better way

Better you can use Dialog (like pop up) instead of Form. You can easily dispose within a Form. No need to show another form. See the below sample code,
Button button = new Button("Click me");
form.addComponent(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
final Dialog cal = new Dialog();
final com.sun.lwuit.Calendar c = new com.sun.lwuit.Calendar();
c.setFocus(true);
c.addActionListener(this);
cal.addComponent(c);
cal.addCommand(new Command("Cancel") {
public void actionPerformed(ActionEvent evt) {
cal.dispose();
}
});
c.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.out.println("Selected date :: " + c.getDate().toString())
}
});
cal.show(20, 20, 20, 20, true, false);
}
});
And add the selected and unselected style for Calendar like CalendarSelectedDay, CalendarDate. Also add the selected and unselected style for ComboBox.

Related

Android Studio: How to create particular Alert Dialog based on where the user clicks

I am new to Android Studio programming. My program has a recyclerview with 3 Textviews, and one image. I would like an alert dialog to appear based on where the user clicks on the screen, for instance if the user clicks on the image the dialog says, "You clicked on image". If the user clicks on the text, it would say "You clicked on the text".
My onclick listener is as follows: itemView.setOnClickListener(this);
#Override
public void onClick(View view) {
int index = getAdapterPosition();
if (playerNames.equals(index)) {
showDialogPlayerNames();
}
if (numberPlayers.equals(index)){
showDialogPlayerNumber();
}
if (ScorePlayers.equals(index)){
showDialogPlayerScore();
}
if (picturePlayers.equals(index)) {
showDialogPlayerImage();
}
}
}
Here is my alert dialog code:
void showDialogPlayerNames() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("Click Information");
alertDialog.setIcon(R.drawable.ic_baseline_info_24);
alertDialog.setMessage("You clicked Player Name");
alertDialog.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
alertDialog.create().show();
}
Thank you
You need to add parameters in your methods
showDialogPlayerNames("Something text here");
void showDialogPlayerNames(String myText) {
alertDialog.setMessage(myText);
}

TextField type to Show Button in javafx

I am trying Show a Button on TextField look like Windows 8 Metro theme in javafx.
If TextField is empty button is invisible otherwise button show.
In this stage i'm little close to success. i use this code to make it.
#FXML
private TextField tfMyName;//fx:id="tfMyName"
#FXML
private Button btnClear;//fx:id="btnClear"
#Override
public void initialize(URL url, ResourceBundle rb) {
clearTextFieldByButton(tfMyName, btnClear);
}
public void clearTextFieldByButton(TextField value, Button btn){
btn.setVisible(false);
value.setOnKeyTyped(new EventHandler<KeyEvent>(){
#Override
public void handle(KeyEvent event) {
if ((value.textProperty().get().length() < 0) || (value.textProperty().get().equals(""))) {
btn.setVisible(false);
} else if (value.textProperty().get().length() > -1 || (!value.textProperty().get().equals(""))) {
btn.setVisible(true);
}
}
});
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
tfMyName.clear();
btn.setVisible(false);
tfMyName.requestFocus();
}
});
Using this code by default button is invisible but the button is only visible when i type more then one Characters.
But i need if anything input into the TextField to Button show.
But when i remove the condition under KeyEvent replace by
value.setOnKeyTyped(new EventHandler<KeyEvent>(){
#Override
public void handle(KeyEvent event) {
btn.setVisible(true);
}
});
Then btn show if any character input into the TextField
You may also prefer to use JavaFX binding mechanism:
#Override
public void start( final Stage primaryStage )
{
TextField textfield = new TextField();
Button button = new Button( "my button" );
button.visibleProperty().bind( textfield.textProperty().isEmpty().not() );
final Scene scene = new Scene( new HBox( button, textfield ), 800, 600 );
primaryStage.setScene( scene );
primaryStage.show();
}
The actual problem in your code:
You have attached a listener to field when "OnKeyTyped", at this stage the newly typed text is not appended to the textfield's text value so your if-else condition will not see it. Instead, the correct way should be attaching the listener on "OnKeyReleased".
Add a listener to the textProperty() of the TextField. Check if the value is empty, hide the button else show it. It will be called whenever a character is added or removed from the textfield.
Here is a MCVE, you can just add the listener to the initialize method of the controller.
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class HideButtonOnTextEntered extends Application {
#Override
public void start(Stage stage) {
TextField textField = new TextField();
Button button = new Button("Button");
button.setVisible(false);
VBox root = new VBox(20, textField, button);
root.setAlignment(Pos.CENTER);
Scene scene = new Scene(root, 200, 200);
stage.setScene(scene);
stage.show();
textField.textProperty().addListener((ov, oldValue, newValue) -> {
if (newValue.isEmpty()) {
button.setVisible(false);
} else {
button.setVisible(true);
}
});
}
public static void main(String[] args) {
launch(args);
}
}

How to create modal dialog with image

I have this very simple modal dialog:
public class DialogPanels
{
public void initClosemainAppDialog(final Stage primaryStage)
{
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>()
{
#Override
public void handle(WindowEvent event)
{
event.consume(); // Do nothing on close request
// Dialog Stage init
final Stage dialog = new Stage();
// If you want to freeze the background during dialog appearence set Modality.APPLICATION_MODAL
// or to allow clicking on the mainstage components set Modality.NONE
// and set dialog.showAndWait();
dialog.initModality(Modality.APPLICATION_MODAL);
dialog.initOwner(primaryStage);
// Frage - Label
Label label = new Label("Exit from the program");
// Button "Yes"
Button okBtn = new Button("Yes");
okBtn.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
//primaryStage.close();
//dialog.close();
//Platform.exit();
System.exit(0);
}
});
// Button "No"
Button cancelBtn = new Button("No");
cancelBtn.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
primaryStage.show();
dialog.close();
}
});
// Layout for the Button
HBox hbox = new HBox();
hbox.setSpacing(10);
hbox.setAlignment(Pos.CENTER);
hbox.getChildren().add(okBtn);
hbox.getChildren().add(cancelBtn);
// Layout for the Label and hBox
VBox vbox = new VBox();
vbox.setAlignment(Pos.CENTER);
vbox.setSpacing(10);
vbox.getChildren().add(label);
vbox.getChildren().add(hbox);
// Stage
Scene scene = new Scene(vbox, 450, 150, Color.WHITESMOKE);
dialog.setScene(scene);
dialog.show();
}
});
}
}
I want to add image and to make it to look like this:
But I admin that it's too complex for my short knowledge to get the appropriate result. Can you show me how I can split the dialog, add second background and make my code to look the same as this example please?
Have a look at the ControlsFX project, they have some sophisticated dialogs and it's open source, so you can look up how it's done. For example, your dialog looks like this confirmation dialog of ControlsFX:
There is also support for custom dialogs.
€dit:
With the "show Masthead" option enabled it actually looks exactly like it:

Double click is not being caught by OnMouseEvent (JavaFX2)

Double clicks are being captured as single click :(
In fxml file:
<Button fx:id="A_button" onMouseClicked="#buttonAClicked">
In the controller
private void buttonAClicked(MouseEvent mouseEvent) {
if (mouseEvent.getButton().equals(MouseButton.PRIMARY)) {
if (mouseEvent.getClickCount() == 2) {
System.out.println("Double clicked A_button");
}
if (mouseEvent.getClickCount() == 1) {
System.out.println("Single clicked A_button");
}
}
}
Unfortunately I am finding that double click is not being caught - only single clicks.
In the debugger, click count is 1.
Update:
Since I cant figure out why it is not working for me on JavaFX 2.2.3-b05, I did a workaround and removed the need for double-click. I added a "Load" button to the UI. Now user must single click AND press the load button.
It was fixed in JavaFX 2.2, see http://javafx-jira.kenai.com/browse/RT-19346
Note, that on double click you will receive two events:
mouse click with getClickCount() == 1
mouse click with getClickCount() == 2
E.g. if you run code below and double click on button output would be:
clicks: 1
clicks: 2
Code (tested with 2.2.4):
public class DoubleClicks extends Application {
#Override public void start(Stage stage) {
Button btn = new Button();
btn.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
System.out.println("clicks: " + event.getClickCount());
}
});
stage.setScene(new Scene(new Group(btn), 300, 250));
stage.setTitle(VersionInfo.getRuntimeVersion());
stage.show();
}
public static void main(String[] args) { launch(); }
}

Popup window with table view in JavaFX 2.0

I want to click a button in order to make a popup window appear with a tableview element inside it. Can anyone tell me how to do it?
Thanks in advance.
This is the code for simple popup window in JavaFX.
Hope this helps.
public class PopupExample extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(final Stage primaryStage) {
primaryStage.setTitle("Popup Example");
final Popup popup = new Popup();
popup.setX(300);
popup.setY(200);
popup.getContent().addAll(new Circle(25, 25, 50, Color.AQUAMARINE));
Button show = new Button("Show");
show.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
popup.show(primaryStage);
}
});
Button hide = new Button("Hide");
hide.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
popup.hide();
}
});
HBox layout = new HBox(10);
layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10;");
layout.getChildren().addAll(show, hide);
primaryStage.setScene(new Scene(layout));
primaryStage.show();
}
}
What kind of popup window do you need? Implemented with using a new Stage or Popup control? JavaFX has a control named Popup, read about it to see does it meet your needs. An entry point for Stage version could be Dialog with CLOSE button.

Resources