Frage

I want to get the value of my JFormattedTextField and convert it to string here is my code for the JFormattedTextField

Here's my code:

public void formattedTextField()
    {

            timeAndDate = new JFormattedTextField(new SimpleDateFormat("MM/dd/yy - HH:mm"));
            timeAndDate.setValue(new Date());
            timeAndDate.setBounds(140,384,145,31);
            add(timeAndDate);
            try{
            contactNo = new JFormattedTextField(new MaskFormatter("(+63)9 ##########"));
            contactNo.setBounds(140,216,145,31);
            add(contactNo);
            }
            catch(Exception e)
            {
                JOptionPane.showMessageDialog(null,e,"",JOptionPane.ERROR_MESSAGE);
            }
    }

I would like to get the contactNo as string too. But I don't know how and what will I put on my actionlistener. When I click the button, the data is to be stored in the database. I use MySQL database heres my current code of actionListener:

public class ButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource()==submitB)
            {
                //validate text
                validation();
                if(validation()){
;
                    // combo box gender
                    String genderText =(String)gender.getSelectedItem();
                    // get the button model selected from the button group
                    ButtonModel selectedModel = group.getSelection();
                    //insert data to database
                    System.out.print("Inserting");
                    GuestsInfo guestInfo = new GuestsInfo(firstName.getText(),lastName.getText(),age.getText(),
                            genderText,address.getText(),"123444","10:00",stay.getText(),"10.00");
                    System.out.print("Successful");
                }
                else{       
                    JOptionPane.showMessageDialog(null,"Invalid input","",JOptionPane.ERROR_MESSAGE);
                }

i entered "123444" as temporary for contact no. and "10:00" for time and date since i don't know yet how to get the value from JFormattedtextField, and also "10.00" because i need to to compute the balance of the user. I want to know how can i get the value of JFormattedTextField in string so i can put it on the MySQL. Please help me out. Thanks in advance.

War es hilfreich?

Lösung

String date = new SimpleDateFormat("MM/dd/yy  HH:mm").format(new Date());

timeAndDate = new JFormattedTextField(date);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top