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

Thread: Type magic

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Type magic

    I used a DefaultListModel to populate a JList.

    Example: dlm.addElement(s); where s=String

    When I retrieve a value from the JList using the index of dlm, it works great
    When I output text -- "JList points to : " + lm.elementAt(idx) -- this also works great

    When I attempt -- s = lm.elementAt(idx); javac flags me with:

    incompatible types
    found : java.lang.Object
    required: java.lang.String

    is there a handy method for getting my String back? A few times in the past I have gotten into similar type issues where I pick up some output and go to its type, at which class there is a method that sounds like it may get closer, but it only outputs to class x ... and so on, and never do get what I needed in the first place. Hoping I missed something simple.



    TIA


  2. #2
    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: Type magic

    I'm not really sure what you're talking about- could you provide an SSCCE that demonstrates what you mean?
    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!

  3. #3
    Member
    Join Date
    Jul 2011
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Type magic

    Is SSCCE same as Java source in whole program form?

  4. #4
    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: Type magic

    Quote Originally Posted by meathead View Post
    Is SSCCE same as Java source in whole program form?
    No. Did you click the link I gave you? It's a very simplified program, only a few lines long, that very specifically demonstrates what you're talking about.
    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!

  5. #5
    Member
    Join Date
    Jul 2011
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Type magic

    Oops!


    Object o = lm.elementAt(idx);
    sGet = o.toString();
    jlabChoose.setText("JList points to : " + sGet);


    thanks for the offer, sorry for the bother. I can't believe this has been holding me up. Seems like a klug way to go about things, but I don't mean to sound ungrateful to Gosling & the crew.

  6. #6
    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: Type magic

    The elementAt() method returns an Object- that just means you can put any Object into it. String is an Object. But you could also put other stuff into it (Integers, other Objects), which is why the class can't guarantee you're getting a String.

    I believe that what you're looking for is a simple cast, which tells Java what kind of Object an instance is:

    String sGet = (String) lm.elementAt(idx);
    Last edited by KevinWorkman; October 19th, 2011 at 12:17 PM.
    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. JAVA MAGIC SQUARE
    By hiimjoey11 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: November 28th, 2012, 12:42 AM
  2. How to convert from type double to type int?
    By rph in forum Object Oriented Programming
    Replies: 7
    Last Post: July 25th, 2011, 04:21 AM
  3. Applet ClassFormatError and magic values
    By mjpam in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 6th, 2011, 04:20 AM
  4. Magic Square Checker
    By Hypnos in forum Object Oriented Programming
    Replies: 1
    Last Post: April 3rd, 2011, 06:37 PM
  5. Magic Squares, input confusion
    By bengiles89 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2010, 08:40 PM