how to get the textfield value when i choose a date in DatePicker

Juice

i made this a datepicker class which extends the JFXPanel so i could use the datepicker feature in my swing application. Everything was going well until i got stuck on how to get the date from the textfield into my textarea.. i tried using a String field n stored the LocalDate object assigned to it using the toString() method but it keeps coming back as null.

heres the code:

public class FNAFrame extends JFrame {

public FNAFrame()
{
    super ("FNA Comments Generator");
    setLayout(new BorderLayout());

    setResizable(false);
    TextFrame comps = new TextFrame();
    add(comps);
    pack();
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
                // 
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) 
            {
                ex.printStackTrace();
            }


            new FNAFrame();

        }
    });
 }  
} // end of class FNA Frame


public class TextFrame extends JPanel {
// variable declarations
private JLabel newbLabel;

private JButton noChange_Button;

private JTextArea display_Area;

// end of variable declarations

public TextFrame()
{
    super(new GridBagLayout());

    setPreferredSize(new Dimension(300,200));
    setBackground(Color.white);

    init();  
  } // end of class constructor

    private void init()
    { 
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10,10,10,10);

        // date picker
        DatePickin date_Picker = new DatePickin();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.anchor = GridBagConstraints.EAST;
        add(date_Picker, gbc);

        // button to display date in textarea
        noChange_Button = new JButton("No Change");
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.WEST;
        add(noChange_Button, gbc);

        ///////////////////// TEXT AREA ///////////////////////

        display_Area = new JTextArea();
        gbc.gridx = 0;
        gbc.gridy = 3;
        //gbc.weighty = 1;
        gbc.gridwidth = 3;
        gbc.gridheight = 4;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.SOUTHWEST;
        display_Area.setEditable(true);
        display_Area.setLineWrap(true);
        display_Area.setWrapStyleWord(true);

        JScrollPane scroll = new JScrollPane(display_Area);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scroll.setPreferredSize(new Dimension(0, 70));
        add(scroll, gbc);

        // adding listeners to components
        // registering all components with their respective listeners
        CompHandler compHandler = new CompHandler();
        noChange_Button.addActionListener(compHandler);
    }

    // class to handle text fields
    private class CompHandler implements ActionListener
    {
        DatePickin date = new DatePickin();

        private String newbDate;

        @Override
        public void actionPerformed(ActionEvent e)  
        {
            Object button_command = e.getActionCommand();

            try {

                if (button_command.equals("No Change"))
                {    
                    newbDate = date.strDate;

                    display_Area.setText("The date is " + newbDate);
                    display_Area.setFont(new Font("Serif", Font.BOLD, 18));
                    display_Area.setForeground(Color.black);
                }
            }
            catch (NullPointerException np)
            {
            }       
        }
       } // end component handler class 
} // end of TextFrame class


public class DatePickin extends javafx.embed.swing.JFXPanel{

private DatePicker date_Picker;
String strDate;
private VBox pane;

public DatePickin()
{
    setLayout(new FlowLayout());
    setPreferredSize(new Dimension(90, 30));

    init();   
} // end of class constructor

private void init()
{
    pane = new VBox();
    pane.setBackground(Background.EMPTY);
    pane.setAlignment(Pos.CENTER_LEFT);
    getDate();
    pane.getChildren().addAll(date_Picker);

    Platform.runLater(this::createScene);
}

public void getDate()
{
    date_Picker = new DatePicker();

    date_Picker.setShowWeekNumbers(false);

    date_Picker.setOnAction((e) -> {

        LocalDate ld;
        try
        {
            //This is where i the problem is
            ld = date_Picker.getValue();

            strDate = ld.toString();
        }
        catch(UnsupportedOperationException uoe)
        {
        }
    });
}

private void createScene()
{
    Scene scene = new Scene(pane);
    setScene(scene);
}    

}

MadProgrammer

There are number of problems, but your first is creating a new instance of DatePickin in your CompHandler

private class CompHandler implements ActionListener
{
    DatePickin date = new DatePickin();

This has nothing to do with the instance of DatePickin that is on the screen, so it will always be null.

Instead, you should pass a reference of DatePickin to CompHandler

private class CompHandler implements ActionListener {

    private DatePickin pickin;

    public CompHandler(DatePickin pickin) {
        this.pickin = pickin;
    }

Next, I wouldn't get the String representation of LocalDate, you should get a reference to the LocalDate and format as you need. You should also limit people from been able to access you class fields and favor getters instead

public class DatePickin extends javafx.embed.swing.JFXPanel {

    private DatePicker date_Picker;
    private LocalDate dateValue;
    //...

    public void getDate() {
        date_Picker = new DatePicker();
        date_Picker.setShowWeekNumbers(false);
        date_Picker.setOnAction((e) -> {

            try {
                dateValue = date_Picker.getValue();
            } catch (UnsupportedOperationException uoe) {
                uoe.printStackTrace();
            }
        });
    }

    public LocalDate getDateValue() {
        return dateValue;
    }

Then, when the No Change button is clicked, you can just grab the value of LocalDate and do with it as you please...

if (button_command.equals("No Change")) {
    LocalDate newbDate = pickin.getDateValue();

    display_Area.setText("The date is " + newbDate);
    display_Area.setFont(new Font("Serif", Font.BOLD, 18));
    display_Area.setForeground(Color.black);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get the value from datepicker in textfield

From Dev

How to get the value from datepicker in textfield

From Dev

How to get date "value" from DatePicker?

From Dev

How to get date "value" from DatePicker?

From Dev

How do i get a value of textField in an UIAlertController?

From Dev

How do i get a value of textField in an UIAlertController?

From Dev

Able to choose future date when setting max date in datepicker

From Dev

How to Get Date from DatePicker When Button Clicked

From Java

How to get the TextField value in flutter

From Dev

How to get value from DatePicker

From Dev

How to get value from datepicker?

From Dev

mark a checkbox when i choose a value in a select

From Dev

Destroy method on jQuery DatePicker removes DatePicker but when I re-instantiate a new one, the previous selected Date value is still selected

From Dev

How do I store a date value from DatePicker into a formatted String value?

From Dev

How to set the date value of ui bootstrap datepicker

From Dev

how to get date from database to html datepicker

From Dev

How to get the selected date from jquery datepicker

From Dev

How to get complete(include hour,minute and seconds) date format of datepicker value in javascript

From Dev

In Excel 2010, how can I subtract two times when I also have the date in the cell and get the value in seconds?

From Dev

bootstrap datepicker invalid date issue when i click date twice?

From Dev

How do I set Min Date in Datepicker from another Datepicker?

From Dev

How do I clear/reset date field when using jquery datepicker

From Dev

How do I clear/reset date field when using jquery datepicker

From Dev

Xamarin Forms, how do I get the entered date from a datepicker to a format of your choice?

From Dev

in zebra datepicker how can i get or extract week number from selected date?

From Dev

How can I get and send the selected date from my datepicker to the controller?

From Dev

how to get value on TextField Java Swing

From Dev

How to get each checkbox textfield value

From Dev

How to get value out of bound textfield in tableview?

Related Related

  1. 1

    How to get the value from datepicker in textfield

  2. 2

    How to get the value from datepicker in textfield

  3. 3

    How to get date "value" from DatePicker?

  4. 4

    How to get date "value" from DatePicker?

  5. 5

    How do i get a value of textField in an UIAlertController?

  6. 6

    How do i get a value of textField in an UIAlertController?

  7. 7

    Able to choose future date when setting max date in datepicker

  8. 8

    How to Get Date from DatePicker When Button Clicked

  9. 9

    How to get the TextField value in flutter

  10. 10

    How to get value from DatePicker

  11. 11

    How to get value from datepicker?

  12. 12

    mark a checkbox when i choose a value in a select

  13. 13

    Destroy method on jQuery DatePicker removes DatePicker but when I re-instantiate a new one, the previous selected Date value is still selected

  14. 14

    How do I store a date value from DatePicker into a formatted String value?

  15. 15

    How to set the date value of ui bootstrap datepicker

  16. 16

    how to get date from database to html datepicker

  17. 17

    How to get the selected date from jquery datepicker

  18. 18

    How to get complete(include hour,minute and seconds) date format of datepicker value in javascript

  19. 19

    In Excel 2010, how can I subtract two times when I also have the date in the cell and get the value in seconds?

  20. 20

    bootstrap datepicker invalid date issue when i click date twice?

  21. 21

    How do I set Min Date in Datepicker from another Datepicker?

  22. 22

    How do I clear/reset date field when using jquery datepicker

  23. 23

    How do I clear/reset date field when using jquery datepicker

  24. 24

    Xamarin Forms, how do I get the entered date from a datepicker to a format of your choice?

  25. 25

    in zebra datepicker how can i get or extract week number from selected date?

  26. 26

    How can I get and send the selected date from my datepicker to the controller?

  27. 27

    how to get value on TextField Java Swing

  28. 28

    How to get each checkbox textfield value

  29. 29

    How to get value out of bound textfield in tableview?

HotTag

Archive