Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Other Java > Java GUI
|
Java GUI Discussions specific to programming Java GUI.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java GUI section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 2nd, 2005, 04:58 PM
Authorized User
 
Join Date: Mar 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default JTable Cell Renderer

Hi all,

I am a kind of new and not using Java Swing often. Currently I am trying to provide a table (JTable) display with "stripping" on even or odd row to create easy reading. The following shows part of the codes on the JTable setup

public class ......
{
  .................
  // setup the AbstractTableModel interfaces
  MyTableModel tm = new MyTableModel();
  JTable tb = new Jtable();
  tb.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  tb.setAutoCreateColumnsFromModel( false );
  tb.setModel( objTableModel );
  // set up the table Column property
  for (int k = 0; k < tm.getColumnCount(); k++ )
  {
    DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
    renderer.setHorizontalAlignment(tm.m_objColumns[k].m_intColAlignment);
    TableColumn column = new TableColumn(k,tm.m_objColumns[k].m_intColWidth,
       renderer, null);
    tb.addColumn(column);
   }
   // put it in the scroll pane
   JScrollPane objScrollPanel = new JScrollPane(tb);
   getContentPane().add( objScrollPanel, BorderLayout.CENTER );
   ....................................
   ....................................
}

I know some of the column data setup is not shown to minimize coding here.

I tried a number of ways to incorporate the "DefaultTableCellRenderer" codes to have the alternated row to display with different color for the "stripping" effect but FAILED. Please help and thanks in advance.



 
Old August 3rd, 2005, 10:28 AM
Authorized User
 
Join Date: Aug 2005
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You need to extend the DefaultTableCellRenderer and handle setting the color.

Here is what you need to do for setting alternate strips of red.

For more examples visit,
http://www.javareference.com/jrexamp...jsp?rootcat=10

public class ColorCellRenderer extends DefaultTableCellRenderer
{
    public Component getTableCellRendererComponent
     (JTable table, Object value, boolean isSelected,
     boolean hasFocus, int row, int column)
    {
        super.getTableCellRendererComponent
         (table, value, isSelected, hasFocus, row, column);

        if (!isSelected)
        {
            if (row % 2 == 0)
                setBackground(Color.red);
            else
                setBackground(table.getBackground());
        }

        return this;
    }
}

Rahul.


[email protected]
--------------------------
http://www.javareference.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to change the colour of a cell in a jtable Tomi Java GUI 1 April 25th, 2007 03:35 AM
JTable and DataBase: bringing data to JTable Rashmirathi Java Databases 0 November 19th, 2006 01:53 AM
SecurityException - Custom Renderer CSV extension PrimalBlaze BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 0 September 21st, 2005 09:05 PM
Questions about JTable chtiteuf Java GUI 0 May 22nd, 2005 07:02 AM
How to implement on searching a value in JTable lxu Apache Tomcat 0 September 28th, 2004 08:18 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.