🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #7835

Re: JTable Questions

#7835
From: James A. Schulz
Date: Wed, 03 Jan 2001 05:49
127 lines
5772 bytes
Rich,

If I follow your suggestion(s) about initializing etc. how would I
"reinitialize" my table (i.e. "clear" the table)?

thanks

James

"John McGrath [TeamB]" <ng-b@JPMcGrath.net> wrote in message
news:MPG.14bb192872e51878989e12@forums.borland.com...
> On Sun, 31 Dec 2000 08:07:40 -0600, James A. Schulz wrote:
>
> > My table is fairly big, 55x25).  I want to initialize all the
> > cells to hold the same (blank) character, but I certainly don't
> > want to write out those vectors.
>
> > I tried using a for loop in the context to a custom tablemodel
> > declaration, like this:
> >
> >   TableModel tm = new AbstractTableModel() {
> >     PcCharacter[][] data =
> >       {{blankPcCharacter,blankPcCharacter,blankPcCharacter},
> >        {blankPcCharacter,blankPcCharacter,blankPcCharacter}};
>     :
> >     for (i = 0;i < 2;i++){
> >       for(ii=0;ii<3;ii++){
> >         data[i][ii] = blankPcCharacter
> >       }
> >     }
> >   };
> >
> > but the compiler doesn't like thiis.  How do I use for loops
> > to initialize my table:
>
> I can see two things that would cause the compiler to object.  First,
> you reference "blankPcCharacter", but I do not see a declaration for
> it.  Second, the statement "data[i][ii] = ..." does not end in a
> semicolon.
>
> What your code does is declare the table with an initializer, which
> would assign values to your table, and then replace the values in the
> loop below.  There should be no need to provide an initializer if you
> are going to redo this in the loop.
>
> The code below is close to what you want, but you have to realize how
> multiple dimension arrays work in Java.  Actually there is no such
> thing as a multiple dimension array;  instead, you have use an array of
> arrays.
>
> If you declare an array of objects, it will be initialized to all null
> references, so you have to allocate objects and store the references in
> the array.  Given the following code:
>
>     PcCharacter[][] data;
>
> This declares a reference to an array of (array of PcCharacter) which
> is initialized to null.  You can initialize it with:
>
>     PcCharacter[][] data = new PcCharacter[25][];
>
> Now you have an array of 25 references to (array of PcCharacter), but
> each of those 25 references will be null, so you must initialize each
> of those arrays as well.
>
>     PcCharacter[][] data = new PcCharacter[25][];
>     for ( int i = 0;  i < 25;  ++ i ) {
>         data[i] = new PcCharacter[80];
>     }
>
> Now you have an array of 25 references to arrays of 80 references to
> PcCharacter.  But the references to PcCharacter are null.  So you can
> initialize those with:
>
>     PcCharacter[][] data = new PcCharacter[25][];
>     for ( int i = 0;  i < 25;  ++ i ) {
>         data[i] = new PcCharacter[55];
>         for ( int j = 0;  j < 55;  ++ j ) {
>             data[i][j] = blankPcCharacter;
>         }
>     }
>
> Now you have a 55 x 25 array of references to PcCharacter, but note
> that they all reference the *same* PcCharacter object.  If you change
> the PcCharacter in one cell, it will change the PcCharacters in all
> cells, since they are all the same PcCharacter.  So you may want to
> change this code to read:
>
>             data[i][j] = new PcCharacter();
>
> But instead of doing that, you could leave the values null, and just
> make sure the rest of your code treats the null value as meaning empty.
> So your renderer would change as follows:
>
>       if ( c != null ) {
>          setForeground(c.getForeground());
>          setBackground(c.getBackground());
>       } else {
>          setForeground(DEFAULT_FOREGROUND);
>          setBackground(DEFAULT_BACKGROUND);
>       }
>
> Depending on how sparse your table is, that could save allocating a lot
> of objects.
>
>
> > Also, you will note a call to repaint() in my tablemodel
> > declaration (above).  With out this setValueAt does not
> > paint the cell, my question is about whether this call to
> > repaint() is the best, most efficient way to handle this.
>
> No.  You are repainting the entire table when any cell changes.  The
> cell is not repainted because you have not informed the JTable that it
> has changed.  There are a number for fireXXX() methods provided by
> AbstractTableModel for informing listeners of changes.  For a single
> cell change, the way to do this is:
>
>     fireTableCellUpdated( row, column );
>
> --
> Regards,
>
> John McGrath [TeamB]
>
> ---------------------------------------------------
> Before sending me e-mail, please read:
>     http://www.JPMcGrath.net/newsgroups/e-mail.html
Technical Details
Message-ID: <92ueaj$in624@bornews.inprise.com>
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!cyclone-sjo1.usenetserver.com!news-out.usenetserver.com!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!bornews.borland.com!not-for-mail
References: <92lbpr$6ro1@bornews.inprise.com> <MPG.14b89345ccc5b0c7989e08@forums.borland.com> <92nedq$66a1@bornews.inprise.com> <MPG.14bb192872e51878989e12@forums.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,7fa15b59fdc30449
X-Google-Attributes: gid111d45,public
X-Google-ArrivalTime: 2001-01-02 21:54:02 PST
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!cyclone-sjo1.usenetserver.com!news-out.usenetserver.com!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!bornews.borland.com!not-for-mail
From: "James A. Schulz" <jaschulz@wiseware.com>
Newsgroups: borland.public.jbuilder.java.swing
Subject: Re: JTable Questions
Date: Tue, 2 Jan 2001 23:49:04 -0600
Organization: Another Netscape Collabra Server User
Lines: 129
Message-ID: <92ueaj$in624@bornews.inprise.com>
References: <92lbpr$6ro1@bornews.inprise.com> <MPG.14b89345ccc5b0c7989e08@forums.borland.com> <92nedq$66a1@bornews.inprise.com> <MPG.14bb192872e51878989e12@forums.borland.com>
Reply-To: "James A. Schulz" <jaschulz@wiseware.com>
NNTP-Posting-Host: user-vcautgu.dsl.mindspring.com
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4522.1200
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200