4 Replies - 1219 Views - Last Post: 08 February 2014 - 10:41 AM Rate Topic: -----

#1 incredibleX   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 212
  • Joined: 01-November 13

Adding User Input to JComboBox

Posted 06 February 2014 - 10:37 PM

I'm creating a program which takes user input only when user selects "Add new Item" from JCombo Box.
And then adds user input to the list.
I'm using array list for this.
Please tell me whats wrong in my code?

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.*;


public class comboBoxAddition extends JFrame{
	
	ArrayList<String> item = new ArrayList<String>();
	JComboBox cb;
	JButton save = new JButton("Save");
	
	public comboBoxAddition() {
		// TODO Auto-generated constructor stub
		setSize(500,500);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(null);
		
		  item.add("Select Item");
	      item.add("RG_UPS");
	      item.add("RG_Stablizer");
	      item.add("Add New Item");
	      
	      cb=new JComboBox(item.toArray());
	      cb.setBounds(50,50,100,25);
	      
	      save.setBounds(50,90,100,25);
			save.addActionListener(new ActionListener() {
	            public void actionPerformed(ActionEvent e){
	            	 if(cb.getSelectedItem().toString().equals("Add New Item")){
	       	    	  String newItem=JOptionPane.showInputDialog("Enter Item");
	       	    	  item.add(newItem);
	            	}
	            }
	            
	        }); 
	      
		add(cb);
		add(save);
		setVisible(true);
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new comboBoxAddition();
	}

}




Is This A Good Question/Topic? 0
  • +

Replies To: Adding User Input to JComboBox

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12355
  • View blog
  • Posts: 45,464
  • Joined: 27-December 08

Re: Adding User Input to JComboBox

Posted 06 February 2014 - 10:43 PM

What specific problems or errors are you encountering?
Was This Post Helpful? 0
  • +
  • -

#3 incredibleX   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 212
  • Joined: 01-November 13

Re: Adding User Input to JComboBox

Posted 06 February 2014 - 11:00 PM

View Postmacosxnerd101, on 07 February 2014 - 05:43 AM, said:

What specific problems or errors are you encountering?


after adding to the array list the item does not appears in JCombo box.
It should be added to Combo Box.
Was This Post Helpful? 0
  • +
  • -

#4 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3580
  • View blog
  • Posts: 16,358
  • Joined: 20-September 08

Re: Adding User Input to JComboBox

Posted 08 February 2014 - 10:27 AM

You need to add directly to the ComboBoxModel

        save.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (cb.getSelectedItem().toString().equals("Add New Item")) {
                        String newItem = JOptionPane.showInputDialog(
                                "Enter Item");
                        DefaultComboBoxModel model = (DefaultComboBoxModel) cb.getModel();
                        model.addElement(newItem);
                    }
                }
            });


btw, the above code is not 100% sound as you didn't create DefaultComboBoxModel explicitly, so the cast is assumptive (it shouldn't be)
Was This Post Helpful? 2
  • +
  • -

#5 incredibleX   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 212
  • Joined: 01-November 13

Re: Adding User Input to JComboBox

Posted 08 February 2014 - 10:41 AM

View Postg00se, on 08 February 2014 - 05:27 PM, said:

You need to add directly to the ComboBoxModel


It worked thanks alot for help :) :rockon:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1