JAVA - How To Display Selected Item From List Into JTextfield In Java

JAVA - How To Display Selected Item From List Into JTextfield In Java

                                                                                             


In this java Tutorial we will see How To Show data from List To JTextFields
In Java NetBeans .

Source Code:


package JavaDB_001;

import java.awt.List;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;


public class Project extends JFrame{
    public Project(){
    super("Set The Selected Item In JTextField");
    JTextField tf = new JTextField(60);
    tf.setBounds(80,20,250,20);
    
    List ll = new List(5,false);
    ll.add("Item 1");
    ll.add("Item 2");
    ll.add("Irem 3");
    ll.add("Item 4");
    ll.add("Item 5");
    ll.add("Irem 6");
    
    ll.addItemListener(new ItemListener() {

        public void itemStateChanged(ItemEvent e) {
         tf.setText(ll.getSelectedItem());
        }
    });
    
    ll.setBounds(100,60,150,100);   
    add(ll);
    add(tf);
    setLayout(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setSize(400,300);
    setVisible(true);
    }
    public static void main(String[] args){
        new Project();
    }
   }
///////////////////////////////OUTPUT:
How To Display Selected Item From List Into JTextfield In Java

Displaying the Selected Item From List Into JTextfield In Java




Share this

Related Posts

Previous
Next Post »