Results 1 to 5 of 5

Thread: Jtable Cell colour

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    UK
    Posts
    271

    Jtable Cell colour

    Hi,

    How do I add a certain colour to the background of an individual cell?

    Thanks
    §tudz

    Studzworld.com - Portfolio

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    To change the color of a selected cell you would want to call either setSelectionForeground(Color selectionForeground) or setSelectionBackground(Color selectionBackground). setGridColor(Color gridColor) is also pretty fun to use.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    UK
    Posts
    271
    thanks, but how do I do this?
    §tudz

    Studzworld.com - Portfolio

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Code:
     import java.awt.*; 
     import java.awt.event.*;
     import javax.swing.*; 
    
     class G{
      public static void main(String[] args){
    
      
      JFrame jf = new JFrame("Inventory"); 
      Container c = jf.getContentPane();
      
      Object[] colnames = {"Title", "Author", "Isbn", "Price"};
     
      Object[][] data = {
       {"Weaveworld", "CliveBarker", "125457", new Double(9.56)}, 
       {"Watership Down", "Richard Adams", "898454", new Double(8.43)},
       {"The Guardian", "John Saul", "87987", new Double(10.98)},          
      }; 
    
      jf.addWindowListener(new WindowAdapter(){
       public void windowClosing(WindowEvent we){
        System.exit(0);
       }
      });
    
      JTable jt = new JTable(data, colnames);
      jt.setGridColor(Color.RED);
      jt.setSelectionBackground(Color.yellow);
      JScrollPane scrollableTable = new JScrollPane(jt); 
      c.add(scrollableTable);
      jf.setSize(500, 100);  
      jf.setVisible(true); 
      
     }
    }

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    UK
    Posts
    271
    I have tried this, and all this has acheived is setting the whole row to yellow, when a cell in that row is selected.

    I want to set a single cell colour to a set colour (ie yellow)

    example:

    if cell.value = 45 then make that cell yellow

    Thanks
    §tudz

    Studzworld.com - Portfolio

Posting Permissions

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



Click Here to Expand Forum to Full Width