1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

HUGE JComboBox Problem. Please Help!

Discussion in 'Java' started by ukeybuntu, May 2, 2010.

  1. ukeybuntu

    ukeybuntu New Member

    Joined:
    May 2, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I'm having this big problem with my JComboBox. Every time I choose an Item, it call's the selected Item twice I can not figure out what I have coded wrong. Here is my code. Thank you in advance for anyone that can help me out.

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.sql.ResultSet;
    import java.sql.*;
    
    
    class womensAthletic extends JFrame
    {
    	private JTextField jtfSearch;
    	private JButton searchButton, orderStatButton, accountButton, cartButton;
    	private JMenuItem optiosnMenu, womenMenu, menMenu, childrenMenu, wAthletic, wCasual, wDress, mAthletic, mCasual, mDress, boysMenu, 
    		girlsMenu, bAthletic, bCasual, bDress, gAthletic, gCasual, gDress, orderMenu, orderStat, accountMenu, jmiAccount, cartMenu, 
    		shopCart, jmiLogout, jmiExit, jmiHelp, jmiAbout, jmiHome;
    	private ImageIcon banner = new ImageIcon("F:/images/footprint_banner.jpg");	
    	
    	//an array of strings for the shoe brands
    	private String [] womenAthNames = {"  ", "ASICS GT-2150", "New Balance WR1906", "Nike Shox - Turbo +10", "Under Armour Blacktip 2"};
    	
    
    	//create a combo box for the shoes
    JComboBox jcbo = new JComboBox(womenAthNames);	
    
    
    	
    	womensAthletic()
     	{
    		//create menu bar
    		JMenuBar homeMenuBar = new JMenuBar();
    		setJMenuBar(homeMenuBar);
    
    		//add menu "options" to menu bar
    		JMenu optionsMenu = new JMenu("Options");
    		optionsMenu.setMnemonic('O');
    		homeMenuBar.add(optionsMenu);
    
    		//add menu items with mnemonics to menu "options"
    		optionsMenu.add(jmiHome = new JMenuItem("Home", 'H'));			//maybe not sure to keep this menuitem yet
    		optionsMenu.add(jmiLogout = new JMenuItem("Logout", 'L'));
    		optionsMenu.addSeparator();
    		optionsMenu.add(jmiExit = new JMenuItem("Exit", 'E'));
    
    
    		//add menu "women"
    		JMenu womenMenu = new JMenu("Women");
    		womenMenu.setMnemonic('W');
    
    		//add menu items to "women"
    		womenMenu.add(wAthletic = new JMenuItem("Athletic", 'A'));
    		womenMenu.add(wCasual = new JMenuItem("Casual", 'C'));
    		womenMenu.add(wDress = new JMenuItem("Dress", 'D'));
    		homeMenuBar.add(womenMenu);
    
    		//add menu "men"
    		JMenu menMenu = new JMenu("Men");
    		menMenu.setMnemonic('M');
    
    		//add menu items to "men"
    		menMenu.add(mAthletic = new JMenuItem("Athletic", 'A'));
    		menMenu.add(mCasual = new JMenuItem("Casual", 'C'));
    		menMenu.add(mDress = new JMenuItem("Dress", 'D'));
    		homeMenuBar.add(menMenu);
    
    		//add menu "chlidren"
    		JMenu childrenMenu = new JMenu("Children");
    		childrenMenu.setMnemonic('C');
    
    		//add menu items to "children"
    		childrenMenu.add(boysMenu = new JMenu("Boys"));
    		boysMenu.add(bAthletic = new JMenuItem("Athletic"));
    		boysMenu.add(bCasual = new JMenuItem("Casual"));
    		boysMenu.add(bDress = new JMenuItem("Dress"));
    
    		childrenMenu.add(girlsMenu = new JMenu("Girls"));
    		girlsMenu.add(gAthletic = new JMenuItem("Athletic"));
    		girlsMenu.add(gCasual = new JMenuItem("Casual"));
    		girlsMenu.add(gDress = new JMenuItem("Dress"));
    		homeMenuBar.add(childrenMenu);
    
    		//add menu "order status" to menu bar
    		JMenu orderMenu = new JMenu("Order Status");
    		orderMenu.setMnemonic('O');
    		homeMenuBar.add(orderMenu);
    		orderMenu.add(orderStat = new JMenuItem("OrderStatus", 'O'));
    
    
    		//add menu "my account" to menu bar
    		JMenu accountMenu = new JMenu("My Account");
    		accountMenu.setMnemonic('M');
    		homeMenuBar.add(accountMenu);
    		accountMenu.add(jmiAccount = new JMenuItem("MyAccount", 'M'));
    
    
    		//add menu "cart" to menu bar
    		JMenu cartMenu = new JMenu("Shopping Cart");
    		cartMenu.setMnemonic('S');
    		cartMenu.add(shopCart = new JMenuItem("Shopping Cart", 'S'));
    		homeMenuBar.add(cartMenu);
    
    		//add menu "help"
    		JMenu helpMenu = new JMenu("Help");
    		helpMenu.setMnemonic('H');
    		helpMenu.add(jmiAbout = new JMenuItem("About", 'A'));
    		homeMenuBar.add(helpMenu);
    
    		//Panel to hold search bar
    		JPanel pSearch = new JPanel(new FlowLayout());		
    		pSearch.setBorder(new TitledBorder("Search for a Product"));
    		pSearch.add(jtfSearch = new JTextField(15));
    		pSearch.add(searchButton = new JButton("GO"));
    
    		//panel to hold buttons
    		JPanel pButtons = new JPanel(new FlowLayout());
    		pButtons.setBorder(new TitledBorder(" "));
    		pButtons.add(orderStatButton = new JButton("Order Status"));
    		pButtons.add(accountButton = new JButton("My Account"));
    		pButtons.add(cartButton = new JButton("Shopping Cart"));
    
    		//panel that holds search bar and buttons
    		JPanel p1 = new JPanel(new FlowLayout());
    		p1.add(pSearch);
    		p1.add(pButtons);
    
    		//Panel with image
    		JPanel p3 = new JPanel(new FlowLayout());
    		p3.add(new JLabel(banner));
    
    		//panel to hold combo
    		JPanel pCombo = new JPanel(new FlowLayout());
    		pCombo.add(new JLabel("Shoe Selection"));
    		pCombo.add(jcbo);
    
    		//panel with combo and description
    		//JPanel comboDesc = new JPanel(new FlowLayout());
    		//comboDesc.add(pCombo);
    
    		//panel to hold p1 and pcombo
    		JPanel p2 = new JPanel(new BorderLayout());
    		p2.add(p1, BorderLayout.CENTER);
    		p2.add(pCombo, BorderLayout.SOUTH);
    
    		//add panels to frame
    		JPanel panel = new JPanel(new FlowLayout());
    		panel.add(p3);
    		panel.add(p2);
    		//panel.add(comboDesc);
    		add(panel);
    		
    
    //all listeners for menu items
    //more listeners
    
    //listner for combobox
    	jcbo.addItemListener(new ItemListener(){
    			public void itemStateChanged(ItemEvent e){						//why is it opening up the window twice??
    					String str = (String)jcbo.getSelectedItem();
    					//System.out.println("you choose this shoe: "+str);
    					
    					
    					if (str.equals("ASICS GT-2150")){
    						womensAsics asicsShoeFace =new womensAsics();
    						asicsShoeFace.setVisible(true);
    						asicsShoeFace.setLocationRelativeTo(null);
    						womensAthletic.this.dispose();
    		                womensAthletic.this.setVisible(false);	
    					}
    					else if (jcbo.getSelectedItem().equals("New Balance WR1906")){	
    						womensNB newBalanceShoeFace =new womensNB();
    						newBalanceShoeFace.setVisible(true);
    						newBalanceShoeFace.setLocationRelativeTo(null);
    						womensAthletic.this.dispose();
    		                womensAthletic.this.setVisible(false);	
    					}
    					else if (jcbo.getSelectedItem().equals("Nike Shox - Turbo +10")){
    						womensNike nikeShoeFace =new womensNike();
    						nikeShoeFace.setVisible(true);
    						nikeShoeFace.setLocationRelativeTo(null);
    						womensAthletic.this.dispose();
    		                womensAthletic.this.setVisible(false);	
    					}
    					else if(jcbo.getSelectedItem().equals("Under Armour Blacktip 2")){
    						womensUA underShoeFace =new womensUA();
    						underShoeFace.setVisible(true);
    						underShoeFace.setLocationRelativeTo(null);
    						womensAthletic.this.dispose();
    		                womensAthletic.this.setVisible(false);	
    					}
    			}
    		});
    
    
    //listener for search items	
    
    
    //listeners for menu items
    
    	   setLocationRelativeTo(null);
    	   setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    	   setTitle("Footprint Shoes: Women's Athletic Shoes");
           setExtendedState(Frame.MAXIMIZED_BOTH);
    
    	}
    
    }
    
    
    

    and here is the code for just the jcombobox listener

    Code:
    jcbo.addItemListener(new ItemListener(){
    			public void itemStateChanged(ItemEvent e){						//why is it opening up the window twice??
    					String str = (String)jcbo.getSelectedItem();
    					//System.out.println("you choose this shoe: "+str);
    					
    					
    					if (str.equals("ASICS GT-2150")){
    						womensAsics asicsShoeFace =new womensAsics();
    						asicsShoeFace.setVisible(true);
    						asicsShoeFace.setLocationRelativeTo(null);
    						womensAthletic.this.dispose();
    		                womensAthletic.this.setVisible(false);	
    					}
    					else if (jcbo.getSelectedItem().equals("New Balance WR1906")){	
    						womensNB newBalanceShoeFace =new womensNB();
    						newBalanceShoeFace.setVisible(true);
    						newBalanceShoeFace.setLocationRelativeTo(null);
    						womensAthletic.this.dispose();
    		                womensAthletic.this.setVisible(false);	
    					}
    					else if (jcbo.getSelectedItem().equals("Nike Shox - Turbo +10")){
    						womensNike nikeShoeFace =new womensNike();
    						nikeShoeFace.setVisible(true);
    						nikeShoeFace.setLocationRelativeTo(null);
    						womensAthletic.this.dispose();
    		                womensAthletic.this.setVisible(false);	
    					}
    					else if(jcbo.getSelectedItem().equals("Under Armour Blacktip 2")){
    						womensUA underShoeFace =new womensUA();
    						underShoeFace.setVisible(true);
    						underShoeFace.setLocationRelativeTo(null);
    						womensAthletic.this.dispose();
    		                womensAthletic.this.setVisible(false);	
    					}
    			}
    		});
    
    
    
    
     

Share This Page