Skip to main content

How to put a jpopup menue in a cell of jtable?

5 replies [Last post]
miroslav
Offline
Joined: 2006-06-10
Points: 0

Hello,
I want to make a jtable in which I want in one of the columns each cell when clicked(with right button) to pop up a jpopu menu with 3 actions. First I thought to make it with combobox but because I want to be activated with right button and then with the left i have changed my mind to jpopup menu.
Can you tell me what kind of render and editor to use? I will be very grateful if you can send me some examples because I am new to the swing.

Thanx,
Miroslav

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
shan-man
Offline
Joined: 2006-02-17
Points: 0

Hi Miroslav,

> Hello,
> I want to make a jtable in which I want in one of the
> columns each cell when clicked(with right button) to
> pop up a jpopu menu with 3 actions.

> Can you tell me what kind of render and editor to
> use?

You don't need to use any special renderer or editor. You just need to add some popup menu functionality. Here's a sample application for you with some framework. It adds popup functionality to the JTable. Right clicking in the second column will bring up a popup menu with two menu items that have been customized for the row that you've clicked on.
[code]
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
* Demo of JPopupMenu use with JTable.
* Popups have been added to the second column.
*
* @author Shannon Hickey
*/
public class TabPopTest extends JFrame {

private JTable table;
private JPopupMenu pm;
private JMenuItem one = new JMenuItem();
private JMenuItem two = new JMenuItem();

public TabPopTest() {
super("TabPopTest");

String[] columnNames = {"Column 0", "Column 1", "Column 2"};
String[][] data = new String[10][3];

for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
data[i][j] = "Table " + i + ", " + j;
}
}

table = new JTable(data, columnNames);
pm = new JPopupMenu();
pm.add(one);
pm.add(two);

getContentPane().add(new JScrollPane(table));

// should listen to both pressed and released
// since either could be the trigger,
// depending on the look and feel
table.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
showPopup(me);
}

public void mouseReleased(MouseEvent me) {
showPopup(me);
}
});
}

private void showPopup(MouseEvent me) {
// is this event a popup trigger?
if (pm.isPopupTrigger(me)) {
Point p = me.getPoint();
int row = table.rowAtPoint(p);
int col = table.columnAtPoint(p);
// if we've clicked on a row in the second col
if (row != -1 && col == 1) {
one.setText("Do something to row " + row + ".");
two.setText("Do something else to row " + row + ".");
pm.show(table, p.x, p.y);
}
}
}

public static void main(String[] args) {
TabPopTest test = new TabPopTest();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.setSize(400, 400);
test.setLocationRelativeTo(null);
test.setVisible(true);
}
}
[/code]
Hope this helps!
Shannon

miroslav
Offline
Joined: 2006-06-10
Points: 0

Hi,
Thank you both, and especially to you shan-man. The code that you send helped me a lot and saved me a lot of problems. I thought I should do the tedious renders and editors... So thank you a lot and keep helping :)

Miroslav

kwalrath
Offline
Joined: 2006-02-17
Points: 0

Hi,

I suspect you could use the default renderer and then make a custom editor that's an arbitrary component (maybe a button or label) with a popup menu registered on it.

You can find an example of making a custom editor in The Java Tutorial:

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#ed...

The coverage is a little old, verging on obsolete, but I'll attach the code from the new version (due to be published on java.sun.com any week now) below.

Popup menus are covered here:

http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html#popup

-k-

New code for ColorEditor:

[code]
public class ColorEditor extends AbstractCellEditor
implements TableCellEditor,
ActionListener {
Color currentColor;
JButton button;
JColorChooser colorChooser;
JDialog dialog;
protected static final String EDIT = "edit";

public ColorEditor() {
button = new JButton();
button.setActionCommand(EDIT);
button.addActionListener(this);
button.setBorderPainted(false);

//Set up the dialog that the button brings up.
colorChooser = new JColorChooser();
dialog = JColorChooser.createDialog(button,
"Pick a Color",
true, //modal
colorChooser,
this, //OK button handler
null); //no CANCEL button handler
}

public void actionPerformed(ActionEvent e) {
if (EDIT.equals(e.getActionCommand())) {
//The user has clicked the cell, so
//bring up the dialog.
button.setBackground(currentColor);
colorChooser.setColor(currentColor);
dialog.setVisible(true);

fireEditingStopped(); //Make the renderer reappear.

} else { //User pressed dialog's "OK" button.
currentColor = colorChooser.getColor();
}
}

//Implement the one CellEditor method that AbstractCellEditor doesn't.
public Object getCellEditorValue() {
return currentColor;
}

//Implement the one method defined by TableCellEditor.
public Component getTableCellEditorComponent(JTable table,
Object value,
boolean isSelected,
int row,
int column) {
currentColor = (Color)value;
return button;
}
}[/code]

shan-man
Offline
Joined: 2006-02-17
Points: 0

Hi,

> Hi,
>
> I suspect you could use the default renderer and then
> make a custom editor that's an arbitrary component
> (maybe a button or label) with a popup menu
> registered on it.

This approach would enable the popup only on a cell that is being edited. If no cell is being edited, then the editor isn't part of the containment hierarchy. Therefore there the popups of that component would never show up. This would likely be the approach you'd take if you only wanted popups on a cell while editing.

:) Shannon

kwalrath
Offline
Joined: 2006-02-17
Points: 0

Yep. D'oh!

-k-