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 2 of 2

Thread: Need Help with my JComboBox and Textfield

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

    Default Need Help with my JComboBox and Textfield

    I have a Combox with a list of items that I want to user to choose from. Now if the user chooses pizza it will show in the combo box twice, if they make another selection for example, fries, it will show fries twice. I want to show all the user choices, but only once, so if they choose Pizza, Pizza appears in the textfield, if the choose fries next, then right after prizza, fries should appear.
    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[] = {"Burger","Fries","PizzA","Coke"};
    		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(txt.getText() + " " + str);
    			}
    		});
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(400,400);
    		frame.setVisible(true);
    	}
    }
    Last edited by helloworld922; November 10th, 2009 at 10:48 AM.


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,102
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Need Help with my JComboBox and Textfield

    See the following thread:

    http://www.javaprogrammingforums.com...textfield.html

    The idea being, you need to screen your input events for selection type events:
    public void itemStateChanged(ItemEvent e){
    	if ( e.getStateChange() == ItemEvent.SELECTED ){
    		//your code here
    	}
    }

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. JComboBox and Textfield
    By Nexusfactor in forum AWT / Java Swing
    Replies: 3
    Last Post: November 9th, 2009, 11:57 AM
  3. Combo/TextField Help
    By GRossi0511 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 26th, 2009, 08:15 PM
  4. JComboBox doubt
    By rajaramesh.tadi in forum AWT / Java Swing
    Replies: 0
    Last Post: August 24th, 2009, 08:18 AM
  5. Replies: 4
    Last Post: May 27th, 2008, 01:20 PM