Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Why is object reference showing in JTable when editing cell?

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why is object reference showing in JTable when editing cell?

    I'm using a custom CellRenderer, see code below. I have a feeling something I'm doing in this is causing the problem. The CellRenderer itself seems to work fine showing the actual value, but when I edit the cell, I see an object reference instead of the actual value.

    Here's the code:

    public class PlannerCellRenderer extends JLabel implements TableCellRenderer, Serializable {
    	private static final long serialVersionUID = 1L;
    	private Border unselectedBorder = null;
    	private Border selectedBorder = null;
    	boolean isBordered = true;
     
    	public PlannerCellRenderer(boolean isBordered) {
    		super();
    		this.isBordered = isBordered;
    		setOpaque(true);  // MUST do this for background to show up.
    	}
     
    	public Component getTableCellRendererComponent(JTable table, Object cell, boolean isSelected, boolean hasFocus, int row, int column) {
    		Color newColor;
    		if(cell == null) {
    			super.setText("");
    			if (isSelected) {
    				super.setBackground(new Color(50,50,100));
    	        } else {
    	    		super.setBackground(new Color(255,255,255));
    	        }
    			return this;
    		}
    		if(column == 0 ) {
    			super.setText((String)cell);
    			super.setBackground(new Color(255,255,255));
    			super.setForeground(new Color(0,0,0));
    			super.setHorizontalAlignment(JLabel.LEFT);
    			return this;
    		}
    		switch(((DisplayCell)cell).getCellType()) {
    			case DisplayCell.BLANK:
    				newColor = new Color(255,255,255);  // White
    				break;
    			case DisplayCell.FIRSTTYPE:
    				newColor = new Color(128,128,128);	// Grey
    				break;
    			case DisplayCell.SECONDTYPE:
    				newColor = new Color(0,255,0);		// Green
    				break;
    			default:
    				newColor = new Color(255,255,255);	// White
    				break;
    		}
     
    		if(((DisplayCell)cell).getNumberResources() == 0) {
    			super.setText("");
    		} else {
    			super.setText(Integer.toString(((DisplayCell)cell).getNumberResources()));
    		}
     
    		super.setHorizontalAlignment(JLabel.CENTER);
    		if (isSelected) {
    			super.setBackground(new Color(50, 50, 100));
            } else {
        		super.setBackground(newColor);
            }
     
            setToolTipText(DisplayCell.CELLTYPE[((DisplayCell)cell).getCellType()]);
            return this;
    	}
     
    }


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Why is object reference showing in JTable when editing cell?

    Could you elaborate what you mean?

    Do you experience output similar to java.lang.Object@1ba34f2?
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why is object reference showing in JTable when editing cell?

    Exactly. But, I only see something similar to "java.lang.Object@1ba34f2" when I click on the cell to edit it. It displays fine otherwise.

  4. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why is object reference showing in JTable when editing cell?

    OK, I found my own problem.

    The toString() method is called on the object the CellRenderer is displaying. In my case, DisplayCell. I failed to override the toString() method for this, so the default was returning the reference string name.

Similar Threads

  1. Object Reference
    By Mr.777 in forum Object Oriented Programming
    Replies: 2
    Last Post: June 13th, 2011, 03:03 AM
  2. JTable - multiple lines in a cell AND cells not editable?
    By friday in forum AWT / Java Swing
    Replies: 3
    Last Post: January 10th, 2011, 08:44 AM
  3. JTable prepareRenderer changes all cell colors
    By BrettStuart in forum AWT / Java Swing
    Replies: 9
    Last Post: July 21st, 2010, 02:51 AM
  4. Object as Reference not working
    By jassi in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 9th, 2010, 09:47 AM
  5. JTable gridlines not showing in a nimbus L&F
    By chronoz13 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 22nd, 2010, 07:45 AM