cell handling...

i got a collection of data in table cell...then i got button of delete

how can i make it when i click the particular cell data at the table and then click button delete,the particular data that i wan delete will delete?how to make the delete button know the cell i clicked is the thing i wan to delete?how how how?

[327 byte] By [ahgana] at [2007-10-2 4:27:17]
# 1

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.table.DefaultTableModel;

public class TableDeletion implements ActionListener

{

JTable table;

public void actionPerformed(ActionEvent e)

{

int col = table.getSelectedColumn();

int row = table.getSelectedRow();

if(row == -1 || col == -1)

return;

DefaultTableModel model = (DefaultTableModel)table.getModel();

model.setValueAt("", row, col);

model.fireTableCellUpdated(row, col);

}

public static void main(String[] args)

{

TableDeletion td = new TableDeletion();

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(td.getNorthPanel(), "North");

f.getContentPane().add(new JScrollPane(td.getTable()));

f.setSize(400,400);

f.setLocation(200,200);

f.setVisible(true);

}

private JPanel getNorthPanel()

{

JButton delete = new JButton("delete selected cell");

delete.addActionListener(this);

JPanel panel = new JPanel();

panel.add(delete);

return panel;

}

private JTable getTable()

{

int rows = 32, cols = 4;

Object[] colNames = { "column 1", "column 2", "column 3", "column 4" };

Object[][] data = new Object[rows][cols];

for(int row = 0; row < rows; row++)

for(int col = 0; col < cols; col++)

data[row][col] = "item " + (row * cols + col + 1);

table = new JTable(new DefaultTableModel(data, colNames));

table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

table.setCellSelectionEnabled(true);

return table;

}

}

74philipa at 2007-7-15 23:56:09 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...
# 2

placing an image in the grid cells and drag and drop that image into

different cells.

i have the code for finding the position of the each cell in a grid. but i have to place an image in a cell and it should be drag and drop to different cell

pls go through my code and give me a suggestion to place an image in particular cell and drag and drop that cell into other cells.

here the code:

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import javax.swing.*;

public class Grid

{

public Grid()

{

GridPanel gridPanel = new GridPanel();

CellSelector cellSelector = new CellSelector(gridPanel);

gridPanel.addMouseListener(cellSelector);

JFrame f = new JFrame();

//ImageIcon ii= create ImageIcon("C:/Documents and Settings/sreehari.m/Desktop/new/java //pgm/new");

//cellSelector.addImageIcon();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(gridPanel);

f.setSize(400,400);

f.setLocation(200,200);

f.setVisible(true);

}

public static void main(String[] args)

{

new Grid();

}

}

class GridPanel extends JPanel

{

double xInc, yInc;

final int

GRID_SIZE = 4,

DRAW= 0,

FILL= 1,

PAD= 20;

int[][] cells;

public GridPanel()

{

initCells();

}

protected void paintComponent(Graphics g)

{

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

double w = getWidth();

double h = getHeight();

xInc = (w - 2*PAD)/GRID_SIZE;

yInc = (h - 2*PAD)/GRID_SIZE;

// row lines

double x1 = PAD, y1 = PAD, x2 = w - PAD, y2 = h - PAD;

for(int j = 0; j <= GRID_SIZE; j++)

{

g2.draw(new Line2D.Double(x1, y1, x2, y1));

y1 += yInc;

}

// col lines

y1 = PAD;

for(int j = 0; j <= GRID_SIZE; j++)

{

g2.draw(new Line2D.Double(x1, y1, x1, y2));

x1 += xInc;

}

// fill cells

g2.setPaint(Color.red);

for(int row = 0; row < cells.length; row++)

{

for(int col = 0; col < cells[0].length; col++)

{

if(cells[row][col] == FILL)

{

x1 = PAD + col * xInc + 1;

y1 = PAD + row * yInc + 1;

g2.drawString("("+row+","+col+")" , (int) x1+50, (int)y1+50);

//g2.drawString("("+Grid("+row+","+col+")+")" , (int) x1+50, (int)y1+50);

// g2.drawString("("")" , (int) x1+50, (int)y1+50);

// g2.fill(new Rectangle2D.Double(x1, y1, xInc - 1, yInc - 1));

}

}

}

}

public void toggleCellColor(int row, int col)

{

int mode = DRAW;

if(cells[row][col] == DRAW)

mode = FILL;

cells[row][col] = mode;

repaint();

}

private void initCells()

{

cells = new int[GRID_SIZE][GRID_SIZE];

for(int row = 0; row < cells.length; row++)

for(int col = 0; col < cells[0].length; col++)

cells[row][col] = DRAW;

}

}

class CellSelector extends MouseAdapter

{

GridPanel gridPanel;

public CellSelector(GridPanel gp)

{

gridPanel = gp;

}

public void mousePressed(MouseEvent e)

{

Point p = e.getPoint();

int col = 0, row = 0;

double x = gridPanel.PAD + gridPanel.xInc;

// find column

for(int j = 0; j < gridPanel.GRID_SIZE; j++)

{

if(p.x < x)

{

col = j;

break;

}

x += gridPanel.xInc;

}

// find row

double y = gridPanel.PAD + gridPanel.yInc;

for(int j = 0; j < gridPanel.GRID_SIZE; j++)

{

if(p.y < y)

{

row = j;

break;

}

y += gridPanel.yInc;

}

gridPanel.toggleCellColor(row, col);

}

}

haria at 2007-7-15 23:56:09 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...
# 3

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import java.awt.image.BufferedImage;

import java.io.IOException;

import javax.imageio.ImageIO;

import javax.swing.*;

import javax.swing.event.MouseInputAdapter;

public class GridRx

{

public GridRx(BufferedImage image)

{

GridPanel gridPanel = new GridPanel(image);

CellSelector cellSelector = new CellSelector(gridPanel);

gridPanel.addMouseListener(cellSelector);

gridPanel.addMouseMotionListener(cellSelector);

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(gridPanel);

f.setSize(475,475);

f.setLocation(200,200);

f.setVisible(true);

}

public static void main(String[] args) throws IOException

{

// http://java.sun.com/docs/books/tutorial/uiswing/

//components/example-1dot4/images/geek/geek--g--.gif

String path = "images/geek--g--.gif"; // 83, 67

BufferedImage bi = ImageIO.read(GridRx.class.getResource(path));

new GridRx(bi);

}

}

class GridPanel extends JPanel

{

BufferedImage image;

Rectangle rect;

double xInc, yInc;

final int

GRID_SIZE = 4,

DRAW= 0,

FILL= 1,

PAD= 20;

int[][] cells;

public GridPanel(BufferedImage image)

{

this.image = image;

initCells();

}

protected void paintComponent(Graphics g)

{

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

double w = getWidth();

double h = getHeight();

xInc = (w - 2*PAD)/GRID_SIZE;

yInc = (h - 2*PAD)/GRID_SIZE;

// row lines

double x1 = PAD, y1 = PAD, x2 = w - PAD, y2 = h - PAD;

for(int j = 0; j <= GRID_SIZE; j++)

{

g2.draw(new Line2D.Double(x1, y1, x2, y1));

y1 += yInc;

}

// col lines

y1 = PAD;

for(int j = 0; j <= GRID_SIZE; j++)

{

g2.draw(new Line2D.Double(x1, y1, x1, y2));

x1 += xInc;

}

// fill cells

g2.setPaint(Color.red);

for(int row = 0; row < cells.length; row++)

{

for(int col = 0; col < cells[0].length; col++)

{

if(cells[row][col] == FILL)

{

x1 = PAD + col * xInc + 1;

y1 = PAD + row * yInc + 1;

g2.drawString("("+row+","+col+")" , (int) x1+50, (int)y1+50);

//g2.drawString("("+Grid("+row+","+col+")+")" ,

//(int) x1+50, (int)y1+50);

// g2.drawString("("")" , (int) x1+50, (int)y1+50);

// g2.fill(new Rectangle2D.Double(x1, y1, xInc - 1, yInc - 1));

}

}

}

// draw image

if(rect == null)

initRect(w, h, xInc, yInc);

g2.drawImage(image, rect.x, rect.y, this);

}

public void setRect(int x, int y)

{

rect.setLocation(x,y);

repaint();

}

public void toggleCellColor(int row, int col)

{

int mode = DRAW;

if(cells[row][col] == DRAW)

mode = FILL;

cells[row][col] = mode;

repaint();

}

private void initCells()

{

cells = new int[GRID_SIZE][GRID_SIZE];

for(int row = 0; row < cells.length; row++)

for(int col = 0; col < cells[0].length; col++)

cells[row][col] = DRAW;

}

private void initRect(double w, double h, double xInc, double yInc)

{

// locate image in cell[0][0]

int width = image.getWidth();

int height = image.getHeight();

int x = (int)(PAD + (xInc - width)/2);

int y = (int)(PAD + (yInc - height)/2);

rect = new Rectangle(x, y, width, height);

}

}

class CellSelector extends MouseInputAdapter

{

GridPanel gridPanel;

Point offset;

boolean dragging;

public CellSelector(GridPanel gp)

{

gridPanel = gp;

offset = new Point();

dragging = false;

}

public void mousePressed(MouseEvent e)

{

Point p = e.getPoint();

int col = 0, row = 0;

double x = gridPanel.PAD + gridPanel.xInc;

// find column

for(int j = 0; j < gridPanel.GRID_SIZE; j++)

{

if(p.x < x)

{

col = j;

break;

}

x += gridPanel.xInc;

}

// find row

double y = gridPanel.PAD + gridPanel.yInc;

for(int j = 0; j < gridPanel.GRID_SIZE; j++)

{

if(p.y < y)

{

row = j;

break;

}

y += gridPanel.yInc;

}

if(gridPanel.rect.contains(p))

{

offset.x = p.x - gridPanel.rect.x;

offset.y = p.y - gridPanel.rect.y;

dragging = true;

}

else

gridPanel.toggleCellColor(row, col);

}

public void mouseReleased(MouseEvent e)

{

dragging = false;

}

public void mouseDragged(MouseEvent e)

{

if(dragging)

{

int x = e.getX() - offset.x;

int y = e.getY() - offset.y;

gridPanel.setRect(x, y);

}

}

}

74philipa at 2007-7-15 23:56:09 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...