Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 299
  • Last Modified:

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
0
CloudStrife209
Asked:
CloudStrife209
  • 5
1 Solution
 
for_yanCommented:
Strange, why your CellRenderee extends JTable?

I'd rather suggest to extend DefaultTableCellRenderer and overwrite getCompoenet methodadding colors there
0
 
for_yanCommented:
Look at this example of TableCellRenderer

http://www.exampledepot.com/egs/javax.swing.table/CustRend.html
0
 
for_yanCommented:
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
0
 
for_yanCommented:

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
0
 
for_yanCommented:

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.

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now