Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » NatTable » How to set background image for whole nattable(How to set background image for whole nattable)
How to set background image for whole nattable [message #1383941] Tue, 27 May 2014 10:27 Go to next message
jacky gao is currently offline jacky gaoFriend
Messages: 4
Registered: May 2014
Junior Member
I am a NatTable user.NatTable is great.
Now I have already set background image or color for NatTable cell.But I don't know how to set background image or color for whole NatTable.
Please help me!Many thanks.
Re: How to set background image for whole nattable [message #1384102 is a reply to message #1383941] Wed, 28 May 2014 06:33 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2494
Registered: July 2012
Senior Member
NatTable itself is a Canvas, therefore a Control, which means you can set a background color via

//set the NatTable background white
natTable.setBackground(GUIHelper.COLOR_WHITE);


I also assume that setBackgroundImage(Image) will work.

But if that should be visible in your cells, like getting transparent cell backgrounds, you will need to ensure that there is no background painter involved for cell rendering. For example, you need to configure a TextPainter that doesn't render the background and ensure that there is also no other painter in your configuration that renders the background, as such a painter would simply paint over your control background.
Re: How to set background image for whole nattable [message #1384115 is a reply to message #1384102] Wed, 28 May 2014 07:11 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2494
Registered: July 2012
Senior Member
Just tried and setBackgroundImage() does not work that simply.

So for a different background color you want to use for the whole NatTable, you can try this code:
final NatTable natTable = new NatTable(
	parent, bodyDataLayer, false);
		
natTable.addConfiguration(new DefaultNatTableStyleConfiguration() {
	{
		cellPainter = new TextPainter(false, false);
	}
});
		
natTable.configure();
		
natTable.setBackground(GUIHelper.COLOR_YELLOW);


For the background image you need to set a different NatLayerPainter, as the default one is cleaning up the background by always painting the background color.
In that case this code should help:
final NatTable natTable = new NatTable(
	parent bodyDataLayer, false);
		
natTable.addConfiguration(new DefaultNatTableStyleConfiguration() {
	{
		cellPainter = new TextPainter(false, false);
	}
});		
natTable.configure();
		
natTable.setBackgroundImage(new Image(Display.getCurrent(), "C:\\Daten\\dirk_fauth.jpg"));

natTable.setLayerPainter(new NatLayerPainter(natTable) {
	@Override
	protected void paintBackground(ILayer natLayer, GC gc, int xOffset,
			int yOffset, Rectangle rectangle,
			IConfigRegistry configRegistry) {
		//do nothing to avoid overpainting the background image
	}
});
Re: How to set background image for whole nattable [message #1384170 is a reply to message #1384115] Wed, 28 May 2014 10:03 Go to previous messageGo to next message
jacky gao is currently offline jacky gaoFriend
Messages: 4
Registered: May 2014
Junior Member
Thanks for your help,I have already realized to set background image for whole nattable.
My code:
natTable.setLayerPainter(new NatLayerPainter(natTable){
	@Override
	protected void paintBackground(ILayer natLayer, GC gc, int xOffset,int yOffset, Rectangle rectangle,IConfigRegistry configRegistry) {
		super.paintBackground(natLayer, gc, xOffset, yOffset, rectangle, configRegistry);
		String path=editor.report.getBackgroundImage();
		if(path!=null){
			try{
				Image bgImg=Activator.getImage(path, true);
				IPreferenceStore store=Utils.getReportPreferenceStore();
				int width=store.getInt(RowHeaderLayerStack.ROW_HEADER_WIDTH);
				gc.drawImage(bgImg, rectangle.x+width, rectangle.y+DataLayer.DEFAULT_ROW_HEIGHT);						
			}catch(Exception ex){
				ex.printStackTrace();
			}
		}
	}
});
Re: How to set background image for whole nattable [message #1384206 is a reply to message #1384170] Wed, 28 May 2014 11:56 Go to previous message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2494
Registered: July 2012
Senior Member
Yes, drawing the image on the GC in the ILayerPainter is also an valid option. Smile

Thanks for sharing!
Previous Topic:Exceptions
Next Topic:get the selected object when double click on Nattable cell
Goto Forum:
  


Current Time: Wed Sep 20 07:39:24 GMT 2017

Powered by FUDForum. Page generated in 0.03476 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software