Avatar of CloudStrife209
CloudStrife209

asked on 

Text Dissappears from Table

Have an issue applying a custom cell renderer to a table that I've created, unfortunately when i apply the renderer the text disappears. remove the renderer and it reappears.

I've set the foreground and background colours and set Opaque to true I've even tried setting the value again in the renderer with no luck. I think I've got all the required code below.
occupationTable = new JTable(model);
occupationTable.setDefaultRenderer(Object.class, new tableColour());

Open in new window

package renderers;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

import dataTypes.OccupationTableModel;

public class tableColour extends JTable implements TableCellRenderer {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// public static final DefaultTableCellRenderer DEFAULT_RENDERER = new
	// DefaultTableCellRenderer();

	@Override
	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
		setOpaque(true);

		OccupationTableModel aModel = (OccupationTableModel) table.getModel();

		String Acceptence = aModel.getOccupation(rowIndex).getgeneralAcceptence();

		if (isSelected) {
			this.setForeground(Color.BLACK);
			this.setBackground(Color.BLUE);
			
		} else {
			if (Acceptence != null) {
				try {
					if (Acceptence.equals("Decline")) {
						this.setForeground(Color.BLACK);
						this.setBackground(Color.RED);
					} else {
						this.setForeground(Color.BLACK);
						this.setBackground(Color.WHITE);
					}
				} catch (Exception e) {
					System.out.println("Shit");
					e.printStackTrace();
				}
			}
			
		}
		return this;
	}

}

Open in new window

UnformattedTable.jpg
UnformattedTable.jpg
Java

Avatar of undefined
Last Comment
for_yan
Avatar of for_yan
for_yan
Flag of United States of America image

Strange, why your CellRenderee extends JTable?

I'd rather suggest to extend DefaultTableCellRenderer and overwrite getCompoenet methodadding colors there
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of for_yan
for_yan
Flag of United States of America image

So look your class tableColour extends JTable, so when you say
return this;
in the getComponent method it actually returns an iinstance of JTable - which does not make much sense
what happend after that with your view is rather hard to imagine
Avatar of for_yan
for_yan
Flag of United States of America image


In fact getComponent sould return the component which will populate the cell of your table - so that would usually
be JLaebel as in the example above or JTextField - something normally lighter than JTable
Avatar of for_yan
for_yan
Flag of United States of America image


also look at good example here:
http://www.roseindia.net/java/example/java/swing/CustomCellRenderer.shtml

With these things it is easier to follow the exisiting pattern  - take one of the above examples -
make sure it returns the background color as you like it and it would most probably work.

Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo