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


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

Re: How to color a cell in a JTable

From clusardi2k@aol.com
Newsgroups comp.lang.java.programmer
Subject Re: How to color a cell in a JTable
Date 2012-07-27 10:35 -0700
Organization http://groups.google.com
Message-ID <50ae32ba-6330-451b-85b1-ecf90a5f481c@googlegroups.com> (permalink)
References <0b779276-ce03-47e1-8ab9-0681e2ebee73@googlegroups.com>

Show all headers | View raw


The below project will paint all cells red!

//-------------- PROJECT 2 ---------------------
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication2;

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import javax.swing.*; 
import javax.swing.table.DefaultTableModel; 
import javax.swing.table.TableModel; 
import javax.swing.table.TableRowSorter; 

class CustomRenderer extends DefaultTableCellRenderer  
{     
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)     
    {         
        Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);         
        c.setBackground(new java.awt.Color(255, 72, 72));         
        return c;     
    } 
}

public class testtable extends javax.swing.JFrame {

    /**
     * Creates new form testtable
     */
    public testtable() {
        initComponents();
 
       Runnable runner = new Runnable() { 
        public void run() { 

           Object rows[][] = { 
               {"AMZN", "Amazon", 41.28}, 
               {"EBAY", "eBay", 41.57}, 
               {"GOOG", "Google", 388.33}, 
               {"MSFT", "Microsoft", 26.56}, 
               {"NOK", "Nokia Corp", 17.13}, 
               {"ORCL", "Oracle Corp.", 12.52}, 
               {"SUNW", "Sun Microsystems", 3.86}, 
               {"TWX",  "Time Warner", 17.66}, 
               {"VOD",  "Vodafone Group", 26.02}, 
               {"YHOO", "Yahoo!", 37.69} 
             }; 
           String columns[] = {"Symbol", "Name", "Price"}; 
           TableModel model = 
               new DefaultTableModel(rows, columns) { 
             public Class getColumnClass(int column) { 
               Class returnValue; 
               if ((column >= 0) && (column < getColumnCount())) { 
                 returnValue = getValueAt(0, column).getClass(); 
               } else { 
                 returnValue = Object.class; 
               } 
               return returnValue; 
             } 
           }; 

        jTable1.setDefaultRenderer(String.class, new CustomRenderer()); 

        jTable1.setModel(model); 
 
        jTable1.setRowSorter(sorter);          } 
       }; 
       EventQueue.invokeLater(runner); 
}
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(13, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(testtable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(testtable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(testtable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(testtable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new testtable().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration
}

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


Thread

How to color a cell in a JTable clusardi2k@aol.com - 2012-07-27 10:07 -0700
  Re: How to color a cell in a JTable clusardi2k@aol.com - 2012-07-27 10:35 -0700
  Re: How to color a cell in a JTable markspace <-@.> - 2012-07-27 13:57 -0700

csiph-web