Results 1 to 8 of 8
  1. #1
    dinu is offline Member
    Join Date
    Jan 2013
    Posts
    31
    Rep Power
    0

    Default How Do I render color for each row in JTable ?

    I have the following code .. I want to add color(red) to the rows only when checkboxes are checked.
    THis should apply for multiple selections also.

    {code}

    import javax.swing.*;
    import javax.swing.table.*;

    public class TableCheckBox extends JFrame {

    private static final long serialVersionUID = 1L;
    private JTable table;

    public TableCheckBox() {
    Object[] columnNames = {"Type", "Company", "Shares", "Price", "Boolean"};
    Object[][] data = {
    {"Buy", "IBM", new Integer(1000), new Double(80.50), false},
    {"Sell", "MicroSoft", new Integer(2000), new Double(6.25), true},
    {"Sell", "Apple", new Integer(3000), new Double(7.35), true},
    {"Buy", "Nortel", new Integer(4000), new Double(20.00), false}
    };
    DefaultTableModel model = new DefaultTableModel(data, columnNames);
    table = new JTable(model) {

    private static final long serialVersionUID = 1L;

    /*@Override
    public Class getColumnClass(int column) {
    return getValueAt(0, column).getClass();
    }*/
    @Override
    public Class getColumnClass(int column) {
    switch (column) {
    case 0:
    return String.class;
    case 1:
    return String.class;
    case 2:
    return Integer.class;
    case 3:
    return Double.class;
    default:
    return Boolean.class;
    }
    }
    };
    table.setPreferredScrollableViewportSize(table.get PreferredSize());
    JScrollPane scrollPane = new JScrollPane(table);
    getContentPane().add(scrollPane);
    }

    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {

    @Override
    public void run() {
    TableCheckBox frame = new TableCheckBox();
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.pack();
    frame.setLocation(150, 150);
    frame.setVisible(true);
    }
    });
    }
    }
    {code}

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: How Do I render color for each row in JTable ?

    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    dinu is offline Member
    Join Date
    Jan 2013
    Posts
    31
    Rep Power
    0

    Default Re: How Do I render color for each row in JTable ?

    how do i add a custom renderer for a checkbox ?

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: How Do I render color for each row in JTable ?

    You've read and understood the whole tutorial in two minutes? Magic!

    Read the tutorial, implement a renderer and come back with specific questions if you get stuck.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    dinu is offline Member
    Join Date
    Jan 2013
    Posts
    31
    Rep Power
    0

    Default Re: How Do I render color for each row in JTable ?

    okey.... il read, n come back ...

  6. #6
    dinu is offline Member
    Join Date
    Jan 2013
    Posts
    31
    Rep Power
    0

    Default Re: How Do I render color for each row in JTable ?

    hi,
    I implemented a custom cell renderer as such but i have a problems :
    1> when the checkbox is selected the row color dosn't change

    Java Code:
    package moxaclient;
    
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import javax.swing.DefaultCellEditor;
    import javax.swing.JCheckBox;
    import javax.swing.JTable;
    
    public class CustomTableCellRenderer extends DefaultCellEditor implements ItemListener {
    
    private static final long serialVersionUID = 1L;
    private JCheckBox checkBox;
    private JTable table1;
    private int row;
    private int column;
    Object abc="null";
    private String Name;
    Component comm=null;
    
    public CustomTableCellRenderer(JCheckBox checkBox,String name2) {
        super(checkBox);
        this.checkBox = checkBox;
        this.checkBox.addItemListener(this);
        this.Name=name2;   
    }
    
    public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected,int row, int column) 
    {
        this.row = row;
        this.table1=table;
        this.column = column;
        checkBox.setSelected((Boolean)value);
        return super.getTableCellEditorComponent(table, value, isSelected,row, column);
    }
    
    public void itemStateChanged(ItemEvent e) 
       {
       this.fireEditingStopped(); 
        //System.out.println("Item Changed " + row + " value is: " + checkBox.isSelected());
         //System.out.println("Item Changed " + column + " value is: " + checkBox.isSelected());
              String S1 =(String) table1.getValueAt(row, 0);
              String S2 =(String) table1.getValueAt(row, 1);
              int s3=(int) table1.getValueAt(row, 2);
              Double Value =(Double) table1.getValueAt(row, 3);
              Boolean select=(Boolean) table1.getValueAt(row,4);
           if (Boolean.TRUE.equals(select)) 
           {
    	    abc += S1+"\t"+s2+"\t"+s3+"\t"+value+"\t";
    	 checkBox.setBackground(Color.black);                           // COLOR THE ROW dosnt work
    }
       }
    Last edited by dinu; 04-12-2013 at 02:53 PM.

  7. #7
    dinu is offline Member
    Join Date
    Jan 2013
    Posts
    31
    Rep Power
    0

    Default Re: How Do I render color for each row in JTable ?

    any1

  8. #8
    dinu is offline Member
    Join Date
    Jan 2013
    Posts
    31
    Rep Power
    0

    Default Re: How Do I render color for each row in JTable ?

    ping
    Quote Originally Posted by dinu View Post
    hi,
    I implemented a custom cell renderer as such but i have a problems :
    1> when the checkbox is selected the row color dosn't change

    Java Code:
    package moxaclient;
    
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import javax.swing.DefaultCellEditor;
    import javax.swing.JCheckBox;
    import javax.swing.JTable;
    
    public class CustomTableCellRenderer extends DefaultCellEditor implements ItemListener {
    
    private static final long serialVersionUID = 1L;
    private JCheckBox checkBox;
    private JTable table1;
    private int row;
    private int column;
    Object abc="null";
    private String Name;
    Component comm=null;
    
    public CustomTableCellRenderer(JCheckBox checkBox,String name2) {
        super(checkBox);
        this.checkBox = checkBox;
        this.checkBox.addItemListener(this);
        this.Name=name2;   
    }
    
    public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected,int row, int column) 
    {
        this.row = row;
        this.table1=table;
        this.column = column;
        checkBox.setSelected((Boolean)value);
        return super.getTableCellEditorComponent(table, value, isSelected,row, column);
    }
    
    public void itemStateChanged(ItemEvent e) 
       {
       this.fireEditingStopped(); 
        //System.out.println("Item Changed " + row + " value is: " + checkBox.isSelected());
         //System.out.println("Item Changed " + column + " value is: " + checkBox.isSelected());
              String S1 =(String) table1.getValueAt(row, 0);
              String S2 =(String) table1.getValueAt(row, 1);
              int s3=(int) table1.getValueAt(row, 2);
              Double Value =(Double) table1.getValueAt(row, 3);
              Boolean select=(Boolean) table1.getValueAt(row,4);
           if (Boolean.TRUE.equals(select)) 
           {
    	    abc += S1+"\t"+s2+"\t"+s3+"\t"+value+"\t";
    	 checkBox.setBackground(Color.black);                           // COLOR THE ROW dosnt work
    }
       }

Similar Threads

  1. Replies: 1
    Last Post: 06-16-2012, 04:17 PM
  2. JTable Row Color
    By ellias2007 in forum New To Java
    Replies: 5
    Last Post: 01-23-2012, 11:19 PM
  3. Replies: 6
    Last Post: 05-14-2011, 07:48 AM
  4. Color cell in JTable
    By ippacciani in forum AWT / Swing
    Replies: 3
    Last Post: 03-25-2009, 12:53 PM
  5. Color of the focued row in a JTable
    By SteM in forum AWT / Swing
    Replies: 2
    Last Post: 11-20-2007, 07:55 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •