So if someone is born in 1980 and they want to enter their age through mouse they would have to click on the year arrow 37 times, which is pretty ludicrous.
Is there a way to view the decades in the javafx datepicker so that the user can just choose and option like 1980-1990 and pick a date?
Edit: since no ones answered this yet i'll just post an image for clarity
https://ibb.co/exrVgQ
You could do something like this
public class JavaFxTest extends Application{
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
Button prevDecade=new Button("Previous Decade");
Button nextDecade=new Button("Next Decade");
DatePicker datePicker=new DatePicker();
Calendar calendar = Calendar.getInstance();
datePicker.setValue(LocalDate.of(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)));
prevDecade.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent arg0) {
LocalDate value = datePicker.getValue();
datePicker.setValue(LocalDate.of(value.getYear()-10, value.getMonth(), value.getDayOfMonth()));
datePicker.show();
}
});
nextDecade.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent arg0) {
datePicker.show();
LocalDate value = datePicker.getValue();
datePicker.setValue(LocalDate.of(value.getYear()+10, value.getMonth(), value.getDayOfMonth()));
}
});
HBox hBox=new HBox();
hBox.getChildren().addAll(datePicker, prevDecade, nextDecade);
primaryStage.setScene(new Scene(hBox,500,300));
primaryStage.show();
}
}
But the only problem is that datepicker hides the calendar when the buttons are clicked, so we have to explicitly do datepicker.show(), may be theres a way to fix this, or a better way to approach your problem, this is all I could think of :)
Related
Hell o,
I would like to know if it's preferrable to inflate a fragment extending DialogFragment class or to display the dialog from a simple method added in onCreateView() ?
Here's a snippet for the second solution from internet (based on an activity and not fragment) :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
positiveDialog = findViewById(R.id.btnPositiveDialog);
negativeDialog = findViewById(R.id.btnNegativeDialog);
positiveDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showAlertDialog(R.layout.dialog_postive_layout);
}
});
negativeDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showAlertDialog(R.layout.dialog_negative_layout);
}
});
}
private void showAlertDialog(int layout){
dialogBuilder = new AlertDialog.Builder(MainActivity.this);
View layoutView = getLayoutInflater().inflate(layout, null);
Button dialogButton = layoutView.findViewById(R.id.btnDialog);
dialogBuilder.setView(layoutView);
alertDialog = dialogBuilder.create();
alertDialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alertDialog.show();
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
alertDialog.dismiss();
}
});
}
}
My only concern is that i use a fragment among navigationDrawer where i want to display my alertDialog (1 editText and 2 buttons), and i would like to prevent user from closing the dialog via backbutton or clicking outside the dialog.
Thank you very much for helping me out
I m trying to print text and some lines that I put into a Group.
Depending on user action possible that it must be on multiple pages.
I tried many things all my Google links to this topic are marked as visited allready...
My problem is the thing with multiple pages .. i would like to do it without any scaling ..
Its also possible that a Group is not the right thing for it ,
but donĀ“t know what would be better.
I hope somebody can help me ..
I added a short example of one way I tried it only prints one page...
public class Main extends Application {
private static Stage primaryStage; // **Declare static Stage**
#Override
public void start(Stage primaryStage) throws Exception{
setPrimaryStage(primaryStage);
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("SK");
primaryStage.setScene(new Scene(root, 1200, 800));
primaryStage.show();
drawStuff();
}
static public Stage getPrimaryStage() {
return Main.primaryStage;
}
private void setPrimaryStage(Stage stage) {
Main.primaryStage = stage;
}
public static void main(String[] args) {
launch(args);
}
Group[] root=new Group[10];
public void drawStuff(){
for(int i=0;i<=1;i++) {
root[i]=new Group();
final Text logo1 = new Text(60, 20, "TEST");
logo1.setFill(Color.BLACK);
logo1.setFont(Font.font(java.awt.Font.SANS_SERIF, FontWeight.BOLD, FontPosture.ITALIC, 18));
root[i].getChildren().add(logo1);
}
print();
}
private void print() {
PrinterJob job = PrinterJob.createPrinterJob();
for(int i=0;i<=1;i++) {
boolean success = job.printPage(root[i]);
if (success) {
job.endJob();
}
}
}
}
I'm making chat application using JAVAFX. The messages displayed in textArea, but the textArea always in the same size. How can I make the textArea to fit exactly to the amount of text? TNX
The following code does exactly what You wants
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
AnchorPane root = new AnchorPane();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
TextArea ta=new TextArea();
ta.addEventHandler(KeyEvent.KEY_RELEASED, new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
// your algorithm to change height
ta.setPrefHeight(ta.getPrefHeight()+10);
}
});
root.getChildren().add(ta);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
I'm having trouble with the JavaFX Preloader. During the start phase the application will have to connect to a DB and read many so I thought it would be nice to display a splash screen during this time. The problem is the ProgressBar automaticly goes to 100% and I don't understand why.
Application class. Thread sleep will be replaced by real code later (DB connection etc)
public void init() throws InterruptedException
{
notifyPreloader(new Preloader.ProgressNotification(0.0));
Thread.sleep(5000);
notifyPreloader(new Preloader.ProgressNotification(0.1));
Thread.sleep(5000);
notifyPreloader(new Preloader.ProgressNotification(0.2));
}
Preloader
public class PreloaderDemo extends Preloader {
ProgressBar bar;
Stage stage;
private Scene createPreloaderScene() {
bar = new ProgressBar();
bar.getProgress();
BorderPane p = new BorderPane();
p.setCenter(bar);
return new Scene(p, 300, 150);
}
#Override
public void start(Stage stage) throws Exception {
this.stage = stage;
stage.setScene(createPreloaderScene());
stage.show();
}
#Override
public void handleStateChangeNotification(StateChangeNotification scn) {
if (scn.getType() == StateChangeNotification.Type.BEFORE_START) {
stage.hide();
}
}
#Override
public void handleProgressNotification(ProgressNotification pn) {
bar.setProgress(pn.getProgress());
System.out.println("Progress " + bar.getProgress());
}
For some reason I get the following output:
Progress 0.0
Progress 1.0
I had same problem and I found solution after two hours of searching and 5 minutes of carefully reading of JavaDoc.:)
Notifications send by notifyPreloader() method can be handled only by Preloader.handleApplicationNotification() method and it doesn't matter which type of notification are you sending.
So change you code like this:
public class PreloaderDemo extends Preloader {
.... everything like it was and add this ...
#Override
public void handleApplicationNotification(PreloaderNotification arg0) {
if (arg0 instanceof ProgressNotification) {
ProgressNotification pn= (ProgressNotification) arg0;
bar.setProgress(pn.getProgress());
System.out.println("Progress " + bar.getProgress());
}
}
}
i tried
public void ChangeFocus(Button browse, final FlowPane mFlowPane)
{
browse.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event)
{
if (event.getCode() == KeyCode.TAB)
{
System.out.println("TAB pressed");
mFlowPane.requestFocus();
event.consume(); // do nothing
}
}
});
}
in above code i set one ImageView inside Flowpane but when i press TAB button on my browse button i can't get focus on imageview how can i solve it?
FlowPane is not focustraversable by default. Call mFlowPane.setFocusTraversable(true) to make it part of the traverse story
some more detail to solve it. instead of focusing the flowpane you need to set the focus on whats in the flowpane, for example a textfield:
flowPane.setOnMouseClicked(new EventHandler<MouseEvent>()
{
public void handle(MouseEvent mouseEvent)
{
textfield.requestFocus();
}
});