package dk.itu.oop.lecture8; import javax.swing.*; import java.awt.*; import java.awt.event.*; class DateDialog extends JDialog { JTextField yearField; JTextField monthField; JTextField dayField; JTextArea messages; JButton accept; JButton cancel; Date date; public DateDialog(){ setModal(true); setSize(250,150); setTitle("Edit a Date"); Container pane = this.getContentPane(); pane.setLayout(new BorderLayout() ); JPanel inputPanel = new JPanel(); inputPanel.setLayout(new FlowLayout() ); JPanel acceptPanel = new JPanel(); acceptPanel.setLayout(new FlowLayout() ); yearField = new JTextField(5); monthField = new JTextField(3); dayField = new JTextField(3); inputPanel.add(dayField); inputPanel.add( new JLabel("/")); inputPanel.add(monthField); inputPanel.add( new JLabel("/")); inputPanel.add(yearField); accept = new JButton("accept"); cancel = new JButton("cancel"); acceptPanel.add(accept); acceptPanel.add(cancel); messages = new JTextArea(30,4); pane.add(inputPanel, "North"); pane.add(messages,"Center"); pane.add(acceptPanel,"South"); accept.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ storeDate(); } }); cancel.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ date = null; DateDialog.this.hide(); } }); } private void storeDate1(){ int y,m,d; try{ y = Integer.parseInt(yearField.getText()); m = Integer.parseInt(monthField.getText()); d = Integer.parseInt(dayField.getText()); date.setDate(y,m,d); this.hide(); }catch(Throwable uups){ messages.setText("Wrong "+uups); } } private void storeDate(){ int y,m,d; try{ try{y = Integer.parseInt(yearField.getText());} catch(NumberFormatException nfe){ messages.setText("Year is not a number"); return; } try{m = Integer.parseInt(monthField.getText());} catch(NumberFormatException nfe){ messages.setText("month is not a number"); return; } try{d = Integer.parseInt(dayField.getText());} catch(NumberFormatException nfe){ messages.setText("day is not a number"); return; } date.setDate(y,m,d); this.hide(); }catch(Date.DateException e){ if ( e.field.equals("month") ) messages.setText("Wrong month number"); else messages.setText("Wrong day number"); }catch(Throwable uups){ messages.setText("Something is not right!?"); } } private static DateDialog editor = new DateDialog(); public static Date editDate(Date d){ editor.date = d; editor.yearField.setText(String.valueOf(d.getYear())); editor.monthField.setText(String.valueOf(d.getMonth())); editor.dayField.setText(String.valueOf(d.getDay())); editor.show(); return editor.date; } public static void main(String args[]) throws Date.DateException { System.out.println("The date is: " + editDate(new MyDate(2004,6,15)) ); } }