Results 1 to 4 of 4
  1. #1
    ippacciani is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Default Color cell in JTable

    Hi,

    I'm new of Java graphics and I've some problems with a JTable.
    I need to change the color of cells(not of the text in the cell) only for the first 5 cells in my JTable. This color should change in a jToggleActionPerform method.
    I know that a CustomTableCellRenderer is necessary but how can I use it in the method?

    Is there anyone who could help me?

    Thanks a lot

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    You may be able to achieve this without a custom renderer. Set a boolean field to the state of the JToggleButton in its actionPerformed, and repaint() the table. Then setBackground of the cell in a prepareRenderer override based on the boolean field value and the row, column parameters.

    Here's a prepareRenderer override that colors table cells on the diagonal yellow:
    Java Code:
    JTable table = new JTable(data, headers) {
    
       @Override
       public Component prepareRenderer(TableCellRenderer renderer,
             int row, int column) {
          JLabel label = (JLabel) super.prepareRenderer(renderer, row, column);
          if (row == column) {
             label.setBackground(Color.YELLOW);
          } else {
             label.setBackground(Color.WHITE);
          }
          return label;
       }
    }
    db

  3. #3
    ippacciani is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Default

    Thanks a lot for your help.
    I've not understood very well the question about the boolean field in jToggle button. Considering I work with NetBeans IDE and I don't create directly the jTable, how could I do for solving the question?

    Thanks again

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Learn how to code a GUI and dump the visual designer until you do that.
    Trail: Creating a GUI with JFC/Swing (The Java™ Tutorials)

    Or limit yourself to what you can do with the visual designer.

    It's your choice.

Similar Threads

  1. Replies: 0
    Last Post: 11-14-2008, 04:14 AM
  2. set different font for each cell in JTable
    By success21061985 in forum AWT / Swing
    Replies: 3
    Last Post: 09-10-2008, 02:06 PM
  3. jTable Cell editing
    By mahaling_m in forum AWT / Swing
    Replies: 0
    Last Post: 06-23-2008, 08:17 AM
  4. Jframe In Jtable cell
    By Clarion in forum AWT / Swing
    Replies: 4
    Last Post: 06-23-2008, 04:42 AM
  5. Limiting the capacity of a cell of JTable
    By rameshraj in forum Advanced Java
    Replies: 0
    Last Post: 03-24-2008, 02:20 PM

Posting Permissions

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