dragging graphics in a JTable  
Author Message
Lou Lipnickey





PostPosted: 2003-12-3 2:56:00 Top

java-programmer, dragging graphics in a JTable I am interested in creating a graphics object (for example, a big dot)
creating it such that it can be moved within a cell and then moved
across cells. I had something working outside of a JTable (using
Point2D) and overiding paintComponent such that the drawing routine
looks like:

public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setComposite(makeComposite(.5f));
}

I have two questions at this point. First, is it feasible? 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.?)?
hanks - Lou



 
ak





PostPosted: 2003-12-5 16:36:00 Top

java-programmer >> dragging graphics in a JTable > 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





PostPosted: 2003-12-6 7:19:00 Top

java-programmer >> dragging graphics in a JTable Thanks - very impressive - Lou

ak wrote:
>>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.
>
>