Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By jim829

Thread: JList Listener ?

  1. #1
    mohsen.noor is offline Member
    Join Date
    Dec 2012
    Posts
    18
    Rep Power
    0

    Default JList Listener ?

    Hi everyone, I want to write the code when I click on an element in JList it shows in JTestField
    But I have compiler error ListSelectionListener class does not exist
    Thanks in advance

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class JListDemo implements ListSelectionListener{
    
    						JTextField txt;
    						JList list;
    						JScrollPane scroll;
    						JPanel panel;
    						JFrame frame;
    						String listEntries[];
    						JListDemo(){
    								listEntries = new String[]{"alpah","beta","gamma","delta"};
    								txt =new JTextField();
    								list = new JList(listEntries);
    								panel = new JPanel();
    								frame = new JFrame();
    								scroll = new JScrollPane(list);
    								scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    								list.setVisibleRowCount(4);
    								list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    								panel.add(scroll);
    								frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    								frame.getContentPane().add(BorderLayout.CENTER,panel);
    								frame.getContentPane().add(BorderLayout.SOUTH,txt);
    								list.addListSelectionListener(this);
    								}
    					  public void valueChanged(ListSelectionEvent lse){
    							if(!lse.getValueIsAdjusting())
    								txt.setText((String)list.getSelectedValue());
    						}
    					public static void main(String args[]){
    						JListDemo jld = new JListDemo();
    						}
    		}//END CLASS

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,375
    Rep Power
    5

    Default Re: JList Listener ?

    import javax.swing.event.ListSelectionListener;

    Regards,
    Jim
    mohsen.noor likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Keyboard Listener to Mouse Listener
    By cupOJava in forum Advanced Java
    Replies: 0
    Last Post: 04-28-2013, 09:26 PM
  2. Replies: 3
    Last Post: 01-18-2012, 05:51 PM
  3. Replies: 4
    Last Post: 01-10-2012, 01:05 PM
  4. Replies: 2
    Last Post: 12-11-2011, 10:44 PM
  5. Link one JList to another JList
    By mib1bee in forum AWT / Swing
    Replies: 1
    Last Post: 12-31-2010, 07:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •