1

i am developing an application in witch i have a table filled with numbers the numbers inferior 10 are colored with green and the others colored with LIGHT_GRAY for this i used TABLECELLRENDERER but it keeps telling me that there is mistake ,i can not find out what is the mistake exactly so i am asking for your help if you would not can and an other thing i want to apply this to all the table the code that i wrote above for one column how can i transform it on all columns thank you

import java.awt.Color;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

public class RenduCellule extends 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);
        Component c = getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        if (column == 2) {
       float val = Float.parseFloat(table.getValueAt(row,column)+"");
            Color color = null;
            if (val < 10)
                color = Color.green;
            else
                color = Color.LIGHT_GRAY;

            c.setBackground(color);
        } else
            c.setBackground(Color.pink);
        return c;

    }
}

and i wrote this in the main

jTable.setDefaultRenderer(Object.class, new RenduCellule());

here is the error

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at sagem02.RenduCellule.getTableCellRendererComponent(RenduCellule.java:17)
at javax.swing.JTable.prepareRenderer(JTable.java:5723)
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2114)
at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:2016)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1812)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
at javax.swing.JComponent.paintComponent(JComponent.java:780)
at javax.swing.JComponent.paint(JComponent.java:1056)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JViewport.paint(JViewport.java:728)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:586)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5217)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1579)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1502)
at javax.swing.RepaintManager.paint(RepaintManager.java:1272)
at javax.swing.JComponent.paint(JComponent.java:1042)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:79)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:116)
at java.awt.Container.paint(Container.java:1975)
at java.awt.Window.paint(Window.java:3912)
at javax.swing.RepaintManager$4.run(RepaintManager.java:842)
at javax.swing.RepaintManager$4.run(RepaintManager.java:814)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:789)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:738)
at javax.swing.RepaintManager.access$1200(RepaintManager.java:64)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1732)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Ela Hidri
  • 49
  • 8
  • Please post the error message as this is *critical* information that we need (and that **you** need) to understand and solve the problem – Hovercraft Full Of Eels Jul 13 '18 at 18:36
  • I've added the error message – Ela Hidri Jul 13 '18 at 18:42
  • 1) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jul 14 '18 at 00:19

1 Answers1

0
private static final TableCellRenderer RENDERER = new DefaultTableCellRenderer();

Not needed.

Component c = RENDERER.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

Should be:

Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

That is you need to invoke the default logic of the renderer first and then you invoke your custom rendering code.

Also,

Object result = table.getModel().getValueAt(row, column);

should be:

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

because the row/column are relative to the view (table) not the model. If your table is every sorted or filtered the row/column values of the table will not match directly to the model.

Edit:

super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
 Component c = getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

Why do you have two lines of code? My suggestion only has a single line of code.

Overriding a method is basic Java and is nothing specific to the renderer class.

Normally to override a method you do something like:

public Component getTableCellRendererComponent(...)
{
    super.getTableCellRendererComponent(...);

    setBackground(Color.RED);

    return this;
}

Or because the method returns the component that does the rendering you could do:

public Component getTableCellRendererComponent(...)
{
    Component c = super.getTableCellRendererComponent(...);

    c.setBackground(Color.RED);

    return c;
}

If you don't know how to use a method, then search the forum using the method name to find other examples in the forum. Take advantage of all the information in the forum and on the web.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • thank you but unfortunately it's not working i got a lot of error messages like Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError at sagem02.RenduCellule.getTableCellRendererComponent(RenduCellule.java:12) – Ela Hidri Jul 13 '18 at 18:49
  • Please take the time to understand the context of the suggestion and don't just blindly copy the code. I said you need to invoke the default logic of the renderer. This means when you override a method you need to invoke super.theMethodName(...) to invoke the default implementation. I forgot to add the "super". The error message should have helped you spot the typo. See edit. You need `super.getTableCell....()` to avoid calling the method over and over.. – camickr Jul 13 '18 at 18:53
  • so as i understand from what you are saying i should write it like this @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); Component c = getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); if (column == 2) { Object result = table.getValueAt(row, column); – Ela Hidri Jul 13 '18 at 19:01
  • 1) Don't post code in a comment. Update your question with the suggested code. 2) That does not appear to be what I suggested. I suggested what line to change. Why do you now have two lines of code replacing one line? – camickr Jul 13 '18 at 19:19
  • i have changed what you suggested and what i understand – Ela Hidri Jul 13 '18 at 19:30
  • I specifically stated that one line of code should be replaced with another single line of code. Once again I ask, why did you add two lines of code. Again, I ask you to think about your code. If you invoke the method within the same method you create an infinite loop because you keep calling the same method over an over. See my edit. – camickr Jul 13 '18 at 19:46
  • thank you i did not understand you back there ..i clearly understand now but it looks like i have a problem in this line val = Double.parseDouble(result.toString()); – Ela Hidri Jul 13 '18 at 20:38
  • Well then fix the problem. You can't keep coming here every time you have a little problem. You need to develop some basic problem solving skills. I have no idea what your problem is. I don't know what error message you are getting. What have you done to debug the problem? Did you use a System.out.println(...) to display the value returned from the table? – camickr Jul 13 '18 at 21:45