Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2003

Tip: Looking for answers? Try searching our database.

dragging graphics in a JTable

Thread view: 
Lou Lipnickey - 02 Dec 2003 18:55 GMT
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 - 05 Dec 2003 08:35 GMT
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.