473,417 Members | 1,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,417 software developers and data experts.

why is my program printing the same thing twice

i have a problem with the jcombobox i made
im guessing the problems are at getSalutation() and getSuffix() but im not completely sure

i want it to print to console once when the user clicks on the option but for some reason it is printing out twice

also how do i use requestFocus() with jcombobox?

please help me out!!!
thank you!!!

Expand|Select|Wrap|Line Numbers
  1. // 221 Fall 2010 Homework 1
  2. // A simple Graphical User Interface (GUI).
  3. // Team 4: Yao Jiang, Guang Jian Li, Davian Gordon
  4.  
  5. package hw1;
  6.  
  7. import javax.swing.JFrame;
  8. import javax.swing.JLabel;
  9. import javax.swing.*;
  10. import java.awt.*;
  11. import java.awt.event.*;
  12.  
  13. public class HW1Fall2010StudentSolution extends JFrame {
  14.     // Including this line avoids an annoying warning message.
  15.     private static final long serialVersionUID = 1L;
  16.  
  17.     private String firstNameAsEntered = new String();
  18.     private JTextField firstNameEntry = null;
  19.  
  20.     private String middleInitialAsEntered = new String();
  21.     private JTextField middleInitialEntry = null;
  22.  
  23.     private String lastNameAsEntered = new String();
  24.     private JTextField lastNameEntry = null;
  25.  
  26.     private String partNumberAsEntered = new String();
  27.     private JTextField partNumberEntry = null;
  28.  
  29.     private String salutationAsEntered = new String();
  30.  
  31.     private String suffixAsEntered = new String();
  32.  
  33.     private String quantityAsEntered = new String();
  34.     private JTextField quantityEntry = null;
  35.  
  36.     private String priceAsEntered = new String();
  37.     private JTextField priceEntry = null;
  38.  
  39.     private javax.swing.JPanel jContentPane = null;
  40.  
  41.     public static void main(String[] args) {
  42.         HW1Fall2010StudentSolution frame = new HW1Fall2010StudentSolution();
  43.         frame.setVisible(true);
  44.     } // end main
  45.  
  46.     // constructor
  47.     public HW1Fall2010StudentSolution() {
  48.         // Invoke constructor of superclass.
  49.         super();
  50.         initialize();
  51.     } // end HW1StarterKit constructor
  52.  
  53.     // "this" is always a reference to the object currently executing.
  54.     // frame, in this case.
  55.     private void initialize() {
  56.         this.setSize(350, 250);
  57.         this.setContentPane(getJContentPane());
  58.         this.setTitle("Customer Data Entry");
  59.     }
  60.  
  61.     private JPanel getJContentPane() {
  62.         jContentPane = new JPanel();
  63.         // Flow layout is one of a number of layout managers.
  64.         // We'll study layout in more detail later.
  65.         // Flow layout: left to right until line full, then move to next line.
  66.         jContentPane.setLayout(new java.awt.FlowLayout());
  67.         // We now have a "pane" to hold our components.
  68.         // We add them in the order we want them to appear: left to right,
  69.         // and top to bottom. (Experiment until you understand.)
  70.         jContentPane.add(makeLabel(" First Name *", 100, 20));
  71.         jContentPane.add(getFirstNameEntry(), null);
  72.  
  73.         jContentPane.add(makeLabel(" Middle Initial", 100, 20));
  74.         jContentPane.add(getMiddleInitialEntry(), null);
  75.  
  76.         jContentPane.add(makeLabel(" Last Name *", 100, 20));
  77.         jContentPane.add(getLastNameEntry(), null);
  78.  
  79.         jContentPane.add(makeLabel(" Part Number *", 100, 20));
  80.         jContentPane.add(getPartNumberEntry(), null);
  81.  
  82.         jContentPane.add(makeLabel(" Salutation *", 100, 20));
  83.         jContentPane.add(getSalutation(), null);
  84.  
  85.         jContentPane.add(makeLabel(" Suffix *", 100, 20));
  86.         jContentPane.add(getSuffix(), null);
  87.  
  88.         jContentPane.add(makeLabel(" Quantity *", 100, 20));
  89.         jContentPane.add(getQuantity(), null);
  90.  
  91.         jContentPane.add(makeLabel(" Unit Price *", 100, 20));
  92.         jContentPane.add(getPrice(), null);
  93.  
  94.         return jContentPane;
  95.     }
  96.  
  97.     // A small "factory" to build labels
  98.     private JLabel makeLabel(String labelName, int width, int height) {
  99.         JLabel label = new JLabel();
  100.         label.setText(labelName);
  101.         label.setPreferredSize(new Dimension(width, height));
  102.         return label;
  103.     }
  104.  
  105.     private JTextField getFirstNameEntry() {
  106.         firstNameEntry = new JTextField();
  107.         firstNameEntry.setPreferredSize(new Dimension(200, 20));
  108.         firstNameEntry.addActionListener(
  109.         // The "new" here, as always, means that a class is being
  110.         // instantiated. But it has no name: it's _anonymous_
  111.                 new ActionListener() {
  112.                     public void actionPerformed(ActionEvent e) {
  113.                         firstNameAsEntered = firstNameEntry.getText().trim();
  114.                         firstNameEntry.setText(firstNameAsEntered);
  115.                         System.out.println(firstNameAsEntered);
  116.                         if (firstNameAsEntered.length() == 0)
  117.                             System.out.println("First name is required");
  118.                         middleInitialEntry.requestFocus();
  119.                     }
  120.                 });
  121.  
  122.         return firstNameEntry;
  123.     } // end private JTextField getFirstNameEntry()
  124.  
  125.     private JTextField getMiddleInitialEntry() {
  126.         middleInitialEntry = new JTextField();
  127.         middleInitialEntry.setPreferredSize(new Dimension(200, 20));
  128.         middleInitialEntry.addActionListener(new ActionListener() {
  129.             public void actionPerformed(ActionEvent e) {
  130.                 middleInitialAsEntered = middleInitialEntry.getText().trim();
  131.                 middleInitialEntry.setText(middleInitialAsEntered);
  132.                 if (middleInitialAsEntered.length() != 0)
  133.                     middleInitialAsEntered = middleInitialAsEntered.substring(
  134.                             0, 1) + '.';
  135.                 middleInitialAsEntered = middleInitialAsEntered.toUpperCase();
  136.                 middleInitialEntry.setText(middleInitialAsEntered);
  137.                 System.out.println(middleInitialAsEntered);
  138.                 lastNameEntry.requestFocus();
  139.             }
  140.         });
  141.         return middleInitialEntry;
  142.     } // end private JTextField getMiddleInitialEntry()
  143.  
  144.     private JTextField getLastNameEntry() {
  145.         lastNameEntry = new JTextField();
  146.         lastNameEntry.setPreferredSize(new Dimension(200, 20));
  147.         lastNameEntry.addActionListener(new ActionListener() {
  148.             public void actionPerformed(ActionEvent e) {
  149.                 lastNameAsEntered = lastNameEntry.getText().trim();
  150.                 // This deletes leading and trailing blanks in entry field, too
  151.                 lastNameEntry.setText(lastNameAsEntered);
  152.                 System.out.println(lastNameAsEntered);
  153.                 if (lastNameAsEntered.length() == 0)
  154.                     System.out.println("Last name is required");
  155.                 partNumberEntry.requestFocus();
  156.             }
  157.         });
  158.         return lastNameEntry;
  159.     } // end private JTextField getLastNameEntry()
  160.  
  161.     private JTextField getPartNumberEntry() {
  162.         partNumberEntry = new JTextField();
  163.         partNumberEntry.setPreferredSize(new Dimension(200, 20));
  164.         partNumberEntry.addActionListener(new ActionListener() {
  165.             public void actionPerformed(ActionEvent e) {
  166.                 partNumberAsEntered = partNumberEntry.getText().trim();
  167.                 // This deletes leading and trailing blanks in entry field, too
  168.                 partNumberEntry.setText(partNumberAsEntered);
  169.                 System.out.println(partNumberAsEntered);
  170.                 if (partNumberAsEntered.length() == 0)
  171.                     System.out.println("Part number is required");
  172.                 quantityEntry.requestFocus();
  173.             }
  174.         });
  175.         return partNumberEntry;
  176.     }
  177.  
  178.     public JComboBox getSalutation() {
  179.         String temp[] = { "Choose one", "Mr.", "Mrs.", "Ms.", "Dr.", "Prof." };
  180.         final JComboBox salutation = new JComboBox(temp);
  181.         salutation.setPreferredSize(new Dimension(200, 20));
  182.         salutation.addItemListener(new ItemListener() {
  183.             public void itemStateChanged(ItemEvent ie) {
  184.                 salutationAsEntered = (String) salutation.getSelectedItem();
  185.                 System.out.println(salutationAsEntered);
  186.             }
  187.         });
  188.         return salutation;
  189.     }
  190.  
  191.     private JComboBox getSuffix() {
  192.         String temp[] = { "Choose one.", "Jr.", "Sr.", "I", "II", "III", "IV",
  193.                 "V" };
  194.         final JComboBox suffix = new JComboBox(temp);
  195.         suffix.setPreferredSize(new Dimension(200, 20));
  196.         suffix.addItemListener(new ItemListener() {
  197.             public void itemStateChanged(ItemEvent ie) {
  198.                 suffixAsEntered = (String) suffix.getSelectedItem();
  199.                 System.out.println(suffixAsEntered);
  200.             }
  201.         });
  202.  
  203.         return suffix;
  204.     }
  205.  
  206.     private JTextField getQuantity() {
  207.         quantityEntry = new JTextField();
  208.         quantityEntry.setPreferredSize(new Dimension(200, 20));
  209.         quantityEntry.addActionListener(new ActionListener() {
  210.             public void actionPerformed(ActionEvent e) {
  211.                 quantityAsEntered = quantityEntry.getText().trim();
  212.                 // This deletes leading and trailing blanks in entry field, too
  213.                 quantityEntry.setText(quantityAsEntered);
  214.                 System.out.println(quantityAsEntered);
  215.                 if (quantityAsEntered.length() == 0)
  216.                     System.out.println("Quantity is required");
  217.                 priceEntry.requestFocus();
  218.             }
  219.         });
  220.         return quantityEntry;
  221.     } // end private JTextField quantityEntry()
  222.  
  223.     private JTextField getPrice() {
  224.         priceEntry = new JTextField();
  225.         priceEntry.setPreferredSize(new Dimension(200, 20));
  226.         priceEntry.addActionListener(new ActionListener() {
  227.             public void actionPerformed(ActionEvent e) {
  228.                 priceAsEntered = priceEntry.getText().trim();
  229.                 // This deletes leading and trailing blanks in entry field, too
  230.                 priceEntry.setText(priceAsEntered);
  231.                 System.out.println(priceAsEntered);
  232.                 if (priceAsEntered.length() == 0)
  233.                     System.out.println("Price is required");
  234.             }
  235.         });
  236.         return priceEntry;
  237.     }// end private JTextField priceEntry()
  238.  
  239. } // end class HW1StarterKit2010
  240.  
Sep 13 '10 #1
0 1052

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: TheKeith | last post by:
I'm just learning php and set up a sample mysql db to practice with. I have the following script and cannot for the life of me figure out why it is printing each field of the row twice? I checked...
1
by: danra | last post by:
Hi. I am trying to bring up the same dialog twice in a dialog-based application (In VC++ 6). Can anyone tell me please why either of these code segments won't work? Thank you very much. ...
3
by: Phil Latio | last post by:
I am following a book on PHP and MySQL and have come across the below SQL statement. CREATE TABLE users ( user_id MEDIUMINT(8) UNSIGNED NOT NULL AUTO_INCREMENT, username VARCHAR(20) NOT NULL,...
6
by: Just Me | last post by:
As I read the documentation sometimes it appears that "type" and "class" mean the same thing. Other times it appears that they do not. Under "Classes" mostly they use the word "class" but I also...
3
by: Bruce Wood | last post by:
Maybe I'm going nuts, but I was so sure that adding the same method more than once to a delegate would result in only one entry on the delegate's call list: this.UpdateEnd += new...
2
by: entfred | last post by:
I was experimenting with trying to select the same item in a select box twice in a row and found out that you need to do a refresh (view - refresh) in Internet Explorer. This is so you can click...
8
by: trooper | last post by:
I have a situation where an application is present in the form of one dll. The interface to the application is an api. The code update some global data structures. This dll comes as third party...
9
by: monkiller | last post by:
Hi there! I am a newbie of .NET and I need help to avoid opening the same application twice at the same time in VB.Net. Thanks for all your help. Monkiller
1
by: Gav | last post by:
Within my application I have a TabControl and I am adding TabPages in dynamically at run time. My problem is that I am able to create the same TabPage twice, ie. I click on button 1 it creates...
2
by: EzeeGoN | last post by:
Using Flash8 Actionscript 2.0 i am looking for a method to generate sequential numbers in a random order (not using the same number twice) can anyone help please?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.