1

All - I'm trying to set a specific cell's background color after it is clicked AND a successful operation has occurred. I cant seem to do it. Here is the code:

JTable table = new JTable(new DefaultTableModel());
String [] colNames = {"col1", "col2", "ClickMe"};
for (String name : colNames)
  table.addColumn(name);

.... some code .....

String [] someArray = {"t", "t2", "t3"};

....
for (int i=0; i<someArray.length;i++) {
  Object [] row = new Object[3];
  row[0] = "bla";
  row[1] = "bla";
  row[2] = "Update";
  ((DefaultTableModel)table.getModel()).addRow(row);
  ((DefaultTableCellRenderer)gameTable.getCellRenderer(i, 2)).setBackground(Color.LIGHT_GRAY);
  ((DefaultTableCellRenderer)gameTable.getCellRenderer(i, 2)).setHorizontalAlignment(JLabel.CENTER);
}

table.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
  int row = gameTable.rowAtPoint(e.getPoint());
  int col = gameTable.columnAtPoint(e.getPoint());
  if (col == 2) {
  Color cellColor = ((DefaultTableCellRenderer)gameTable.getCellRenderer(row,col)).getBackground();
  if (cellColor == Color.LIGHT_GREY) {
    String val1 = (String)table.getModel().getValueAt(row,1);
    String val2 = (String)table.getModel().getValueAt(row,0);
    if (doSomething(val1, val2)) {  //this returns either true or false, its a Database operations
      ((DefaultTableCellRenderer)table.getCellRenderer(row, 2)).setBackground(Color.BLUE);
      }
   }
 }
};

Even thought i am specific calling setBackground on a row & column, it makes every cell in every row in column "2" change background color instead of just one specific one.

All the examples with customRenderers seem to just change the color based on when its clicked just change it to something else, i need to do some processing as well.

any thoughts here?

Thanks-

nachokk
  • 14,363
  • 4
  • 24
  • 53
user1772250
  • 319
  • 5
  • 15
  • make your own `CellRenderer` for example extending `DefaultCellRenderer` and when you override you switch backgrounds, if it's the one you want. – nachokk Sep 17 '13 at 18:34
  • see this previous post may helpyou http://stackoverflow.com/questions/9607670/how-do-i-correctly-use-custom-renderers-to-paint-specific-cells-in-a-jtable/9617446#9617446 – nachokk Sep 17 '13 at 18:37
  • This say that you should override `prepareRender` http://stackoverflow.com/questions/6908819/how-to-set-the-colour-of-my-1st-row-in-my-jtable-to-any-color-and-keep-previous – nachokk Sep 17 '13 at 18:39
  • The comments below have been good, but they seem to point to a customCellRenderer, where inside the renderer you explicity say "if row = 2 and column = 4 then { }" however i dont know which row i am currently on. and in the mouseListener, i get a bunch of data from the row which was selected and pass it to the database, so putting the "doSomething()" method in a customCellRenderer i dont think is an option. i have updated the code for this... – user1772250 Sep 17 '13 at 18:54
  • you can put a variable or a collections of variable and you set what rows you want to be of that colour... you can do it! with a custom cell renderer easily , but @trashgod that is a swing guru say something about using prepareRender! i'll add swing tag to capte their attention. – nachokk Sep 17 '13 at 18:56
  • How would that work exactly ? without passing some universal variable? basically when you click on the last column in any row, it needs to do a bunch of stuff with the text from the other columns, then change that last columns color from LIGHT_GREY to WHITE. i've trade custom cell renderers, but cant get it to work... thanks- – user1772250 Sep 17 '13 at 19:10
  • please to clarify whats "based on some other inputs", because I don't see there some ..., then there are possible only wild short to dark based on your code and with description – mKorbel Sep 17 '13 at 19:45
  • I have updated the code to have val1 & val2 which get text from other elements in the row which col2 is clicked. basically those two are editable, the user edits those, then clicks on col2, at which point some DB stuff is done (doSomething(val1, val2);) which returns a boolean. if the boolean==true, then after that i want to modify that particular rows col2 background color. – user1772250 Sep 17 '13 at 21:21

1 Answers1

1
Try this


table.setDefaultRenderer(Object.class, new TableCellRenderer(){
            private DefaultTableCellRenderer DEFAULT_RENDERER =  new DefaultTableCellRenderer();
            private Component comp;

            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                Component c = DEFAULT_RENDERER.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                if(isSelected){

                    c.setBackground(Color.YELLOW);
                }else{
                if (row%2 == 0){
                if (column==2){
                    c.setBackground(Color.WHITE);

                }
                else {
                    c.setBackground(Color.LIGHT_GRAY);
                } }    }

                return c;
            }

        });
ravibagul91
  • 20,072
  • 5
  • 36
  • 59