DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 5 of 5

Thread: JList

  1. #1
    Pelle Henriksson Guest

    JList

    Hi !!

    Can someone please help me with this:
    I store objects in a Vector, and then i store the Vector in a file, here is that object:

    import java.io.*;
    import java.util.*;

    public class tblQuery implements Serializable {
    public String fldSuggestionTitle = null;
    public String fldSuggestionText = null;
    public Date fldSuggestionDate = null;
    public int fldUserID;

    }

    I would like to display these objects in a JList but only fldSuggestionTitle and then fldUserID as a hidden value,
    db is my vector with the tblQury objects. I dont understand how to hide an id and only show the title.

    private void test() {
    m = new DefaultListModel();

    db = clsDBHandler.readData("tblQuery.dat");
    for(int i =0; i < db.size(); i++){
    qry = (tblQuery) db.elementAt(i);
    m.addElement(qry.fldSuggestionTitle);

    }
    JList lstQuery = new JList(m);
    lstQuery.setBounds(10,10,100,100);
    getContentPane().add(lstQuery);
    }


    This one fetches the vector from file.

    import java.io.*;
    import java.util.*;

    public class clsDBHandler {

    public static Vector readData(String strFile) {
    Vector db = null;

    try {
    ObjectInputStream in = new ObjectInputStream(new FileInputStream(strFile));
    db = (Vector) in.readObject();
    in.close();
    }

    catch(FileNotFoundException e) {
    db = new Vector();
    }

    catch(Exception e) {
    }

    finally {
    return db;
    }
    }

    Chees and thanx Pelle


  2. #2
    Paul Clapham Guest

    Re: JList

    You can put any object into a JList, not only Strings. Whatever object you use, the default behaviour of JList should be to display the result of its "toString()" method. So change your class to include a method "toString()" that returns fldSuggestionTitle, and add the objects directly to the JList.

    PC2
    Pelle Henriksson <pelle_henriksson@home.se> wrote in message news:3a067ee7@news.devx.com...
    Hi !!

    Can someone please help me with this:
    I store objects in a Vector, and then i store the Vector in a file, here is that object:

    ...

    I would like to display these objects in a JList but only fldSuggestionTitle and then fldUserID as a hidden value,
    db is my vector with the tblQury objects. I dont understand how to hide an id and only show the title.



  3. #3
    Pelle Henriksson Guest

    Re: JList

    Thanx !!

    How should that method be implemented?, I tried

    public String toString() {

    retutrn fldSuggestionTitle;

    }

    but then I got a nullpointerexcepiton, if I remove this method it works ok, but I get tblQuery@484234 in the list.
    Do I need to change anything else in the class??

    pelle
    "Paul Clapham" <pclapham@core-mark
    .com> wrote in message news:3a06d5ae$1@news.devx.com...
    You can put any object into a JList, not only Strings. Whatever object you use, the default behaviour of JList should be to display the result of its "toString()" method. So change your class to include a method "toString()" that returns fldSuggestionTitle, and add the objects directly to the JList.

    PC2
    Pelle Henriksson <pelle_henriksson@home.se> wrote in message news:3a067ee7@news.devx.com...
    Hi !!

    Can someone please help me with this:
    I store objects in a Vector, and then i store the Vector in a file, here is that object:

    ...

    I would like to display these objects in a JList but only fldSuggestionTitle and then fldUserID as a hidden value,
    db is my vector with the tblQury objects. I dont understand how to hide an id and only show the title.



  4. #4
    Paul Clapham Guest

    Re: JList

    Yes, that's the correct implementation. But you need to set fldSuggestionTitle to something other than null -- I didn't see any code in your original post that does that.

    PC2
    Pelle Henriksson <pelle_henriksson@home.se> wrote in message news:3a07dd74$1@news.devx.com...
    Thanx !!

    How should that method be implemented?, I tried

    public String toString() {

    retutrn fldSuggestionTitle;

    }

    but then I got a nullpointerexcepiton, if I remove this method it works ok, but I get tblQuery@484234 in the list.
    Do I need to change anything else in the class??

    pelle


  5. #5
    Madani Guest

    Re: JList


    the toString method should be in another meothd that take your object as an
    argument and return a string

    public String thismethod (fldSuggestionTitle f) {

    return f.toString();

    }









    "Pelle Henriksson" <pelle_henriksson@home.se> wrote:
    >This is a multi-part message in MIME format.
    >
    >------=_NextPart_000_0026_01C048B0.2D0AF270
    >Content-Type: text/plain;
    > charset="iso-8859-1"
    >Content-Transfer-Encoding: quoted-printable
    >
    >Thanx !!
    >
    >How should that method be implemented?, I tried
    >
    >public String toString() {
    >
    > retutrn fldSuggestionTitle;
    >
    >}
    >
    >but then I got a nullpointerexcepiton, if I remove this method it works

    =
    >ok, but I get tblQuery@484234 in the list.
    >Do I need to change anything else in the class??
    >
    >pelle
    > "Paul Clapham" <pclapham@core-mark
    > .com> wrote in message news:3a06d5ae$1@news.devx.com...
    > You can put any object into a JList, not only Strings. Whatever =
    >object you use, the default behaviour of JList should be to display the

    =
    >result of its "toString()" method. So change your class to include a =
    >method "toString()" that returns fldSuggestionTitle, and add the objects

    =
    >directly to the JList.
    >
    > PC2
    > Pelle Henriksson <pelle_henriksson@home.se> wrote in message =
    >news:3a067ee7@news.devx.com...
    > Hi !!
    >
    > Can someone please help me with this:
    > I store objects in a Vector, and then i store the Vector in a file,

    =
    >here is that object:
    >
    > ...
    >
    > I would like to display these objects in a JList but only =
    >fldSuggestionTitle and then fldUserID as a hidden value,
    > db is my vector with the tblQury objects. I dont understand how to =
    >hide an id and only show the title.
    >
    >
    >------=_NextPart_000_0026_01C048B0.2D0AF270
    >Content-Type: text/html;
    > charset="iso-8859-1"
    >Content-Transfer-Encoding: quoted-printable
    >
    ><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    ><HTML><HEAD>
    ><META content=3D"text/html; charset=3Diso-8859-1" =
    >http-equiv=3DContent-Type>
    ><META content=3D"MSHTML 5.00.3103.1000" name=3DGENERATOR>
    ><STYLE></STYLE>
    ></HEAD>
    ><BODY bgColor=3D#ffffff>
    ><DIV><FONT face=3DArial size=3D2>Thanx !!</FONT></DIV>
    ><DIV>*</DIV>
    ><DIV><FONT face=3DArial size=3D2>How should that method be =
    >implemented?,*I=20
    >tried</FONT></DIV>
    ><DIV>*</DIV>
    ><DIV><FONT face=3DArial size=3D2>public String toString() {</FONT></DIV>
    ><DIV>*</DIV>
    ><DIV><FONT face=3DArial size=3D2>*** retutrn=20
    >fldSuggestionTitle;</FONT></DIV>
    ><DIV>*</DIV>
    ><DIV><FONT face=3DArial size=3D2>}</FONT></DIV>
    ><DIV>*</DIV>
    ><DIV><FONT face=3DArial size=3D2>but then I got a nullpointerexcepiton,

    =
    >if*I=20
    >remove this method it works ok, but*I get tblQuery@484234 in the=20
    >list.</FONT></DIV>
    ><DIV><FONT face=3DArial size=3D2>Do I need to change anything else in =
    >the=20
    >class??</FONT></DIV>
    ><DIV>*</DIV>
    ><DIV><FONT face=3DArial size=3D2>pelle</FONT></DIV>
    ><BLOCKQUOTE=20
    >style=3D"BORDER-LEFT: #000000 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT:

    =
    >0px; PADDING-LEFT: 5px; PADDING-RIGHT: 0px">
    > <DIV>"Paul Clapham" <<A=20
    > href=3D"mailtoclapham@core-mark.com">pclapham@core-mark</A></DIV>
    > <DIV>.com> wrote in message <A=20
    > =
    >href=3D"news:3a06d5ae$1@news.devx.com">news:3a06d5ae$1@news.devx.com</A>.=
    >...</DIV>
    > <DIV><FONT face=3DArial size=3D2>You can put any object into a JList,

    =
    >not only=20
    > Strings.* Whatever object you use, the default behaviour of JList =
    >should=20
    > be to display the result of its "toString()" method.* So change =
    >your=20
    > class to include a method "toString()" that returns =
    >fldSuggestionTitle, and=20
    > add the objects directly to the JList.</FONT></DIV>
    > <DIV>*</DIV>
    > <DIV><FONT face=3DArial size=3D2>PC2</FONT></DIV>
    > <BLOCKQUOTE=20
    > style=3D"BORDER-LEFT: #000000 2px solid; MARGIN-LEFT: 5px; =
    >MARGIN-RIGHT: 0px; PADDING-LEFT: 5px; PADDING-RIGHT: 0px">
    > <DIV>Pelle Henriksson <<A=20
    > =
    >href=3D"mailtoelle_henriksson@home.se">pelle_henriksson@home.se</A>>=
    >=20
    > wrote in message <A=20
    > =
    >href=3D"news:3a067ee7@news.devx.com">news:3a067ee7@news.devx.com</A>...</=
    >DIV>
    > <DIV><FONT face=3DArial size=3D2>Hi !!</FONT></DIV>
    > <DIV>*</DIV>
    > <DIV><FONT face=3DArial size=3D2>Can someone please help me with=20
    > this:</FONT></DIV>
    > <DIV><FONT face=3DArial size=3D2>I store objects in a Vector, and =
    >then i store=20
    > the Vector in a file, here is that object:</FONT></DIV>
    > <DIV>*</DIV>
    > <DIV><FONT face=3D"Courier New" size=3D2>...</FONT></DIV>
    > <DIV>*</DIV>
    > <DIV><FONT face=3DArial size=3D2>I would like to display these =
    >objects in a=20
    > JList but only fldSuggestionTitle* and then fldUserID as a =
    >hidden=20
    > value,</FONT></DIV>
    > <DIV><FONT face=3DArial size=3D2>db is my vector with the tblQury =
    >objects. I=20
    > dont understand how to hide an id and only show the =
    >title.</FONT></DIV>
    > <DIV>*</DIV>
    > <DIV><FONT =
    >size=3D2></DIV></BLOCKQUOTE></BLOCKQUOTE></FONT></BODY></HTML>
    >
    >------=_NextPart_000_0026_01C048B0.2D0AF270--
    >



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links