Sample Code 19: How to display a 2D array in table - Part 2?
Written by kksou   
Sunday, 17 September 2006
Problem

We have displayed a 2D array in a table in Part 1. Now we would like to add borders to the table as shown below:

$data = array(
array('', 'header1', 'header2', 'header3'),
array('row0', 1, 2, 3),
array('row1', 4, 5, 6));

How to display a 2D array in table - Part 2?


Solution
  • GtkTable itself does not provide built-in method such as the <table border=1> statement in html to add a border to each cell.
  • However, the GtkTable::attach() method allows us to attach almost any widget to each cell.
  • As such, to draw a border, we simply put each cell inside a GtkFrame, and attach the GtkFrame to the table.

Sample Code
1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
<?php
$window = new GtkWindow();
$window->set_size_request(400, 200);
$window->connect_simple('destroy', array('Gtk','main_quit'));

$vbox = new GtkVBox();
$window->add($vbox);

// display title
$title = new GtkLabel('Display 2D Array in Table - Part 2 (add borders)');
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 40);
$vbox->pack_start($title, 0, 0);

$table = new GtkTable();
$vbox->pack_start($table);

$data = array(
array('', 'header1', 'header2', 'header3'),
array('row0', 1, 2, 3),
array('row1', 4, 5, 6));

display_table ($table, $data);

function display_table($table, $a) {
  • Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
  • Registration is free and immediate.
  • Have some doubt about the registration? Please read this forum article.
Explanation

Please refer to Part 1 first which explains how to display a 2D array in a table.

What's new here in Part 2:

  1. Create the frame
  2. Put the cell content (GtkLabel) inside the frame
  3. Attach the frame (not the GtkLabel) to the table
And you now have a table with borders!

User reviews   Average user ratings:    1.0   (from 1 user)
  1. sami
    May 19, 2008 3:22am

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Copyright © 2006-2008. kksou.com. All Rights Reserved