Question
Doing JFrame and make comments
16; 17, 18 (Marks: 50) Develop a Java Gur application that will produce an Area lookup application that will allow a user to view a phone or postal code for a Town in a particular Country. Do not forget to use allocated the marking guideline at the end of this question to see how the marks are create two combo boxes, one for the country selection and another to select a town. Also, add two radio buttons for the user to select to display either a phone o postal code. Q.2.2 You are also required to create a submit and exit button. The layout of the form is left to your discretion. Marks will be allocated to the presentation and effectiveness of the layout, but the following layout is displayed for your convenience. Country Lookup AREA CODE LOOKUP Select Country. South Africa Select Town Cape Town Item to lookup: O Phone Code O Postal Code SUBMIT EXIT
media%2F963%2F963d7e29-9555-4003-a4bd-78
0 0
Add a comment Improve this question Transcribed image text
Answer #1

AreaCodeLookup.java
import java.util.HashMap;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Admin
*/
public class AreaCodeLookup extends javax.swing.JFrame {

/**
* Creates new form AreaCodeLookup
*/
boolean phone_code,postal_code;
HashMap<String, HashMap<String,CountryItem>> hashMap=new HashMap<>();//hashmap to store country,country town and town phone code and postal code
String str_country[]={"South Africa","United Kingdom"};
String str_town[]={"Cape Town","Johannesberg","Durban","London","Oxford","Southampton"};
String country_selected=str_country[0],town_selected=str_town[0];
  
public AreaCodeLookup() {
HashMap<String,CountryItem> town=new HashMap<>();//hashmap to store town and its ph code and postal code
town.put(str_town[0],new CountryItem("021", "8000"));
town.put(str_town[1],new CountryItem("011", "2000"));
town.put(str_town[2],new CountryItem("031", "4000"));
  
hashMap.put(str_country[0],town );//link town data to country
town.put(str_town[3],new CountryItem("020", "WC2N5DU"));//add town data for second country
town.put(str_town[4],new CountryItem("01865", "OX13PN"));
town.put(str_town[5],new CountryItem("023", "SO147DW"));
  
hashMap.put(str_country[1],town );//link second country with town data
initComponents();//initialize all ui components
country.setModel(new javax.swing.DefaultComboBoxModel<>(str_country));//set country in country jcombobox
//set first country town in town jcombobox
this.town.setModel(new javax.swing.DefaultComboBoxModel<>(new String[]{str_town[0],str_town[1],str_town[2]}));
//Group the radio buttons. so that only one at a time will be selected
ButtonGroup group = new ButtonGroup();
  
group.add(phone_code_radio);
group.add(postal_code_radio);
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jSpinner1 = new javax.swing.JSpinner();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
phone_code_radio = new javax.swing.JRadioButton();
postal_code_radio = new javax.swing.JRadioButton();
submit = new javax.swing.JButton();
exit = new javax.swing.JButton();
country = new javax.swing.JComboBox<>();
town = new javax.swing.JComboBox<>();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("AREA CODE LOOKUP");
jLabel1.setToolTipText("");

jLabel2.setText("Select Country:");

jLabel3.setText("Select Town:");

jLabel4.setText("Item to lookup:");

phone_code_radio.setText("Phone Code");
phone_code_radio.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
phone_code_radioItemStateChanged(evt);
}
});

postal_code_radio.setText("Postal Code");
postal_code_radio.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
postal_code_radioItemStateChanged(evt);
}
});

submit.setText("SUBMIT");
submit.setName(""); // NOI18N
submit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
submitActionPerformed(evt);
}
});

exit.setText("EXIT");
exit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitActionPerformed(evt);
}
});

country.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
country.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
countryItemStateChanged(evt);
}
});

town.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
town.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
townItemStateChanged(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(54, 54, 54)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(82, 82, 82)
.addComponent(submit)
.addGap(55, 55, 55)
.addComponent(exit))
.addGroup(layout.createSequentialGroup()
.addGap(19, 19, 19)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jLabel2)
.addComponent(jLabel4))
.addGap(35, 35, 35)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(postal_code_radio)
.addComponent(phone_code_radio)
.addComponent(country, 0, 97, Short.MAX_VALUE)
.addComponent(town, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addContainerGap(103, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(country, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(30, 30, 30)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(town, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(30, 30, 30)
.addComponent(jLabel4)
.addGap(18, 18, 18)
.addComponent(phone_code_radio)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(postal_code_radio)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(submit)
.addComponent(exit))
.addContainerGap(12, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void submitActionPerformed(java.awt.event.ActionEvent evt) {   
// TODO add your handling code here:
if (evt.getSource() == submit) {
if(phone_code)
//display popupbox for phone code
JOptionPane.showMessageDialog(null, "Phone code for "+town_selected+" in "+country_selected+" is "+hashMap.get(country_selected).get(town_selected).phone_code);
else if(postal_code)

//display popupbox for postal code
JOptionPane.showMessageDialog(null, "Postal code for "+town_selected+" in "+country_selected+" is "+hashMap.get(country_selected).get(town_selected).postal_code);

}
}

private void exitActionPerformed(java.awt.event.ActionEvent evt) {   
// TODO add your handling code here:
dispose();//close application
}

private void countryItemStateChanged(java.awt.event.ItemEvent evt) {   
// TODO add your handling code here:
if (evt.getSource() == country) {
country_selected=(String) country.getSelectedItem();

if(country_selected.equals(str_country[0])){//if south arica selected
country_selected=str_country[0];
town_selected=str_town[0];
  
//set town data of SA country in jcombobox
  
this.town.setModel(new javax.swing.DefaultComboBoxModel<>(new String[]{str_town[0],str_town[1],str_town[2]}));
}else if(country_selected.equals(str_country[1])){//if UK selected
country_selected=str_country[1];
town_selected=str_town[3];
  
//set town data of UK country in jcombobox
this.town.setModel(new javax.swing.DefaultComboBoxModel<>(new String[]{str_town[3],str_town[4],str_town[5]}));
  
}
}

}

private void townItemStateChanged(java.awt.event.ItemEvent evt) {
// TODO add your handling code here:
if (evt.getSource() == town) {
town_selected=(String) town.getSelectedItem();
  
}
}   

private void phone_code_radioItemStateChanged(java.awt.event.ItemEvent evt) {
// TODO add your handling code here:
if (evt.getSource() == phone_code_radio) { //if phone radio button selected
phone_code=true;
postal_code=false;
}
}   

private void postal_code_radioItemStateChanged(java.awt.event.ItemEvent evt) {   
// TODO add your handling code here:
if (evt.getSource() == postal_code_radio) { //if postal radio button selected
phone_code=false;
postal_code=true;
}
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(AreaCodeLookup.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(AreaCodeLookup.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(AreaCodeLookup.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(AreaCodeLookup.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
AreaCodeLookup areaCodeLookup=new AreaCodeLookup();
areaCodeLookup.setTitle("Country lookup");
areaCodeLookup.setVisible(true);
}
});
}

// Variables declaration - do not modify   
private javax.swing.JComboBox<String> country;
private javax.swing.JButton exit;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JSpinner jSpinner1;
private javax.swing.JRadioButton phone_code_radio;
private javax.swing.JRadioButton postal_code_radio;
private javax.swing.JButton submit;
private javax.swing.JComboBox<String> town;
// End of variables declaration   

}

CountryItem.java


import java.util.ArrayList;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Admin
*/
public class CountryItem {
String phone_code,postal_code;

public CountryItem(String phone_code, String postal_code) {
this.phone_code = phone_code;
this.postal_code = postal_code;
}

  
}

Output

Country lookup AREA CODE LOOKUP Select Country: South Africa Select Town: Cape Town O Phone Code O Postal Code | SUBMIT | | EXIT |

Project structure

Design

Add a comment
Know the answer?
Add Answer to:
Doing JFrame and make comments 16; 17, 18 (Marks: 50) Develop a Java Gur application that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question 1 (Marks: 50 Develop a Java GUI application that will produce an investment report based...

    Question 1 (Marks: 50 Develop a Java GUI application that will produce an investment report based on various criteria, such as investment amount, investment type and term. On the form create two text fields, one to capture the customer name and (10) Q.1.1 another to capture the amount to invest. Also create a combo box for the user to select the investment type which will be moderate or aggressive. Finally add three radio buttons for the user to select the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT