Friday, July 29, 2011

Add Items on JList using JTextField

Output:
Code:

/**

* File: jtextfieldToJList.java

* Tiltle: Add Items on JList using JTextField

* Author: http://java-code-complete.blogspot.com

*/



//Java Extension Packages

import javax.swing.*;

//Java Core Packages

import java.awt.*;

import java.awt.event.*;



public class jtextfieldToJList extends JFrame {



//Initializing JTextField, JList, and DefaultListModel class

private DefaultListModel model;

private JList list;

private JTextField input;



//Setting up GUI

public jtextfieldToJList() {



//Setting up the Title of the Window

super("Add Item on JList using JTextField");



//Set Size of the Window (WIDTH, HEIGHT)

setSize(280,170);



//Exit Property of the Window

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



//Constructing JTextField, JList, and DefaultListModel

model = new DefaultListModel();

input = new JTextField("Type your Inputs Here and Press Enter");

list = new JList(model);



//Setting JList Properties

list.setVisibleRowCount(8); //Number of Itmes to be displayed. If greater than 8, Vertical ScrollBar will be displayed.

list.setFixedCellHeight(15); //Fix width of the JList

list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);



//Setting up the container ready for the components to be added.

Container pane = getContentPane();

setContentPane(pane);



//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 JList

model.addElement(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 in the container with a BorderLayout of NORTH

pane.add(input,BorderLayout.NORTH);



//Adding the JList in the container with a component JScrollPane that's automatically creates Vertical and Horizontal Scroll Bar

//if the items or elements are greater than the specified "VisibleRowCount" in line 38.

pane.add(new JScrollPane(list),BorderLayout.SOUTH);



/**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) {

jtextfieldToJList aijl = new jtextfieldToJList();

}

}


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 JList

model.addElement(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);

}

}

);

1 comment: