🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: JTable Questions

#7700
From: James A. Schulz
Date: Wed, 03 Jan 2001 21:22
220 lines
7189 bytes
Niall,

Well I tried your strategy (see below) but the compiler complains that it
cannot see the "initialize"  method.  I cannot understand why.  Can you give
me a clue?  Thanks

James

package testbed;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.awt.geom.*;
import javax.swing.table.*;

/**
 * Title:        Test Bed
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:      WiseWare
 * @author JAS
 * @version 1.0
 */

public class TBApplet extends JApplet {
  boolean isStandalone = false;
  JPanel jPanel1 = new JPanel();
  BorderLayout borderLayout1 = new BorderLayout();
  JScrollPane jScrollPane1 = new JScrollPane();

  public class PcCharacter {
    public PcCharacter() {
      this.ch = '\u0000';
      this.fg = Color.black;
      this.bg = Color.white;
    }

    public PcCharacter(char ch) {
      this.ch = ch;
      this.fg = Color.black;
      this.bg = Color.white;
    };

    public PcCharacter(char ch, Color fg, Color bg) {
      this.ch = ch;
      this.fg = fg;
      this.bg = bg;
    }

    public String toString() {
      String s = new String();
      s = s + this.ch;
      return s;
    }

    public Color getForeground() {
      return this.fg;
    };

    public Color getBackground() {
      return this.bg;
    };

    protected char ch;
    protected Color fg;
    protected Color bg;
  };

  public class PcTableCellRenderer implements TableCellRenderer {
    private JLabel text = new JLabel();
    private JLabel blankLabel = new JLabel();
    private PcCharacter ch;
    public Component getTableCellRendererComponent(JTable table, Object
value,
                                                   boolean isSelected,
                                                   boolean hasFocus,
                                                   int r, int c) {
      if (value == null) {
        return blankLabel;
      } else {
        ch = (PcCharacter)value;
        text.setText(value.toString());
        text.setForeground(ch.getForeground());
        text.setBackground(ch.getBackground());
        return text;
      };
    };
  };

  TableModel PcTableModel = new AbstractTableModel() {
    private static final int MaxRows = 5;
    private static final int MaxCols = 15;
    private PcCharacter[][] pcCharacterMatrix = new
PcCharacter[MaxRows][MaxCols];
    public int getRowCount() { return MaxRows;};
    public int getColumnCount() {return MaxCols;};
    public Object getValueAt(int r, int c) {return
pcCharacterMatrix[r][c];};
    public String getColumnName(int c) {return "";};
    public void setValueAt(Object value, int r, int c) {
      if (value == null) {
        pcCharacterMatrix[r][c] = null;
      } else {
         pcCharacterMatrix[r][c] = (PcCharacter)value;
      };
      fireTableCellUpdated(r,c);
    };

    public boolean isCellEditable(int r, int c) {
      return false;
    };

    public void initializePcCharacterMatrix() {
      pcCharacterMatrix = new PcCharacter[MaxRows][MaxCols];
      fireTableDataChanged();
    };
  };

  JTable jTable1 = new JTable(PcTableModel);
  JPanel jPanel2 = new JPanel();
  JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();
  JButton jButton3 = new JButton();

  /**Get a parameter value*/
  public String getParameter(String key, String def) {
    return isStandalone ? System.getProperty(key, def) :
      (getParameter(key) != null ? getParameter(key) : def);
  }

  /**Construct the applet*/
  public TBApplet() {
  }
  /**Initialize the applet*/
  public void init() {
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  /**Component initialization*/
  private void jbInit() throws Exception {
    this.setSize(new Dimension(400,300));
    jPanel1.setBorder(BorderFactory.createEtchedBorder());
    jPanel1.setLayout(borderLayout1);
    jButton1.setText("jButton1");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton1_actionPerformed(e);
      }
    });
    jButton2.setText("jButton2");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton2_actionPerformed(e);
      }
    });
    jButton3.setText("jButton3");
    jButton3.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton3_actionPerformed(e);
      }
    });
    this.getContentPane().add(jPanel1, BorderLayout.CENTER);
    jPanel1.add(jScrollPane1, BorderLayout.CENTER);
    jPanel1.add(jPanel2, BorderLayout.SOUTH);
    jPanel2.add(jButton1, null);
    jPanel2.add(jButton2, null);
    jPanel2.add(jButton3, null);
    jScrollPane1.getViewport().add(jTable1, null);
    jTable1.setDefaultRenderer(Character.class, new PcTableCellRenderer());
  }
  /**Start the applet*/
  public void start() {
  }
  /**Stop the applet*/
  public void stop() {
  }
  /**Destroy the applet*/
  public void destroy() {
  }
  /**Get Applet information*/
  public String getAppletInfo() {
    return "Applet Information";
  }
  /**Get parameter info*/
  public String[][] getParameterInfo() {
    return null;
  }

  //static initializer for setting look & feel
  static {
    try {
      //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

//UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName())
;
    }
    catch(Exception e) {
    }
  }

  void jButton3_actionPerformed(ActionEvent e) {
    jTable1.getModel().initializePcCharacterMatrix();
  }

  void jButton1_actionPerformed(ActionEvent e) {
    PcCharacter c = new PcCharacter('H');
    jTable1.setValueAt(c,1,1);
  }

  void jButton2_actionPerformed(ActionEvent e) {
    PcCharacter c = new PcCharacter('B');
    jTable1.setValueAt(c,2,2);
  }
};
Technical Details
Message-ID: <93050q$rvc2@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-mail
References: <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 13:22:00 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 15:22:26 -0600
Organization: Another Netscape Collabra Server User
Lines: 222
Message-ID: <93050q$rvc2@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