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

Thread: SpinnerListModel setList(List<?> list)

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default SpinnerListModel setList(List<?> list)

    I'm being challenged by SpinnerListModel's setList(List<?> list) method.

    Currently I have defined a SpinnerListModel:

    private static Vector Hours12Strings = new Vector();
    static{
    Hours12Strings.add("1");
    ......... etc..........
    }

    Later on in "createGUI"


    SpinnerListModel hoursModel = new SpinnerListModel(Hours12Strings);
    This works fine.

    I have also created a static Vector named Hours24Strings and I would like to switch between 12 and 24 hour modes so I do:

    if(checkBox1224.isSelected()){
    hoursModel.setList((java.util.List)Hours24Strings) ;
    }else{
    hoursModel.setList((java.util.List)Hours12Strings) ;
    }
    And the compiler gives me the following error:

    java:150: cannot find symbol
    [javac] symbol : method setList(java.util.Vector)
    [javac] location: interface javax.swing.SpinnerModel
    [javac] hoursModel.setList(Hours24Strings);
    [javac] ^
    I'm being "dense" / what am I missing?

    Thanks in advance.
    roy


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

    Default Re: SpinnerListModel setList(List<?> list)

    Vector implements List, so there shouldn't be a need to perform the class cast, and a setList method that accepts a List should work for a Vector. Can you provide a short compilable example the demo's the problem?

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SpinnerListModel setList(List<?> list)

    here's the compilable version:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;        
    import javax.swing.event.*;        
    import java.util.*;
    import java.io.PrintWriter;
    import java.io.ByteArrayOutputStream;
     
    public class DigitalTimePanel 
    extends JPanel
    implements ActionListener
    {	
    	public DigitalTimePanel()
    	{
    		super();
    		initialize();
    	}
     
    	private static Vector Hours24Strings = null;
    	private static Vector Hours12Strings = null;
     
    	static 
    	{
    		Hours24Strings = new Vector();
    		for(int n = 0; n < 24; n++){ Hours24Strings.add( Formatter(n)); }
     
    		Hours12Strings = new Vector();
    		for(int n = 0; n <13; n++){ Hours12Strings.add( (n == 0 ? "12" : Formatter(n))); }
    	}
     
       	private static String Formatter(int value)
       	{
       		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
       		PrintWriter pw = new PrintWriter(byteArrayOutputStream);
       		pw.format("%1$02d",value);
       		pw.flush();
       		return byteArrayOutputStream. toString();
       	}
     
    	private void initialize()
    	{
     
    		setLayout(new FlowLayout(FlowLayout.CENTER,2,1));
    		JPanel p = new JPanel(new GridLayout(2,1));
    		add(p);
     
    		JPanel sp = new JPanel(new FlowLayout(FlowLayout.LEFT, 2,1));
    		p.add(sp);
    			hoursModel = new SpinnerListModel(Hours12Strings);
    			hoursSpinner =  new JSpinner(hoursModel);
    			sp.add(hoursSpinner);
     
    		sp = new JPanel(new FlowLayout(FlowLayout.CENTER, 2,1));
    		p.add(sp);
    			checkBox1224 = new JCheckBox("24 hour");
    			checkBox1224.setSelected(is24Hours);
    			checkBox1224.addActionListener(this);
    			sp.add(checkBox1224);
       	}	
     
     
        	public JFormattedTextField getTextField(JSpinner spinner) 
        	{
    		JComponent editor = spinner.getEditor();
    		if (editor instanceof JSpinner.DefaultEditor) {
    			return ((JSpinner.DefaultEditor)editor).getTextField();
    		} else {
    			System.err.println("Unexpected editor type: " + spinner.getEditor().getClass() + " isn't a descendant of DefaultEditor");
    			return null;
    		}
    	}
     
          	private JSpinner hoursSpinner =  null;	
    	private SpinnerModel hoursModel = null;
     
    	private JCheckBox checkBox1224 = null;
      	private boolean is24Hours = false;
     
    	public void actionPerformed(ActionEvent e)
    	{
    		Object source = e.getSource();
    		if(source == checkBox1224){
    			if(checkBox1224.isSelected()){
    				hoursModel.setList(Hours24Strings);
    			}else{
    				hoursModel.setList(Hours12Strings);
    			}
    		}
    	}
     
    	public void set24Hours(boolean is24Hours)
    	{
    		this.is24Hours = is24Hours; 
    		checkBox1224.setSelected(is24Hours);
    	}
     
    }

    Which gives javac DigitalTimePanel.java yields:

    DigitalTimePanel.java:26: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.Vector
    		for(int n = 0; n < 24; n++){ Hours24Strings.add( Formatter(n)); }
    		                                               ^
    DigitalTimePanel.java:29: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.Vector
    		for(int n = 0; n <13; n++){ Hours12Strings.add( (n == 0 ? "12" : Formatter(n))); }
    		                                              ^
    DigitalTimePanel.java:85: cannot find symbol
    symbol  : method setList(java.util.Vector)
    location: interface javax.swing.SpinnerModel
    				hoursModel.setList(Hours24Strings);
    				          ^
    DigitalTimePanel.java:87: cannot find symbol
    symbol  : method setList(java.util.Vector)
    location: interface javax.swing.SpinnerModel
    				hoursModel.setList(Hours12Strings);
    				          ^
    2 errors
    2 warnings

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

    Default Re: SpinnerListModel setList(List<?> list)

    The function setList is not defined for the interface SpinnerModel - the code abstracts the SpinnerListModel to SpinnerModel, eg:
    SpinnerModel hoursModel = new SpinnerListModel();
    Define it as a SpinnerListModel which defines this method and it should compile.

  5. The Following User Says Thank You to copeg For This Useful Post:

    roy epperson (November 29th, 2010)

  6. #5
    Junior Member
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SpinnerListModel setList(List<?> list)

    Quote Originally Posted by copeg View Post
    The function setList is not defined for the interface SpinnerModel - the code abstracts the SpinnerListModel to SpinnerModel, eg:
    SpinnerModel hoursModel = new SpinnerListModel();
    Define it as a SpinnerListModel which defines this method and it should compile.

    Good spot!! That's what I get from working on too many things yesterday

    Thanks for the help!

Similar Threads

  1. list in JSP
    By smackdown90 in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: November 13th, 2011, 01:08 PM
  2. ADT list help
    By jkalm in forum Collections and Generics
    Replies: 15
    Last Post: October 18th, 2010, 03:13 PM
  3. need help with a list class
    By araujo3rd in forum Object Oriented Programming
    Replies: 1
    Last Post: February 25th, 2010, 07:58 PM
  4. Doubt regarding LIST
    By puneetsr in forum Collections and Generics
    Replies: 1
    Last Post: February 23rd, 2010, 04:19 PM
  5. Which collection is best to do mathematical operation on it?
    By Sterzerkmode in forum Java Theory & Questions
    Replies: 1
    Last Post: May 7th, 2009, 04:48 AM