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 3 of 3

Thread: Why does JList use Vectors?

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Why does JList use Vectors?

    Out of curiosity, does anyone know why JList allows you to only set the data via an Object array or an Object Vector (unless you make your own ListModel and such)? Why a Vector<Object> instead of the more generic List<Object>? They must have had a good reason for this.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Why does JList use Vectors?

    Interesting question. Same could be asked for JTable, JComboBox, etc... My understanding is that Swing was available prior to the introduction of Collections, at a time when Vector was the only 'List' available. List was probably never introduced as an option because implementation of your own model allows you the flexibility to use any data structure you wish.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Why does JList use Vectors?

    Vector is synchronized, so I could see an argument for preferring it over unsynchronized ArrayLists. You can modify the Vector that you pass in to the JList constructor, since the constructor just refers to the Vector directly:

        public JList(final Vector<? extends E> listData) {
            this (
                new AbstractListModel<E>() {
                    public int getSize() { return listData.size(); }
                    public E getElementAt(int i) { return listData.elementAt(i); }
                }
            );
        }

    Since you're using Vector, in theory you can modify the Vector from any Thread without weird stuff happening.

    However, from my testing, you need to call JList.updateUI() after you modify it anyway, so I don't see any huge benefits from this approach:

    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Vector;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.JScrollPane;
     
    public class JListTest {
     
    	public JListTest(){
     
    		final JFrame frame = new JFrame ("JList Test");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		final Vector<String> v = new Vector<String>();
    		v.add("one");
    		v.add("two");
    		v.add("three");
     
    		final JList<String> list = new JList<String>(v);
     
    		JButton button = new JButton("Add");
    		button.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    				v.add("four");
    				list.updateUI();
    			}
    		});
     
    		frame.add(new JScrollPane(list), BorderLayout.CENTER);
    		frame.add(button, BorderLayout.SOUTH);
     
    		frame.pack();
    		frame.setVisible(true);
    	}
     
    	public static void main(String [] args){
    		new JListTest();
    	}
    }

    So I don't know. This might be a remnant of AWT which is supposed to be thread-safe, but it's often a bit of a waste of time to focus too much on these kinds of questions.

    Also, from the DefaultListModel API:

    This class loosely implements the java.util.Vector API, in that it implements the 1.1.x version of java.util.Vector, has no collection class support, and notifies the ListDataListeners when changes occur. Presently it delegates to a Vector, in a future release it will be a real Collection implementation.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Vectors and objects
    By Matty Alan in forum Java Theory & Questions
    Replies: 3
    Last Post: May 10th, 2011, 11:44 AM
  2. Need help with Vectors
    By Locky in forum Collections and Generics
    Replies: 6
    Last Post: October 19th, 2010, 04:45 PM
  3. Vectors
    By UnderX in forum Object Oriented Programming
    Replies: 1
    Last Post: October 19th, 2010, 04:29 PM
  4. Vectors
    By mgutierrez19 in forum Collections and Generics
    Replies: 4
    Last Post: March 3rd, 2010, 11:46 AM
  5. Replies: 3
    Last Post: November 15th, 2008, 07:17 AM