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); } }