4

I have created a scene2d.ui Table and would like to alternate the background colours of each row.

I've had a look through the various table and cell methods but there doesn't seem to be an obvious way of doing it.

Is it even possible? And if so, what's the easiest way of going about it?

James Skemp
  • 8,018
  • 9
  • 64
  • 107
eby
  • 191
  • 2
  • 12

1 Answers1

9

I’ve used drawable backgrounds with one colored pixel. I haven’t found easier way even though it is far from elegant.

//Pixmap with one pixel
Pixmap pm1 = new Pixmap(1, 1, Format.RGB565);
pm1.setColor(Color.GREEN);
pm1.fill();

Pixmap pm2 = new Pixmap(1, 1, Format.RGB565);
pm2.setColor(Color.RED);
pm2.fill();

dialogueWindow = getWindow();

dialogueWindow.setTitle("New Game");
// The table that will have the green color 
Table row1 = new Table(mySkin);
row1.add(nameStation);
row1.add(nameStationField);
row1.setBackground(new TextureRegionDrawable(new TextureRegion(new Texture(pm1))));
dialogueWindow.add(row1);
dialogueWindow.row();

// The table that will have  the green color
Table row2 = new Table(mySkin);
row2.setBackground(new TextureRegionDrawable(new TextureRegion(new Texture(pm2))));
row2.add(cancel).size(120, 60);
row2.add(ok).size(100, 60);

dialogueWindow.add(row2).fillX();
dialogueWindow.row();
  • Thanks. I had thought about that but found it hard to believe that was the best way to do it. Would be more intuitive if they simply added a setBackgroundColor method to the row object. – eby Jun 17 '14 at 14:49
  • @eby I agree, this is crating two seperate tables and thus not aligning the columns properly. I also want to add a background to a column since I add rows by passing the table to a method that dynamicaly creates them and I do not want to fix column width. The only method I can currently think off is creating a stack for each cell and add in a drawable in the back. But if you want to have like somekind of border you need 3 different images (left, middle and right). – Madmenyo Aug 15 '15 at 10:07
  • use `Format.RGBA8888` instead of `Format.RGB565` to be able to manage transparency. – Benjamin Lucidarme Oct 09 '17 at 00:11