Question

I am trying to make a check where if the value of cell 3 is not blank, the whole row will be colored green, however this does nothing but, if I make no check of that, only checking to be sure nothing is selected (so that selecting gives the color to show that you did select) everything is drawn green.

if (!table.isRowSelected(row))
            {
                component.setBackground(getBackground());


                if(table.getModel().getValueAt(row, 3).equals(""))
                {
                component.setBackground(Color.GREEN);
                }
            }

I tried to output the value and everything works properly, is there a problem here? A different way of doing this? thank you

Was it helpful?

Solution 2

A different way of doing this?

Check out Table Row Rendering for a different approach that doesn't require you to use a custom renderer.

This approach is easier because you don't need a custom renderer when you have different types of data in each table column.

OTHER TIPS

I tried to output the value and everything works properly, is there a problem here? A different way of doing this?

We would need some more code/info to answer this question properly:

In any case I'd suggest you take a look to Using Custom Renderer section in How to Use Tables trail. Also you can see the example below and take this as start point:

import java.awt.Color;
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class Demo {

    private void initGUI() {

        DefaultTableModel model = new DefaultTableModel(new Object[]{"Manufacturer", "Model", "Country", "Price"}, 0);
        model.addRow(new Object[]{"Fender", "Stratocaster", "Japan", ""});
        model.addRow(new Object[]{"Gibson", "Les Paul", "USA", "$ 1599"});
        model.addRow(new Object[]{"Jackson", "Soloist S3", "Japan","$ 1299"});
        model.addRow(new Object[]{"Paul Reed Smith","Standard 24", "USA", ""});

        JTable table = new JTable(model);

        table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer(){
            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {                
                super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                if(!isSelected){
                    Color background = table.getModel().getValueAt(row, 3).equals("") ? Color.GREEN : table.getBackground();
                    setBackground(background);
                } else {
                    setBackground(table.getSelectionBackground());
                }
                return this;
            }            
        });

        JFrame frame = new JFrame("Demo");      
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(new JScrollPane(table));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {   
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Demo().initGUI();
            }
        });
    }
}

Picture

enter image description here

Is your if statement correct? You have to check for non-blank cell

So, your if statement should look like this

if(!table.getModel().getValueAt(row, 3).equals(""))

Also to check value at cell#3, you have to use index 2 like this.

if(!table.getModel().getValueAt(row, 2).equals(""))

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top