[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[news.eclipse.tools] [Q] Table getValueAt(int row, int col), addMouseMotionListener and table contents update/repaint?

I wonder if some deeply needed access method  might be missing in the Table
or TableItem classes. I miss here something like  "int
table.rowAtPoint(Point p)", "int table.columnAtPoint(Point p)", which would
allow to implement something like "Object getValueAt(int row, int col)".

For example in Swing's JTable you can get the exact particular row and
column values via a "Point p" e.g.:


----------- Swing JTable example fragments -----------------
...
table.addMouseListener(new MouseHandler());
table.addMouseMotionListener(new MouseHandler());
...

class MouseHandler extends MouseInputAdapter {
  public void mouseMoved(MouseEvent evt) {
     Point p = evt.getPoint();
      int row = table.rowAtPoint(p);
      int column = table.columnAtPoint(p);
      if (row == -1 || column == -1) {
        status.setText(" ");
      }
      else {
        String ch = (String) table.getModel().getValueAt(row, column);
        ...
        ...
      }
  }

 public void mouseClicked(MouseEvent evt) {
      Point p = evt.getPoint();
      int row = table.rowAtPoint(p);
      int column = table.columnAtPoint(p);
     ...
  }
...
}

class MyTableModel extends AbstractTableModel {
    public int getColumnCount() {
      return columnCount;
    }

    public int getRowCount() {
      return rowCount;
    }

    public Object getValueAt(int row, int col) {
          return new String(new byte[] { (byte) (row * getRowCount() +
col) }, ourString);
    }

----------- EOF Swing JTable example fragments -----------------


In contrast here for JFace/SWT, I've only seen the following example, which
would only popup a message dialog for the first clicked column item of a row
(column 0 of row y). But this way it isn't capable of reacting in a general
manner on (column x of  row y):

 theTable.addListener (SWT.MouseDown, new Listener () {
  public void handleEvent (Event event) {
   Point pt = new Point (event.x, event.y);
   TableItem item = theTable.getItem (pt);
   if (item == null) return;
   for (int i=0; i < 16; i++) {
    Rectangle rect = item.getBounds (i);
     if (rect.contains (pt)) {
        int index = theTable.indexOf (item);
        showMessage("Int: " + index + "-" + i);
    }
   }
  }
 });


So what's the right way (for say a N x M Table) to get the table value of a
particular (row, col) back, which is clicked, hovered over with or selected
with the mouse?

1  2   3
4 [5] 6
7  8   9

And whats the prefered/best way, to dynamically change a whole tables
contents (for an N x M table all of it's row, col entries) with a repaint?


-Valentino