> I have two questions at this point. First, is it feasible?
yes
> and Second,
> if feasible, how would one generally go about it (custom editor, firing
> events when it the object gets near a cell border, etc.?)?
here is a sample code:
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
public class TableTest extends JTable {
Point p = new Point(100, 100);
boolean drag;
int row, column;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawOval(p.x - 2, p.y - 2, 4, 4);
}
public TableTest(TableModel dm) {
super(dm);
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
if(drag) {
p = e.getPoint();
int rap = rowAtPoint(p);
int cap = columnAtPoint(p);
Rectangle r = getCellRect(rap, cap, true);
if(row != rap || column != cap) {
firePropertyChange("point entered another cell", new Point(row,
column), new Point(rap, cap));
row = rap;
column = cap;
}
repaint();
}
}
});
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
Point p = e.getPoint();
if((p.x >= TableTest.this.p.x - 2 || p.x <= TableTest.this.p.x + 2) &&
(p.y >= TableTest.this.p.y - 2 || p.y <= TableTest.this.p.y + 2)) {
drag = true;
}
}
public void mouseReleased(MouseEvent e) {
drag = false;
}
});
}
public static void main(String[] args) {
TableTest tt = new TableTest(new DefaultTableModel(20,10));
final JTextField field = new JTextField();
tt.addPropertyChangeListener("point entered another cell", new
PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
Point p = (Point)e.getNewValue();
field.setText("row:" + p.x + " column:" + p.y);
}
});
JFrame frame = new JFrame();
frame.getContentPane().add(new JScrollPane(tt));
frame.getContentPane().add(field, BorderLayout.SOUTH);
frame.pack();
frame.show();
}
}
This example has problem with selection while point dragging. So it is
better to put GlassPane over the table and use it to paint the point and
hear to mouse events.
--
____________
http://reader.imagero.com the best java image reader.
Lou Lipnickey - 05 Dec 2003 23:19 GMT
Thanks - very impressive - Lou
>>I have two questions at this point. First, is it feasible?
>
[quoted text clipped - 88 lines]
>
> http://reader.imagero.com the best java image reader.