Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: JComboBox and Textfield

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default JComboBox and Textfield

    Hello, I am creating a JComboBox and textfield. When ever the user selects an item from the ComboBox its supposed to show up in the textfield. Right now, if you select one item, it will show up in the textfield, select another item and it take out the previous item and show the new one. I want to make it so that all of the user choices will be in the textfield.

    My code so far:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class ComboBox{
    	JComboBox combo;
    	JTextField txt;
    	public static void main(String[] args) {
    		ComboBox b = new ComboBox();
    	}
     
    	public ComboBox(){
    		String course[] = {"BCA","MCA","PPC","CIC"};
    		JFrame frame = new JFrame("Creating a JComboBox Component");
    		JPanel panel = new JPanel();
    		combo = new JComboBox(course);
    		combo.setBackground(Color.gray);
    		combo.setForeground(Color.red);
    		txt = new JTextField(10);
    		panel.add(combo);
    		panel.add(txt);
    		frame.add(panel);
    		combo.addItemListener(new ItemListener(){
    			public void itemStateChanged(ItemEvent ie){
    				String str = (String)combo.getSelectedItem();
    				txt.setText(str);
    			}
    		});
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(400,400);
    		frame.setVisible(true);
    	}
    }
    Last edited by helloworld922; November 8th, 2009 at 06:02 PM. Reason: please use [code] tags


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,301
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: JComboBox and Textfield

    JComboBoxes are usually used to provide for single rather than multiple selections, so when you say "all the users choices" do you mean all the choices that the user has gone through up to that point? If so this would be as simple as
    changing
    txt.setText(str);
    to
    txt.setText(txt.getText() + " " + str);//space or any other delim
    Should you wish for multiple selections at a time, you may want to use a series of JCheckBoxes (for smaller numbers) or a JTable (for larger numbers)

  3. The Following User Says Thank You to copeg For This Useful Post:

    Nexusfactor (November 9th, 2009)

  4. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JComboBox and Textfield

    Copeg Thanks, It does what I want, only problem is it repeats the selection twice instead of once, any ideas why?

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,301
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: JComboBox and Textfield

    Quote Originally Posted by Nexusfactor View Post
    Copeg Thanks, It does what I want, only problem is it repeats the selection twice instead of once, any ideas why?
    Item events are triggered for a variety of events, so you should screen them to get the behavior you want.

    public void itemStateChanged(ItemEvent e){
    	if ( e.getStateChange() == ItemEvent.SELECTED ){
    		//your code here
    	}
    }

  6. The Following User Says Thank You to copeg For This Useful Post:

    Nexusfactor (November 9th, 2009)

Similar Threads

  1. Problem with reset on JComboBox
    By WorkingMan in forum AWT / Java Swing
    Replies: 4
    Last Post: April 25th, 2013, 12:19 PM
  2. Combo/TextField Help
    By GRossi0511 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 26th, 2009, 08:15 PM
  3. JComboBox doubt
    By rajaramesh.tadi in forum AWT / Java Swing
    Replies: 0
    Last Post: August 24th, 2009, 08:18 AM
  4. Replies: 4
    Last Post: May 27th, 2008, 01:20 PM