The Artima Developer Community
Sponsored Link

Java Answers Forum
JTable mouse selection "Drawing the rectangle"

4 replies on 1 page. Most recent reply: Apr 25, 2002 5:46 AM by james smith

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
james smith

Posts: 8
Nickname: jochem
Registered: Apr, 2002

JTable mouse selection "Drawing the rectangle" Posted: Apr 22, 2002 4:26 AM
Reply to this message Reply
Advertisement
I'm trying to create a mouse selection on a JTable, where I can click and drag the mouse and draw the selection rectangle. I can't seem to find the best way to draw the rectangle that resizes with the moving of the mouse.


Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: JTable mouse selection "Drawing the rectangle" Posted: Apr 23, 2002 6:21 AM
Reply to this message Reply
Give a look there :
http://www.artima.com/forums/flat.jsp?forum=1&thread=847

Thomas,

Jay Kandy

Posts: 77
Nickname: jay
Registered: Mar, 2002

Re: JTable mouse selection "Drawing the rectangle" Posted: Apr 23, 2002 4:28 PM
Reply to this message Reply
Smith, if you already found what you were looking for, please ignore this. But heres a JTable you wanted.
import javax.swing.JTable;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
import java.awt.Point;
 
public class AnotherTableEx extends JFrame
{
	public AnotherTableEx()
	{
		final TransparentJTable table = new TransparentJTable(new MyTableModel());
		table.setRowSelectionAllowed(true);
		table.setColumnSelectionAllowed(true);
 
		JScrollPane scrollPane = new JScrollPane(table);
		getContentPane().add(scrollPane);
 
		setSize(200, 200);
		show();
 
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
 
	public static void main(String[] args)
	{
		new AnotherTableEx();
	}
 
 
	class MyTableModel extends AbstractTableModel
	{
		Object[][] data = {
			{"Calories",    "130", "calories"},
			{"Total Fat",   "0", "%"},
			{"Sodium",      "1", "%"},
			{"Total Carbs", "12", "%"},
		};
 
		String[] columns = {"Item", "Amount", "Units"};
 
		public int getColumnCount()
		{ return 3; }
 
		public int getRowCount()
		{ return 4; }
 
		public String getColumnName(int c)
		{ return columns[c]; }
 
		public Object getValueAt(int rowIndex, int colIndex)
		{ return data[rowIndex][colIndex]; }
 
		public boolean isCellEditable(int rowIndex, int colIndex)
		{ return false; }
	}
}
 
class TransparentJTable extends JTable implements MouseMotionListener, MouseListener
{
	protected Point begin = new Point();
	protected Point end = new Point();
 
	public TransparentJTable(TableModel tm)
	{
		super(tm);
		setOpaque(false);
		addMouseListener( this );
		addMouseMotionListener( this );
	}
 
	public void mouseClicked(MouseEvent me)
	{ }
 
	public void mousePressed(MouseEvent me)
	{
		begin = me.getPoint();
	}
 
	public void mouseReleased(MouseEvent me)
	{ }
 
	public void mouseEntered(MouseEvent me)
	{ }
 
	public void mouseExited(MouseEvent me)
	{ }
 
	public void mouseDragged(MouseEvent me)
	{
		end = me.getPoint();
		update(getGraphics());
 
		getGraphics().drawRect(begin.x, begin.y, end.x - begin.x, end.y - begin.y);
 
		addRowSelectionInterval(rowAtPoint(begin), rowAtPoint(end));
		addColumnSelectionInterval(columnAtPoint(begin), columnAtPoint(end));
	}
 
	public void mouseMoved(MouseEvent me)
	{ }
}

Jay Kandy

Posts: 77
Nickname: jay
Registered: Mar, 2002

Re: JTable mouse selection "Drawing the rectangle" Posted: Apr 23, 2002 4:35 PM
Reply to this message Reply
oops forgot to mention - its crude. A little fine tuning is needed. But good for starters.

james smith

Posts: 8
Nickname: jochem
Registered: Apr, 2002

Re: JTable mouse selection "Drawing the rectangle" Posted: Apr 25, 2002 5:46 AM
Reply to this message Reply
Hi there just wanted to say thanks for the help
cheers

Flat View: This topic has 4 replies on 1 page
Topic: php and Java2 in win2k Previous Topic   Next Topic Topic: re: Graphics2D class cannot found in IE 5.0

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use