1

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

mKorbel
  • 109,525
  • 20
  • 134
  • 319
allegroBegin
  • 115
  • 1
  • 10

3 Answers3

4

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

dic19
  • 17,821
  • 6
  • 40
  • 69
  • I tried it like yours, it just colors everything green, even though some values are blank – allegroBegin Dec 19 '13 at 09:54
  • @allegroBegin the problem is you never override the `DefaultTableCellRenderer` bg color, you just set bg color to `component`. When `JTable` will render the second cell and the next ones this call: `component.setBackground(getBackground());` will set the bg color to green. In my example I don't use a `component` variable but override the background color of the `DefaultTableCellRenderer` and use `return this` instead. That's the trick. Take a look to [this topic](http://stackoverflow.com/questions/13672980/difficulties-understanding-the-renderers-mechanism-of-swings-jtable-and-jtree) – dic19 Dec 19 '13 at 15:31
  • Ok thank you for the tip, however, why was your way skipping any check what so ever (same check which works with rowrenderer and making everything green? Just trying to understand. If it was something which isn't that easily explained as a common bug, then nevermind, thank you very much for the help, I would upvote if I had the right – allegroBegin Dec 19 '13 at 22:29
  • @allegroBegin I'm not sure if I have understood this last question correctly (sorry if I haven't) but the two checks are there: the first one is ask if the cell is not selected `if(!isSelected)`, and second one is setting the right color if the 3rd column is empty which I made by using ternary operator: `Color background = table.getModel().getValueAt(row, 3).equals("") ? Color.GREEN : table.getBackground();`. – dic19 Dec 20 '13 at 14:14
2

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(""))

Ravindra Gullapalli
  • 9,049
  • 3
  • 48
  • 70
  • Hi, my if statement is correct, I am going for cell 4, where, if the cell is blank, do that. I had an output inside the statement which would print the statement value (true and the value of the cell ""), so it would already be true since it got to it and I also triple checked. Thank you for trying, I ll check out the other answers. – allegroBegin Dec 19 '13 at 08:53
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.

camickr
  • 321,443
  • 19
  • 166
  • 288