Question

I have a problem with a method that should change the color of one specific cell i a JTable, however all the cells change color.

This is the method I am trying to implement:

public Component cellBG(JTable table, int row, int column)
{
    TableCellRenderer renderer = table.getCellRenderer(row, column);
    Component c = table.prepareRenderer(renderer, row, column);

    Object steak = table.getValueAt(row,column);

    if(((String) steak).startsWith("+"))
    {
       c.setBackground(Color.GREEN);
    }
    else if(((String) steak).startsWith("-"))
    {
        c.setBackground(Color.RED);
    }
    else if(((String) steak).startsWith("~"))
    {
        c.setBackground(Color.YELLOW);
    }
    else if(((String) steak).equals(""))
    {
        c.setBackground(Color.WHITE);
    }

    return c;

}

my table contains 10 columns with 8 rows all containing either +, -, ~, or " ", which should result in a scattered pattern i different colors, however the tabel is colored solid in the last entri which is read.

Ass per request an SSCCE

import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;

public class ptf_viewer extends JFrame {

    static JTable table;
    static DefaultTableModel model;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ptf_viewer frame = new ptf_viewer();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }); 
    }

    public ptf_viewer(){
    getContentPane().setBackground(Color.WHITE);
    setTitle("Portable test file viewing");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(50, 50, 1024, 768);
    getContentPane().setLayout(null);

    final String[] columnNames = {"Test 1", "Test 2", "Test 3", "Test 4", "Test 5", "Test 6", "Test 7", "Test 8", "Test 9", "Test 10"};
    final Object[][] data = {
                {"+","+","","","","~","","","",""},
                {"-","+","","","","","","","",""},
                {"+","+","","","~","","","","",""},
                {"+","+","","","","","~","","",""},
                {"+","-","","~","","","","","",""},
                {"+","-","","","","","","","",""},
                {"+","-","","","","","","","",""},
                {"-","+","~","","","","","","","+"}
    };
    model = new DefaultTableModel(data, columnNames);
    table = new JTable(data, columnNames);
    table.setModel(model);
    for(int i = 0 ; i < 8 ; i++){
            cellBG(table, i, 9);
    }
    JTableHeader header = table.getTableHeader();
    header.setFont(new Font("Times New Roman", Font.BOLD, 13));
    header.setBackground(Color.black);
    header.setForeground(Color.white);

    JScrollPane scrollPane = new JScrollPane(table);
    scrollPane.setSize(988, 618);
    scrollPane.setFont(new Font("Times New Roman", Font.BOLD, 13));
    scrollPane.setLocation(10, 60);
    getContentPane().add(scrollPane);
    }
    public Component cellBG(JTable table, int row, int column)
    {
        TableCellRenderer renderer = table.getCellRenderer(row, column);
        Component c = table.prepareRenderer(renderer, row, column);

        Object bøf = table.getValueAt(row,column);

        if(((String) bøf).startsWith("+"))
        {
           c.setBackground(Color.GREEN);
        }
        else if(((String) bøf).startsWith("-"))
        {
            c.setBackground(Color.RED);
        }
        else if(((String) bøf).startsWith("~"))
        {
            c.setBackground(Color.YELLOW);
        }
        else if(((String) bøf).equals(""))
        {
            c.setBackground(Color.WHITE);
        }
        return c;
    }
    }
Was it helpful?

Solution 2

You will need to create your own cell renderer and have it return the proper background color depending on the text contained in the cell.

public class MyTableCellRenderer extends DefaultTableCellRenderer {
    @Override
    public Color getBackground() {
        String steak = getText();
        if (steak.startsWith("+")) {
            return Color.GREEN;
        } else if (steak.startsWith("-")) {
            return Color.RED;
        } else if (steak.startsWith("~")) {
            return Color.YELLOW;
        } else if (steak.equals("")) {
            return Color.WHITE;
        }

            return super.getBackground();
        }
    }
}

And make sure your table use it.

table.setDefaultRenderer(String.class, new MyTableCellRenderer());

Assuming here that type of all your cells is String.

Edit after SSCCE : The class is Obejct and not String, use

table.setDefaultRenderer(Object.class, new MyTableCellRenderer());

OTHER TIPS

abbisDK wrote - SSCCE made and added to question,

  • see by by using DefaultTableCellRenderer and Pattern.compile (one of variour ways, Strin.equals() or String.startWith, contains are another and quite easy)

  • the same, so very similair code could be used for prepareRenderer

.

table = new JTable(data, columnNames) {
   //logics place for prepareRenderer
};

enter image description here

from code

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.util.regex.Pattern;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;

public class Ptf_Viewer {

    private JFrame frame = new JFrame("Portable test file viewing");
    private JTable table;
    private final String[] columnNames = {"Test 1", "Test 2", "Test 3", "Test 4",
        "Test 5", "Test 6", "Test 7", "Test 8", "Test 9", "Test 10"};
    private final Object[][] data = {
        {"+", "+", "", "", "", "~", "", "", "", ""},
        {"-", "+", "", "", "", "", "", "", "", ""},
        {"+", "+", "", "", "~", "", "", "", "", ""},
        {"+", "+", "", "", "", "", "~", "", "", ""},
        {"+", "-", "", "~", "", "", "", "", "", ""},
        {"+", "-", "", "", "", "", "", "", "", ""},
        {"+", "-", "", "", "", "", "", "", "", ""},
        {"-", "+", "~", "", "", "", "", "", "", "+"}
    };
    private DefaultTableModel model = new DefaultTableModel(data, columnNames);

    public Ptf_Viewer() {
        table = new JTable(data, columnNames) {
            //logics place for prepareRenderer
        };
        table.setModel(model);
        table.setPreferredScrollableViewportSize(
                new Dimension(800, table.getPreferredSize().height));
        JTableHeader header = table.getTableHeader();
        header.setFont(new Font("Times New Roman", Font.BOLD, 13));
        header.setBackground(Color.black);
        header.setForeground(Color.white);
        for (int i = 0; i < table.getColumnCount(); i++) {
            RowColorRenderer rowRenderer = new RowColorRenderer(i);
            TableColumn column = table.getColumnModel().getColumn(i);
            column.setCellRenderer(rowRenderer);
        }
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setFont(new Font("Times New Roman", Font.BOLD, 13));
        frame.add(scrollPane);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(50, 50);
        frame.pack();
        frame.setVisible(true);
    }

    private class RowColorRenderer extends DefaultTableCellRenderer {

        private static final long serialVersionUID = 1L;
        private int colNo = 0;

        RowColorRenderer(int col) {
            colNo = col;
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int row, int column) {
            Component comp = super.getTableCellRendererComponent(table, value,
                    isSelected, hasFocus, row, column);
            JComponent jc = (JComponent) comp;
            if (!isSelected) {
                if (table.getValueAt(row, colNo) != null) {
                    String str = table.getValueAt(row, colNo).toString();
                    if (!str.isEmpty()) {
                        if ((Pattern.compile("[+]").matcher(str).find())) {
                            setForeground(Color.magenta);
                            setBackground(Color.GREEN);
                            setFont(new Font("Serif", Font.BOLD, 16));
                            setHorizontalAlignment(CENTER);
                        } else if ((Pattern.compile("[-]").matcher(str).find())) {
                            setForeground(Color.blue);
                            setBackground(Color.RED);
                            setFont(new Font("Serif", Font.BOLD, 16));
                            setHorizontalAlignment(CENTER);
                        } else if ((Pattern.compile("[~]").matcher(str).find())) {
                            setForeground(Color.red);
                            setBackground(Color.YELLOW);
                            setFont(new Font("Serif", Font.BOLD, 16));
                            setHorizontalAlignment(CENTER);
                        } else {
                            setBackground(Color.WHITE);
                            setForeground(table.getForeground());
                            setFont(new Font("Serif", Font.PLAIN, 12));
                            setHorizontalAlignment(CENTER);
                        }
                    } else {
                        setBackground(Color.WHITE);
                        setForeground(table.getForeground());
                    }
                }
            }
            return this;
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                Ptf_Viewer frame = new Ptf_Viewer();
            }
        });
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top