0

I'm trying to get value from JDateChooser and use it as a filename I've created a file with a path, I can write on it, but the only problem is I can't change its name to the variable(data from JDateChooser)

Here is the part of the code:

JButton btnSave = new JButton("Save"); 
    btnSave.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String text = textField.getText();
            JDateChooser day = new JDateChooser();
            try{
                    File remindFile = new File("\\path", day + ".txt");
                    remindFile.createNewFile();
                    BufferedWriter writer = new BufferedWriter(new FileWriter(remindFile));
                    writer.write(text);
                    writer.close();
                    }
                    catch(Exception k)
                    { System.out.println("Oops");}

            textField.setText(null);

        }
    });
    btnSave.setFont(new Font("Tahoma", Font.PLAIN, 13));
    btnSave.setBounds(401, 215, 108, 30);
    panel.add(btnSave);

In the result created file gets name as:

com.toedter.calendar.JDateChooser
[JDateChooser,0,0,0x0,invalid,layout=java.awt.BorderLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=]

How can I fix it?

JNewb
  • 1
  • 2
  • First you need to get the **date** from the date chooser. Then you need to figure out how to write that date in characters that are compatible with a file name on the particular OS being used (e.g. `31/12/2017` would be Windows incompatible because the `/` is used to denote a separation between directories). – Andrew Thompson Mar 02 '17 at 10:46
  • 1) `btnSave.setBounds(401, 215, 108, 30);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) `catch(Exception k) { System.out.println("Oops");}` should be `catch(Exception k) { System.out.println(k.printStackTrace();}` for **useful** information. 3) .. – Andrew Thompson Mar 02 '17 at 10:51
  • .. 3) `new Font("Tahoma", Font.PLAIN, 13)` Don't presume particular fonts are installed. Either use generic fonts (e.g. `Font.SERIF`) or iterate the list of fonts and select one. 4) `new File("C:\\Users\\student_ib\\eclipse\\..` This is dangerous, given the directory might not exist on the user machine. Safer to offer a `JFileChooser` to allow the user to select a directory. Set the string as the default directory if you like. If it does not exist, the file chooser will ignore it and direct the user to the documents directory on their OS / machine. – Andrew Thompson Mar 02 '17 at 10:55

4 Answers4

2

You are actually appending the JDateChooser object itself, instead of a String representation of its selected Date :

File remindFile = new File("C:\\Users\\student_ib\\eclipse\\d", day + ".txt");

Try :

Date chosenDate = day.getDate();
DateFormat dateFormat = new SimpleDateFormat("yyMMdd");

File remindFile = new File("C:\\Users\\student_ib\\eclipse\\d", dateFormat.parse(chosenDate) + ".txt");
Arnaud
  • 17,229
  • 3
  • 31
  • 44
  • getDate() method is undefined for the type JDateChooser. But method getDay() is not worling properly – JNewb Mar 03 '17 at 05:08
  • 1
    From the javadoc, there is a `getDate` method, check your version of JDateChooser : http://javadox.com/com.toedter/jcalendar/1.4/doc/api/com/toedter/calendar/JDateChooser.html – Arnaud Mar 03 '17 at 06:45
0

You have to call day.getDate().getDay() to get the date from the JDateChooser not to use day itself.

Jens
  • 67,715
  • 15
  • 98
  • 113
0

Check the documentation for JDateChooser.

You will need to first get the date value via either .getDate() or .getCalendar(), then you should use something like SimpleDateFormat to format the date.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Haroldo_OK
  • 6,612
  • 3
  • 43
  • 80
0

Thank you all for your answers. Using your advices I solved it in this way:

            int day = calendar_1.getDayChooser().getDay();
            int month = calendar_1.getMonthChooser().getMonth();
            int year = calendar_1.getYearChooser().getYear();
            String name = "" + day + month + year;
            File remindFile = new File(name + ".txt");
JNewb
  • 1
  • 2