Article View: borland.public.jbuilder.java.swing
Article #7809Re: JTable Questions
From: Niall Pemberton
Date: Fri, 29 Dec 2000 22:51
Date: Fri, 29 Dec 2000 22:51
93 lines
3327 bytes
3327 bytes
James,
your table cell renderer is strange...I'm not an expert but I don't think
you need a setValue() method in it ... you need to implement a
getTableCellRendererComponent() method as below, from your earlier post you
can determine the cell coordinates which are passed as arguments to the
method. Otherwise your PcCharacter could be a class which contains the
character value and colurs associated with it.
public class PcCellRenderer extends DefaultTableCellRenderer {
private JLabel blankLabel;
private JLabel charLabel;
private PcCharacter c;
public Component getTableCellRendererComponent(JTable table, Object obj,
boolean isSelected, boolean hasFocus, int row, int column) {
if (obj == null)
return blankLabel;
c = (PcCharacter)obj;
charLabel.setText(c.getValue());
charLabel.setForeground(c.getForeground());
charLabel.setBackground(c.getBackground());
return charLabel;
}
}
On the subject of initialising the values, you don't have to do it. Set up
your own Table Model as below, using a HashMap to only store values when you
actually have one:
public abstract class PcCellModel extends AbstractTableModel {
private Map objectMatrix = new HashMap();
private Point objectIndex = new Point(0,0);
protected int maxCol = 55;
protected int maxRow = 25;
public PcCellModel () {
}
public PcCellModel (int row, int col) {
maxRow = row;
maxCol = col;
}
public Object getValueAt(int row, int col) {
objectIndex.x = row;
objectIndex.y = col;
if (objectMatrix.get(objectIndex) == null)
return null;
return (PcCharacter)objectMatrix.get(objectIndex);
}
public void setValueAt(Object setObject, int row, int col) {
if (setObject == null) {
objectMatrix.remove(new Point(row, col));
}
else {
maxCol = Math.max(col, maxCol);
maxRow = Math.max(row, maxRow);
objectMatrix.put(new Point(row, col), setObject);
}
}
public int getRowCount() {
return maxRow;
}
public int getColumnCount() {
return maxCol;
}
public int getMaxColumn() {
return maxCol;
}
public int getMaxRow() {
return maxRow;
}
public boolean isCellEditable(int row, int col) {
return false;
}
}
I kind of cobbled this together but I hope it helps.
Niall
Technical Details
Message-ID:
<3a4fb895_1@dnews>Path:
arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!supernews.com!newsfeed.mesh.ad.jp!news.maxwell.syr.edu!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!bornews.borland.com!dnews!62.7.34.190References:
<92lbpr$6ro1@bornews.inprise.com> <MPG.14b89345ccc5b0c7989e08@forums.borland.com> <92nedq$66a1@bornews.inprise.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: 2000-12-31 14:59:01 PST From: "Niall Pemberton" <Niall.Pemberton@btinternet.com> Newsgroups: borland.public.jbuilder.java.swing References: <92lbpr$6ro1@bornews.inprise.com> <MPG.14b89345ccc5b0c7989e08@forums.borland.com> <92nedq$66a1@bornews.inprise.com> Subject: Re: JTable Questions Date: Fri, 29 Dec 2000 22:51:29 -0000 Lines: 96 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2615.200 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200 NNTP-Posting-Host: 62.7.34.190 Message-ID: <3a4fb895_1@dnews> X-Trace: 31 Dec 2000 14:52:05 -0800, 62.7.34.190 Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!supernews.com!newsfeed.mesh.ad.jp!news.maxwell.syr.edu!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!bornews.borland.com!dnews!62.7.34.190