🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: JTable CellRenderer Quetion

#7696
From: James A. Schulz
Date: Fri, 29 Dec 2000 20:29
218 lines
7484 bytes
John,

Yes, this is what I am currently using (and I do not see the null pointer
exception you describe).  Below is the whole of what I have:

package workbench;

import java.awt.*;
import java.awt.event.*;
import java.awt.font.FontRenderContext;
import java.applet.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;

/**
 * Title:        Work Bench Project
 * Description:
 * Copyright:    Copyright (c) 2000
 * Company:      WiseWare
 * @author JAS
 * @version 1.0
 */

public class Applet1 extends JApplet {
  boolean isStandalone = false;
  char curlChar = '\u007E';
  char wedgeChar = '\u2228';
  char dotChar = '\u2219';
  char horseshoeChar = '\u2283';
  char triplebarChar = '\u2261';
  JPanel jPanel1 = new JPanel();
  BorderLayout borderLayout1 = new BorderLayout();
  JPanel jPanel2 = new JPanel();
  JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();
  JScrollPane jScrollPane1 = new JScrollPane();
  Border border1;
  Border border2;
  GridLayout gridLayout1 = new GridLayout();
/**
  public class PropcalculatorTableModel extends AbstractTableModel {
    String[][] data = {{"","","","","","","","","",""},
                       {"","","","","","","","","",""}};
    String[] headers = {"","","","","","","","","",""};
    public int getRowCount() {return data.length;};
    public int getColumnCount() {return headers.length;};
    public Object getValueAt(int r, int c) {return data[r][c];};
    public void setValueAt(Object s, int r, int c) {data[r][c] =
(String)s;};
  };
*/
  //JTable jTable1 = new JTable(new PropcalculatorTableModel());

  public class PropcalculatorCellRenderer extends DefaultTableCellRenderer {
    public PropcalculatorCellRenderer(int align) {
      this.align = align;
    }
    protected void setValue(Object value) {
      super.setValue(value);
      setHorizontalAlignment(align);
    }
    protected int align;
  }

  JTable jTable1 = new JTable(new String[][]
{{"","","","","","","","","",""},

{"","","","","","","","","",""}},
                              new String[]
{"","","","","","","","","",""});

  /**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 Applet1() {
  }
  /**Initialize the applet*/
  public void init() {
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  /**Component initialization*/
  private void jbInit() throws Exception {
    border1 = BorderFactory.createEmptyBorder();
    border2 = new EtchedBorder(EtchedBorder.RAISED,Color.white,new
Color(134, 134, 134));
    this.setSize(new Dimension(160,160));
    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);
      }
    });
    jScrollPane1.setBorder(border1);
    jPanel2.setBorder(border2);
    jPanel2.setLayout(gridLayout1);
    jTable1.setEnabled(false);
    jTable1.setFont(new java.awt.Font("Monospaced",1,14));
    jTable1.setRequestFocusEnabled(false);
    jTable1.setRowSelectionAllowed(false);
    this.getContentPane().add(jPanel1, BorderLayout.CENTER);
    jPanel1.add(jPanel2, BorderLayout.SOUTH);
    jPanel2.add(jButton1, null);
    jPanel2.add(jButton2, null);
    jPanel1.add(jScrollPane1, BorderLayout.CENTER);
    jScrollPane1.getViewport().add(jTable1, null);
    jTable1.setDefaultRenderer(null, new
PropcalculatorCellRenderer(SwingConstants.CENTER));
  }
  /**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 jButton1_actionPerformed(ActionEvent e) {
    String s = new String();
    s = s + horseshoeChar;
    if (jTable1.getValueAt(0,0).equals("")) {
      jTable1.setValueAt(s.toString(),0,0);
    }
    else {
      jTable1.setValueAt("",0,0);
    }
  }

  void jButton2_actionPerformed(ActionEvent e) {
    if (jTable1.getValueAt(0,1).equals("")) {
      jTable1.setValueAt("S",0,1);
    }
    else {
      jTable1.setValueAt("",0,1);
    }
  }
}

thanks,

James





"John McGrath [TeamB]" <ng-b@JPMcGrath.net> wrote in message
news:MPG.14b60c60bdb5b7b2989dfd@forums.borland.com...
> On Thu, 28 Dec 2000 07:10:41 -0600, James A. Schulz wrote:
>
> > Sorry to have bothered you with such a simple problem,
> > but I am very new at this.
>
> It's not a bother, as that is what we are here for, and we are all new
> at something.
>
> > my renderer does not seem to have any effect.
>
> My guess is that you are not registering your TableCellrenderer
> properly, so it is not being used.  I noticed the following line in
> your original post.
>
>     jTable1.setDefaultRenderer(null,
>         new MyCellRenderer(SwingConstants.CENTER));
>
> The first argument to setDefaultRenderer() is the Class for which it
> will be used.  I took a quick glance at the code, and it looks like you
> should get a NullPointerException when you execute this.
>
> Is this the code you are currently using?
>
> --
> Regards,
>
> John McGrath [TeamB]
>
> ---------------------------------------------------
> Before sending me e-mail, please read:
>     http://www.JPMcGrath.net/newsgroups/e-mail.html
Technical Details
Message-ID: <92is13$fef7@bornews.inprise.com>
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!supernews.com!newsfeed.online.be!newsfeed01.sul.t-online.de!t-online.de!fr.clara.net!heighliner.fr.clara.net!news.imp.ch!news-zh.switch.ch!newsfeed-zh.ip-plus.net!news.ip-plus.net!bornews.borland.com!not-for-mail
References: <925vkp$luo7@bornews.inprise.com> <MPG.14b49e66609c4485989df1@forums.borland.com> <92fdv0$ohp7@bornews.inprise.com> <MPG.14b60c60bdb5b7b2989dfd@forums.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,78ede608538ed371
X-Google-Attributes: gid111d45,public
X-Google-ArrivalTime: 2000-12-29 12:30:29 PST
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!supernews.com!newsfeed.online.be!newsfeed01.sul.t-online.de!t-online.de!fr.clara.net!heighliner.fr.clara.net!news.imp.ch!news-zh.switch.ch!newsfeed-zh.ip-plus.net!news.ip-plus.net!bornews.borland.com!not-for-mail
From: "James A. Schulz" <jaschulz@wiseware.com>
Newsgroups: borland.public.jbuilder.java.swing
Subject: Re: JTable CellRenderer Quetion
Date: Fri, 29 Dec 2000 14:29:06 -0600
Organization: Another Netscape Collabra Server User
Lines: 220
Message-ID: <92is13$fef7@bornews.inprise.com>
References: <925vkp$luo7@bornews.inprise.com> <MPG.14b49e66609c4485989df1@forums.borland.com> <92fdv0$ohp7@bornews.inprise.com> <MPG.14b60c60bdb5b7b2989dfd@forums.borland.com>
Reply-To: "James A. Schulz" <jaschulz@wiseware.com>
NNTP-Posting-Host: 165.121.198.13
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