Article View: borland.public.jbuilder.java.swing
Article #7699Re: JTable Questions
From: James A. Schulz
Date: Wed, 03 Jan 2001 19:11
Date: Wed, 03 Jan 2001 19:11
144 lines
4879 bytes
4879 bytes
Niall,
Thanks. This is very helpful.
James
"Niall Pemberton" <Niall.Pemberton@btinternet.com> wrote in message
news:3a5376e4_2@dnews...
> 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:
<92vtb6$edf4@bornews.inprise.com>Path:
arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!supernews.com!nntp-relay.ihug.net!ihug.co.nz!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!neel.uni2.net!newssuck.uni2.dk!bornews.borland.com!not-for-mailReferences:
<92lbpr$6ro1@bornews.inprise.com> <MPG.14b89345ccc5b0c7989e08@forums.borland.com> <92nedq$66a1@bornews.inprise.com> <MPG.14bb192872e51878989e12@forums.borland.com> <3a5376e4_2@dnews>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:16 PST Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!supernews.com!nntp-relay.ihug.net!ihug.co.nz!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!neel.uni2.net!newssuck.uni2.dk!bornews.borland.com!not-for-mail From: "James A. Schulz" <jaschulz@wiseware.com> Newsgroups: borland.public.jbuilder.java.swing Subject: Re: JTable Questions Date: Wed, 3 Jan 2001 13:11:26 -0600 Organization: Another Netscape Collabra Server User Lines: 146 Message-ID: <92vtb6$edf4@bornews.inprise.com> References: <92lbpr$6ro1@bornews.inprise.com> <MPG.14b89345ccc5b0c7989e08@forums.borland.com> <92nedq$66a1@bornews.inprise.com> <MPG.14bb192872e51878989e12@forums.borland.com> <3a5376e4_2@dnews> 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