Output:
Code:
/** * File: jtextfieldToJComboBox.java * Tiltle: Add Item on JComboBox using JTextField * Author: http://java-program-sample.blogspot.com */ //Java Extension Packages import javax.swing.*; //Java Core Packages import java.awt.*; import java.awt.event.*; public class jtextfieldToJComboBox extends JFrame { //Initializing JTextField and JComboBox private JComboBox combo; private JTextField input; //Setting up GUI public jtextfieldToJComboBox() { //Setting up the Title of the Window super("Add Item on JComboBox using JTextField"); //Set Size of the Window (WIDTH, LENGTH) setSize(320,80); //Exit Property of the Window setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Constructing JTextField and JComboBox input = new JTextField("Type your Input Here and Press Enter"); combo = new JComboBox(); //Setting JComboBox Properties combo.setMaximumRowCount(5); //Number of Itmes to be displayed. If greater than 5, Vertical ScrollBar will be displayed. //Setting up the container ready for the components to be added. Container pane = getContentPane(); setContentPane(pane); //Setting up the container layout GridLayout layout = new GridLayout(2,1); pane.setLayout(layout); //Implemeting Even-Listener on JTextField's reference name "input" using ActionListener input.addActionListener( new ActionListener() { //Handle JTextField event if Enter key is pressed public void actionPerformed(ActionEvent event) { //Store the input from JTextField to the variable "message" String message = "The Input <"+input.getText()+"> is Successfully Added!"; //Adding items or elements from JTextField to JComboBox combo.addItem(input.getText()); //Display the input from JTextField to JOptionPane using the variable "message" JOptionPane.showMessageDialog(null, message,"Successfully Added!",JOptionPane.INFORMATION_MESSAGE); //The JTextField will be empty after JOptionPane is closed ready for the next input. input.setText(null); } } ); //Adding JTextField and JComboBox in the container pane.add(input); pane.add(combo); /**Set all the Components Visible. * If it is set to "false", the components in the container will not be visible. */ setVisible(true); } //Main Method public static void main(String[] args) { jtextfieldToJComboBox jtcb = new jtextfieldToJComboBox(); } }
Important Part of the Program:
//Implemeting Even-Listener on JTextField's reference name "input" using ActionListener input.addActionListener( new ActionListener() { //Handle JTextField event if Enter key is pressed public void actionPerformed(ActionEvent event) { //Store the input from JTextField to the variable "message" String message = "The Input <"+input.getText()+"> is Successfully Added!"; //Adding items or elements from JTextField to JComboBox combo.addItem(input.getText()); //Display the input from JTextField to JOptionPane using the variable "message" JOptionPane.showMessageDialog(null, message,"Successfully Added!",JOptionPane.INFORMATION_MESSAGE); //The JTextField will be empty after JOptionPane is closed ready for the next input. input.setText(null); } } );