Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to Use the JList component - Java Swing

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Location
    SomeWhere in the world
    Posts
    27
    Thanks
    1
    Thanked 11 Times in 6 Posts

    Post How to Use the JList component - Java Swing



    import java.awt.Color;
    import java.awt.FlowLayout;
    import javax.swing.border.*;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.JScrollPane;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
     
     
    public class ListFrame extends JFrame{
     
        private static final long serialVersionUID = 1L;
        private JList colorList;
        private final String colorNames[]={"BLACK","BLUE","CYAN","ORANGE","DRAK_GRAY"};
        private final Color colors[]={Color.black,Color.blue,Color.cyan,Color.orange,Color.DARK_GRAY};
        private EtchedBorder eb= new EtchedBorder(1, Color.black, Color.gray);
     
        public ListFrame(){
            super("List ");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(300, 300);
            setLayout(new FlowLayout());
     
            colorList = new JList(colorNames);
            colorList.setSelectionMode(0);
            colorList.setBorder(eb);
     
            add(new JScrollPane(colorList));
     
            colorList.addListSelectionListener(new ListSelectionListener() {
     
                @Override
                public void valueChanged(ListSelectionEvent e) {
                    getContentPane().setBackground(colors[colorList.getSelectedIndex()]);
                }
            });
     
            add(colorList);
            setVisible(true);
        }
     
        public static void main(String args[]){
            new ListFrame();
        }
    }

  2. The Following 3 Users Say Thank You to neo_2010 For This Useful Post:

    Fendaril (September 1st, 2009), JavaPF (July 16th, 2009), Json (July 11th, 2009)


  3. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: How to Use JList

    Thanks for that, I'm not a swing person my self but if I ever want to learn I know where to go

    Keep it up!

    // Json

Similar Threads

  1. Replies: 10
    Last Post: June 22nd, 2009, 08:45 AM
  2. [SOLVED] JAVA JList Problem in visual images
    By antitru5t in forum AWT / Java Swing
    Replies: 4
    Last Post: April 15th, 2009, 04:09 PM
  3. Replies: 1
    Last Post: March 31st, 2009, 03:49 PM
  4. Dropping to graphic element dragged from JList
    By tua1 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 29th, 2008, 08:22 AM