CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 4 FirstFirst 1234 LastLast
Results 16 to 30 of 58
  1. #16
    Join Date
    May 2009
    Posts
    115

    Re: Setting Environment Variables

    The reason, I do not want to go through the entire table is because.

    Student | Grade | Extra-curricular Activities

    That is basically how the table is arranged. Now everything is stored into the table as strings. However, if I tried to run the entire row and parse it as doubles. I would have problems with the contents of Student and Extra-cuuricular acitivies as they are stored as strings and thus couldn't be able to get converted into doubles.

    Dang ... it seems its quite challenging to figure out how to change the text color ....

  2. #17
    Join Date
    May 2009
    Posts
    115

    Re: Setting Environment Variables

    Hehe, I conincidentally stumbled across one of your old posts dlorde, at : http://www.codeguru.com/forum/showth...ble+cell+color

    I was trying to use the code. I think it works to an extent because when I remove the if conditions ... it paints all the elements in the specified column to a specified color. But when I put the if conditions it doesn't do anything. I'm guessing, this is because, I'm not properly converting the value to a double?

    Code:
                Table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
                public Component getTableCellRendererComponent(JTable table,
                                                        String value,
                                                        boolean isSelected,
                                                        boolean hasFocus,
                                                        int row,
                                                        int column) {
                    super.getTableCellRendererComponent(table, value, isSelected,
                            hasFocus, row, column);
                    // Prepare default color
                    Color color = isSelected ? table.getSelectionForeground(): table.getForeground();
    
                    if (column == 1) {
    
                        if( Double.valueOf(value) <= 49.0) {
                            color = isSelected ? Color.red.brighter() : Color.red;
                        }
    
                        if( Double.valueOf(value) > 49.0) {
                            color = isSelected ? Color.green.brighter() : Color.green;
                        }
                    }
                    setForeground(color);
                    return this;
                }
            });

  3. #18
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: Setting Environment Variables

    Didn't you also change the function signature?

    From dlordes post:
    Code:
               public Component getTableCellRendererComponent(JTable table,
                                                        Object value,
                                                        boolean isSelected,
                                                        boolean hasFocus,
                                                        int row,
                                                        int column) {
    From your post:
    Code:
                public Component getTableCellRendererComponent(JTable table,
                                                        String value,
                                                        boolean isSelected,
                                                        boolean hasFocus,
                                                        int row,
                                                        int column) {

  4. #19
    Join Date
    May 2009
    Posts
    115

    Re: Setting Environment Variables

    Yes, I did. Primarily because, the value returned is a string.

    If I do not change it and use it as Object : when, I try to do the parsing ... I am forced to change it to a string.

  5. #20
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: Setting Environment Variables

    Unless I'm mistaken, by changing the function signature, you are no longer overriding an existing method (that will be called by the Swing framework), you are now creating a totally new method that wont ever run unless explicitly called.

    Any reason you're not debugging? A simple debugging technique is putting System.out.println calls throughout your code.

    Ex:
    System.out.println("at this point in the code variable abc = " + abc);
    System.out.println("Entered getTableCellRendererComponent method");
    System.out.println("Double.valueOf(value) = " + Double.valueOf(value));
    etc, etc
    Last edited by Martin O; July 31st, 2009 at 12:42 PM. Reason: changed 'function' to 'method'. old c habits die hard

  6. #21
    Join Date
    May 2009
    Posts
    115

    Re: Setting Environment Variables

    Quote Originally Posted by Martin O View Post
    Unless I'm mistaken, by changing the function signature, you are no longer overriding an existing method (that will be called by the Swing framework), you are now creating a totally new function that wont ever run unless explicitly called.

    Any reason you're not debugging? A simple debugging technique is putting System.out.println calls throughout your code.

    Ex:
    System.out.println("at this point in the code variable abc = " + abc);
    System.out.println("Entered getTableCellRendererComponent method");
    System.out.println("Double.valueOf(value) = " + Double.valueOf(value));
    etc, etc
    Hey Martin - Thanks for your reply.

    Hmm, I wasn't aware of the consequences of changing Object to String ... but it does make sense now. When, I change it it throws a null pointer exception. I thought its cause, I should change the parsing to a Float rather than a double. But the same message still persists.

    Code:
     Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
            at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:991)
    So basically, its getting value as a null object. I'm going to try and figure out why that is happening ...

    Oh and regarding the debugging ... hehe, yes I'm aware of that. I just didn't post them within my code.

  7. #22
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: Setting Environment Variables

    Quote Originally Posted by worldChanger View Post
    Oh and regarding the debugging ... hehe, yes I'm aware of that. I just didn't post them within my code.
    I think its better to include everyone else in on the debugging results.

    Regarding the value == null, I just checked all the getTableCellRendererComponent definitions in my code & I see that I do have checks for value == null. I also see that dlordes post didn't have these checks. Not sure whether value == null is a normal occurrence (i.e. you really should check for null), or whether value should never be null in getTableCellRendererComponent (i.e. you shouldn't have to check for null & I probably had some other problem in my code).

  8. #23
    Join Date
    May 2009
    Posts
    115

    Re: Setting Environment Variables

    Hmm, I got the problem fixed. What I did was that I declared another variable and took the float value of "value" and then did the comparison with this new variable. Thankfully, that did do the trick.

    But there's something else I'd like to do. Write now the check is with
    if (column == 1) { as it is passed in as in interger to the method. I was hoping to do something like if (column.toString().startsWith("Grades")) { and then only apply the color change to that column ...

    EDIT : Works! Just changed the if condition to ...
    if (Table.getColumnName(column).startsWith("Grade")) {
    Last edited by worldChanger; July 31st, 2009 at 01:13 PM.

  9. #24
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: Setting Environment Variables

    I usually create constants to represent the columns. For example this is how I do it:

    Code:
    class MyTableModel extends AbstractTableModel
    {
    ...
    	public static final int FIRST_NAME_COLUMN = 0;
    	public static final int LAST_NAME_COLUMN = 1;
    	public static final int GRADES_COLUMN = 2;
    ...
    }
    
    ...
    
    if(column == MyTableModem.GRADES_COLUMN) ...
    EDIT: just saw your edit. Cool that works too.

  10. #25
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: Setting Environment Variables

    Quote Originally Posted by worldChanger View Post
    if (Table.getColumnName(column).startsWith("Grade")) {
    Just one more thing...you really should use java naming conventions, especially when you're posting on forums asking others to read your code. (the capitalization of 'Table' implies its a class name not a variable name)

  11. #26
    Join Date
    May 2009
    Posts
    115

    Re: Setting Environment Variables

    Hey Martin - Mentally noted, thanks .

  12. #27
    Join Date
    May 2009
    Posts
    115

    Re: Setting Environment Variables

    Hmm, rather than changing the foreground color. I tried to change the background. In order to make the information stand out more. However, I am facing a slight problem. Its changing the cells that I don't intend to change as well and it changes it to the color black (default text color) and thus making it unreadable.

    Here's what I've done so far :

    Code:
                        if( Double.valueOf(value) <= 49.0) {
                            color =  Color.red;
                        }
    
                        if( Double.valueOf(value) > 49.0) {
                            color = Color.green;
                        }
                                      
                        }
    //Only change the color if the color has been changed
                    if (color != (table.getForeground())){
                    setBackground(color);
                    return this;
                    }
    //else, don't change the bg                 
    return this;
                    
                    }
                });
    Additionally, I also tried this
    Code:
    //Only change the color if the color has been changed
                    if (color != new Color(51,51,51){
                    setBackground(color);
                    return this;
                    }
    //else, don't change the bg                 
    return this;
                    
                    }
                });
    51,51,51 was the RGB code that I got before the color was changed. Thus I used it in the if condition ...
    Last edited by worldChanger; July 31st, 2009 at 02:33 PM.

  13. #28
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: Setting Environment Variables

    Some suggestions:

    Use debugging to figure out at what point in your code what you expected to happen differs from what actually is happening. You said you are, but, no offence--I don't believe you.

    Properly indent your code. (or put another way: You can understand code without the benefit of indentation? You must be smart! Personally I need indentation so I can keep track of context but that's just me).

    When something doesn't work, get to the bottom of why it doesn't work instead of just trying something else. Programming by trial & error, or throwing everything at the wall & seeing what sticks....is not the best technique.

    SSCCE. SSCCE. SSCCE. SSCCE. SSCCE. SSCCE. Did I mention SSCCE?

    I get the impression that you often think the problem is some high level thing, like how Swing works, but really your problems are with Java basics. Things like how to compare objects (hint: you don't use '!=').

  14. #29
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Setting Environment Variables

    Quote Originally Posted by worldChanger View Post
    What, I want to be able to do is search for the coloumn titled "Grades" ... and change the colours of the marks contained within it. So for example if the student recevied between 0 - 49 ... it should be red ... between 90 - 100 it should be green etc.
    I think you need to find out a bit more about how tables work. They use TableCellRenderers to display the contents of a cell. When the table needs to display a cell, it asks the cell renderer for a component to display in that space. The renderer passes back a component (by default a JLabel, but it could be any Component) that the table displays for that cell. The renderer decides how to display the value for that cell by configuring that component. If you want to customise the way a cell displays its value, you need to set your own cell renderer into the table, so it can check the value its being asked to display and change the background colour of the component it returns appropriately. To create a custom TableCellRenderer, you implement the interface and implement the sole method 'getTableCellRendererComponent' to return an appropriately configured component for display. The parameters of the method tell you all you need to configure it. See the TableCellRenderer Javadocs. You can set a renderer for an individual table column, or set a default renderer and decide how to display each cell in a single class. For full details and example code, see Editors & Renderers in the Java Tutorial.

    Unfortunately, I'm not sure how to collect the data in the coloumn as an array. Also, its easier if I just have 1 coloumn of grades, I can manually specify the col ... but how do I make the code generic to find even the 2nd or more cols if they do exist?
    If all you want to do is change the way the table displays various values (as above), you don't need to do this, just supply a cell renderer. But in any case, if you make a custom table model as I suggested, you can store the data however you want - as long as you can keep track of how many rows & columns there are, and return the value in a cell by row/column (see AbstractTableModel Javadocs and Creating A Table Model).

    Simplicity is the ultimate sophistication...
    L. Da Vinci
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  15. #30
    Join Date
    May 2009
    Posts
    115

    Re: Setting Environment Variables

    Quote Originally Posted by Martin O View Post
    Some suggestions:

    Use debugging to figure out at what point in your code what you expected to happen differs from what actually is happening. You said you are, but, no offence--I don't believe you.
    Hmm, I don't really know why I am even commenting to this as it clearly looks like you've made up your mind. If I wasn't putting System.outs how do you think would I know that the deafult color was 51 51 51?

    Properly indent your code. (or put another way: You can understand code without the benefit of indentation? You must be smart! Personally I need indentation so I can keep track of context but that's just me).
    This I agree with. I should have done that before posting.

    --- Thanks dlorde for the info.

    Just so that if someone searches for this same problem ... all that needed to be done was to put an else statement stating color = table.getBackground(); and that prevents the cells from changing to black.

Page 2 of 4 FirstFirst 1234 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured