Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #16680

Hints Appreciated: How to fix a DefaultTableCellRenderer and getTableCellRendererComponent Clash!

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
From clusardi2k@aol.com
Newsgroups comp.lang.java.programmer
Subject Hints Appreciated: How to fix a DefaultTableCellRenderer and getTableCellRendererComponent Clash!
Date Mon, 30 Jul 2012 13:01:51 -0700 (PDT)
Organization http://groups.google.com
Lines 105
Message-ID <307a1255-329c-47dc-9ae3-92a7bc2bc726@googlegroups.com> (permalink)
NNTP-Posting-Host 198.151.13.60
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
X-Trace posting.google.com 1343678511 17326 127.0.0.1 (30 Jul 2012 20:01:51 GMT)
X-Complaints-To groups-abuse@google.com
NNTP-Posting-Date Mon, 30 Jul 2012 20:01:51 +0000 (UTC)
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=198.151.13.60; posting-account=r24XpwkAAABfAJg5TJRsTScS4AL5MjOT
User-Agent G2/1.0
X-Received-Bytes 4208
Xref csiph.com comp.lang.java.programmer:16680

Show key headers only | View raw


//How do I get the below project to use the tooltips and color the rows cyan 
//and gray. If I comment output the below indicated three lines I get column 
//coloring, but lose tooltips. 

Thank you,

// Reference:
// http://www.roseindia.net/java/example/java/swing/CustomCellRenderer.shtml

package customcellrenderer;

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;

public class CustomCellRenderer 
{
   JTable table;
   TableColumn tcol;

   public static void main(String[] args) 
   {
      new CustomCellRenderer();
   }
  
    public static void StudentColTooltip(TableColumn OnLine_Col, String text)
    {
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        
        renderer.setToolTipText(text);
        OnLine_Col.setCellRenderer(renderer);
    }
    
  public CustomCellRenderer()
  {
      JFrame frame = new JFrame("Creating a Custom Cell Reanderer!");

      JPanel panel = new JPanel();

      String data[][] = {{"Vinod","Computer","3"},
            {"Rahul","History","2"},
            {"Manoj","Biology","4"},
            {"Sanjay","PSD","5"}};

      String col []           = {"Name","Course","Year"};
      DefaultTableModel model = new DefaultTableModel(data,col);

      table = new JTable(model);

      tcol = table.getColumnModel().getColumn(0);
      tcol.setCellRenderer(new CustomTableCellRenderer());
      tcol = table.getColumnModel().getColumn(1);
      tcol.setCellRenderer(new CustomTableCellRenderer());
      tcol = table.getColumnModel().getColumn(2);
      tcol.setCellRenderer(new CustomTableCellRenderer());

      JTableHeader header = table.getTableHeader();
      header.setBackground(Color.yellow);

      JScrollPane pane = new JScrollPane(table);
      panel.add(pane);

      frame.add(panel);
      frame.setSize(500,150);
      frame.setUndecorated(true);

      frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
  }

  public class CustomTableCellRenderer extends DefaultTableCellRenderer
  {
      public Component getTableCellRendererComponent (JTable table, 
            Object obj, boolean isSelected, boolean hasFocus, int row, int column) 
      {
          Component cell = super.getTableCellRendererComponent(
                table, obj, isSelected, hasFocus, row, column);

          if ( isSelected ) 
          {
              cell.setBackground(Color.green);
          }
          else 
          {
              if ( row % 2 == 0 ) 
              {
                  cell.setBackground(Color.cyan);
              }
              else 
              {
                  cell.setBackground(Color.lightGray);
              }
          }

          //Commenting out the below 3 lines yields cyan and gray columns
          StudentColTooltip(table.getColumnModel().getColumn(0),"Student's Last Name");
          StudentColTooltip(table.getColumnModel().getColumn(1),"Student's Major");
          StudentColTooltip(table.getColumnModel().getColumn(2),"Year In School"); 
            
          return cell;
  
      }
  }
}

Back to comp.lang.java.programmer | Previous | Next | Find similar | Unroll thread


Thread

Hints Appreciated: How to fix a DefaultTableCellRenderer and getTableCellRendererComponent Clash! clusardi2k@aol.com - 2012-07-30 13:01 -0700

csiph-web