Right Click Functionality with options like Refresh and Copy

The scenario is like this, in a GUI JTable , when i right click on some cell. I should get the screen similar in windows but i should add the funtionality of copy and cancel . Can any one resolve this? Any one help is appreciated.
[237 byte] By [PashaGeeka] at [2007-10-2 1:05:01]
«« JComboBox
»» XSL
# 1

i assume that u want to copy the contents of the cell...

now i assmune also that u can add popup menus thats easy one

now the main thing is how to get the content...then the trick is to get the co-ordinate of click event

then get the row and col by getRowAt and getColAt

the use getValueAt to get the content...ok

do all this in a method and call the method with MouseEvent from where u can get the co-ordinates

hope this will solve ur prob... because i have done sth like that

have fun in java

i_virusa at 2007-7-15 18:25:43 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...
# 2

here is a sample for u, i have used Eclipse

/*

* Created on Sep 28, 2005

*

*/

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import javax.swing.*;

/**

* Simple class to show how to create and add your own popup

* for menu in a table

*

* @author i_virus

**/

public class TablePopUp extends JFrame {

Containercontainer;

JTabletable;

JPopupMenupopupmenu;

JButtoncopyB;

JButtoncancelB;

intxc, yc;

final Stringcol[]= { "A", "B", "C" };

final Stringrows[][]= { { "A1", "B1", "C1" }, { "A2", "B2", "C2" },

{ "A3", "B3", "C3" } };

final StringpopupMenu[]= { "Copy", "Cancel" };

public TablePopUp()

{

super("Table Popup Menu Example");

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setupPopup();

}

private void setupPopup()

{

this.popupmenu = new JPopupMenu();

this.copyB = new JButton(this.popupMenu[0]);

copyB.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)

{

tableRightClickHandler(e);

}

});

this.cancelB = new JButton(this.popupMenu[1]);

cancelB.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)

{

tableRightClickHandler(e);

}

});

this.popupmenu.add(copyB);

this.popupmenu.add(cancelB);

}

public void showGUI()

{

container = getContentPane();

container.setLayout(new GridLayout(1, 0));

this.table = new JTable(this.rows, this.col);

// i have used array for creating the table you can use

// other things as well like Vectors,TableModel etc.

this.table.addMouseListener(new MouseAdapter() {

public void mouseReleased(MouseEvent e)

{

if(e.isPopupTrigger()) {

//store the position where popup was called

xc = e.getX();

yc = e.getY();

//show the popup

popupmenu.show(e.getComponent(), e.getX(), e.getY());

}

}

});

container.add(table);

this.setSize(400, 300);

this.pack();

this.show();

}

private void tableRightClickHandler(ActionEvent e)

{

if(e.getActionCommand().equals("Copy")) {

//if Copy was clicked in the popup menu

//get the row no.

int row = this.table.rowAtPoint(new Point(this.xc, this.yc));

//get the col no.

int col = this.table.columnAtPoint(new Point(this.xc, this.yc));

//get the data in row col

String data = this.table.getValueAt(row, col).toString();

//do sth with the data

System.out.println("Copy: " + data);

}

//finally hide the popup menu

this.popupmenu.setVisible(false);

}

public static void main(String[] args)

{

TablePopUp tp = new TablePopUp();

tp.showGUI();

}

}

hope this will solve ur problem at least

i_virusa at 2007-7-15 18:25:43 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...