import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * Main frame to enter address * * @author fbergeon */ public class AddressFrame extends JFrame implements ActionListener { // Static labels private static final String LBL_FIRSTNAME = "First name "; private static final String LBL_SECONDNAME = "Second name "; private static final String LBL_PHONENUMBER = "Phone number"; private static final String LBL_HOUSENUMBER = "House number"; private static final String LBL_STREET1NAME = "Street1 name "; private static final String LBL_STREET2NAME = "Street2 name "; private static final String LBL_CITYNAME = "City name "; // Content pane private JPanel jContentPane = null; // Fist name private JLabel lblFirstName = null; private JTextField txtFirstName = null; // Second name private JLabel lblSecondName = null; private JTextField txtSecondName = null; // Phone number private JLabel lblPhoneNumber = null; private JTextField txtPhoneNumber = null; // House number private JLabel lblHouseNumber = null; private JTextField txtHouseNumber = null; // Street1 name private JLabel lblStreet1Name = null; private JTextField txtStreet1Name = null; // Street2 name private JLabel lblStreet2Name = null; private JTextField txtStreet2Name = null; // City name private JLabel lblCityName = null; private JTextField txtCityName = null; // Show short and full address buttons private JButton btnShort = null; private JButton btnFull = null; /** * Default constructor */ public AddressFrame() { super(); initialize(); } /** * Initialize the JFrame * * @return void */ private void initialize() { // Set generic paramters this.setSize(360, 250); this.setTitle("Enter address"); this.setLayout(new FlowLayout()); // First name label and field lblFirstName = new JLabel(LBL_FIRSTNAME); txtFirstName = new JTextField(20); this.add(lblFirstName, null); this.add(txtFirstName, null); // Second name label and field lblSecondName = new JLabel(LBL_SECONDNAME); this.add(lblSecondName, null); txtSecondName = new JTextField(20); this.add(txtSecondName, null); // Phone number label and field lblPhoneNumber = new JLabel(LBL_PHONENUMBER); this.add(lblPhoneNumber, null); txtPhoneNumber = new JTextField(20); this.add(txtPhoneNumber, null); // House number label and field lblHouseNumber = new JLabel(LBL_HOUSENUMBER); this.add(lblHouseNumber, null); txtHouseNumber = new JTextField(20); this.add(txtHouseNumber, null); // Street1 name label and field lblStreet1Name = new JLabel(LBL_STREET1NAME); this.add(lblStreet1Name, null); txtStreet1Name = new JTextField(20); this.add(txtStreet1Name, null); // Street2 name label and field lblStreet2Name = new JLabel(LBL_STREET2NAME); this.add(lblStreet2Name, null); txtStreet2Name = new JTextField(20); this.add(txtStreet2Name, null); // City name label and field lblCityName = new JLabel(LBL_CITYNAME); this.add(lblCityName, null); txtCityName = new JTextField(20); this.add(txtCityName, null); // Show short address button btnShort = new JButton("Show short address"); btnShort.addActionListener(this); this.add(btnShort, null); // Show full address button btnFull = new JButton("Show full address"); btnFull.addActionListener(this); this.add(btnFull, null); } /** * Event handler to catch btnShort and btnFull clicks * * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) */ public void actionPerformed(java.awt.event.ActionEvent e) { // Show short address if (e.getSource() == btnShort) displayShortAddress(); // Show full address else if (e.getSource() == btnFull) displayFullAddress(); } /** * Display full address in new JFrame */ private void displayFullAddress() { // Obtain address FullAddress address = getAddress(); if (address != null) { // Create new JFrame JFrame frame = new JFrame("Full address"); frame.setSize(360, 220); frame.setLayout(new FlowLayout()); // Populate short address populateShortAddress(address, frame); // Populate full address addField(frame, LBL_HOUSENUMBER, new Integer(address.houseNumber).toString(), false); addField(frame, LBL_STREET1NAME, address.street1Name, false); addField(frame, LBL_STREET2NAME, address.street2Name, false); addField(frame, LBL_CITYNAME, address.cityName, false); // Display frame.setVisible(true); } } /** * Display short address in new JFrame */ private void displayShortAddress() { // Obtain address FullAddress address = getAddress(); if (address != null) { // Create new JFrame JFrame frame = new JFrame("Short address"); frame.setSize(360, 120); frame.setLayout(new FlowLayout()); // Populate short address populateShortAddress(address, frame); // Display frame.setVisible(true); } } /** * Populate JFrame with short address * * @param address ShortAddress to populate with * @param frame JFrame to populate */ private void populateShortAddress(ShortAddress address, JFrame frame) { addField(frame, LBL_FIRSTNAME, address.firstName, false); addField(frame, LBL_SECONDNAME, address.secondName, false); addField(frame, LBL_PHONENUMBER, address.phoneNumber, false); } /** * Build FullAddress object from input * * @return FullAddress object */ private FullAddress getAddress() { try { FullAddress address = new FullAddress( txtFirstName.getText(), txtSecondName.getText(), txtPhoneNumber.getText(), new Integer(txtHouseNumber.getText()), txtStreet1Name.getText(), txtStreet2Name.getText(), txtCityName.getText() ); return address; // Catch exception and display error } catch (Exception e) { JOptionPane.showMessageDialog(null, "Error " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); return null; } } /** * Add JLabel and JTextField to JFrame * * @param frame JFrame to populate * @param label Label string * @param value Initial field value * @param enabled Field enabled true/false */ private static void addField(JFrame frame, String label, String value, boolean enabled) { // New JLabel JLabel lbl = new JLabel(label); // New JTextField JTextField txt = new JTextField(20); txt.setText(value); txt.setEnabled(enabled); // Add label and field to JFrame frame.add(lbl); frame.add(txt); } }