Article View: borland.public.jbuilder.java.swing
Article #7698Re: JTable Questions
From: Niall Pemberton
Date: Wed, 03 Jan 2001 19:00
Date: Wed, 03 Jan 2001 19:00
128 lines
4337 bytes
4337 bytes
James,
As per Johns suggestion here are examples of model and renderer for you to
use. There are 2/3 ,methods in the model in initialize the array - either
work, perhaps John wil give an opinion of which is better.
Also I tried the renderer with both a JLabel and JTextField - the foreground
color came out OK with both but the background color only worked with the
JTextField - not sure why.
Niall
/*-------------------------------Creating the
table---------------------------*/
// Create Table
model = new PcCellModel();
jTable1 = new JTable(model);
jTable1.setDefaultRenderer(Character.class, new PcCellRenderer());
// Load Values
model.setValueAt(new Character('a'), 0, 0);
// ..... etc, etc
// Initialize
model.initialize1();
// or . . .
model.initialize1(55, 25);
// or . . .
model.initialize2();
/*-------------------------------Model ---------------------------*/
import java.util.*;
import java.awt.*;
import javax.swing.table.*;
public class PcCellModel extends AbstractTableModel {
private Character[][] characterMatrix;
private static final int DEFAULTROW = 5;
private static final int DEFAULTCOL = 25;
protected int maxRow;
protected int maxCol;
public PcCellModel () {
this(DEFAULTROW, DEFAULTCOL);
}
public PcCellModel (int row, int col) {
maxRow = row;
maxCol = col;
characterMatrix = new Character[maxRow][maxCol];
}
public Object getValueAt(int row, int col) {
return characterMatrix[row][col];
}
public Class getColumnClass(int col) {
return Character.class;
}
public void setValueAt(Object obj, int row, int col) {
if (obj == null)
characterMatrix[row][col] = null;
else
characterMatrix[row][col] = (Character)obj;
}
public void initialize1(int row, int col) {
maxRow = row;
maxCol = col;
characterMatrix = new Character[maxRow][maxCol];
fireTableStructureChanged();
}
public void initialize1() {
characterMatrix = new Character[maxRow][maxCol];
fireTableDataChanged();
}
public void initialize2() {
for (int i=0;i < maxRow;i++) {
for (int j=0;j < maxCol;j++) {
characterMatrix[i][j] = null;
}
}
fireTableDataChanged();
}
public int getRowCount() {
return maxRow;
}
public int getColumnCount() {
return maxCol;
}
public boolean isCellEditable(int row, int col) {
return false;
}
}
/*-------------------------------Renderer---------------------------*/
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class PcCellRenderer implements TableCellRenderer {
private static JLabel blankLabel = new JLabel();
private JLabel text = new JLabel();
// private JTextField text = new JTextField();
public Component getTableCellRendererComponent(JTable table, Object obj,
boolean isSelected, boolean hasFocus, int row, int column) {
if (obj == null)
return blankLabel;
text.setText(((Character)obj).toString());
if (row == 0 || row == 2 || row == 4) {
text.setForeground(Color.red);
text.setBackground(Color.white);
} else {
text.setForeground(Color.blue);
text.setBackground(Color.yellow);
}
return text;
}
}
Technical Details
Message-ID:
<3a5376e4_2@dnews>Path:
arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!hub.org!hub.org!PeerFeed.KWeb.on.ca!bornews.borland.com!dnews!213.1.107.223References:
<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-03 11:12:04 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> <MPG.14bb192872e51878989e12@forums.borland.com> Subject: Re: JTable Questions Date: Wed, 3 Jan 2001 19:00:06 -0000 Lines: 135 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: 213.1.107.223 Message-ID: <3a5376e4_2@dnews> X-Trace: 3 Jan 2001 11:00:52 -0800, 213.1.107.223 Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!hub.org!hub.org!PeerFeed.KWeb.on.ca!bornews.borland.com!dnews!213.1.107.223