jtable 行的动态(真实)上下文菜单
Dynamic (real) context menu for jtable row
我希望上下文菜单中有不同的菜单项,具体取决于我在 JTable 中单击的行
大多数示例并没有真正显示上下文菜单(应该根据上下文 - 所选行进行填充)
我试过这个:
popupMenu = new JPopupMenu(){
@Override
public void show(Component invoker, int x, int y) {
int rowAtPoint = table.rowAtPoint(SwingUtilities.convertPoint(this, new Point(x, y), table));
FilesManager.this.generateTablePopupMenu(rowAtPoint);
super.show(invoker, x, y);
}
};
其中 generateTablePopupMenu 是 adding/removing 个菜单项,具体取决于行数据
但它不起作用,索引 (rowAtPoint) 没有 return 正确的值
JPopupMenu#show(int, int) (Java Platform SE 8)
public void show(Component invoker, int x, int y)
Displays the popup menu at the position x,y in the coordinate space of the component invoker.
Parameters:
- invoker - the component in whose space the popup menu is to appear
- x - the x coordinate in invoker's coordinate space at which the popup menu is to be displayed
- y - the y coordinate in invoker's coordinate space at which the popup menu is to be displayed
因此,没有必要使用SwingUtilities.convertPoint(...)
方法转换坐标。
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class JTablePopupMenuTest {
public JComponent makeUI() {
JTable table = new JTable(new DefaultTableModel(5, 3));
table.setFillsViewportHeight(true);
JPopupMenu popupMenu = new JPopupMenu() {
@Override
public void show(Component invoker, int x, int y) {
//int rowAtPoint = table.rowAtPoint(
// SwingUtilities.convertPoint(this, new Point(x, y), table));
//FilesManager.this.generateTablePopupMenu(rowAtPoint);
int rowAtPoint = table.rowAtPoint(new Point(x, y));
System.out.println(rowAtPoint);
super.show(invoker, x, y);
}
};
table.setComponentPopupMenu(popupMenu);
JPanel p = new JPanel(new BorderLayout());
p.add(new JScrollPane(table));
return p;
}
public static void main(String... args) {
EventQueue.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new JTablePopupMenuTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}
我希望上下文菜单中有不同的菜单项,具体取决于我在 JTable 中单击的行
大多数示例并没有真正显示上下文菜单(应该根据上下文 - 所选行进行填充)
我试过这个:
popupMenu = new JPopupMenu(){
@Override
public void show(Component invoker, int x, int y) {
int rowAtPoint = table.rowAtPoint(SwingUtilities.convertPoint(this, new Point(x, y), table));
FilesManager.this.generateTablePopupMenu(rowAtPoint);
super.show(invoker, x, y);
}
};
其中 generateTablePopupMenu 是 adding/removing 个菜单项,具体取决于行数据
但它不起作用,索引 (rowAtPoint) 没有 return 正确的值
JPopupMenu#show(int, int) (Java Platform SE 8)
public void show(Component invoker, int x, int y)
Displays the popup menu at the position x,y in the coordinate space of the component invoker.
Parameters:
- invoker - the component in whose space the popup menu is to appear
- x - the x coordinate in invoker's coordinate space at which the popup menu is to be displayed
- y - the y coordinate in invoker's coordinate space at which the popup menu is to be displayed
因此,没有必要使用SwingUtilities.convertPoint(...)
方法转换坐标。
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class JTablePopupMenuTest {
public JComponent makeUI() {
JTable table = new JTable(new DefaultTableModel(5, 3));
table.setFillsViewportHeight(true);
JPopupMenu popupMenu = new JPopupMenu() {
@Override
public void show(Component invoker, int x, int y) {
//int rowAtPoint = table.rowAtPoint(
// SwingUtilities.convertPoint(this, new Point(x, y), table));
//FilesManager.this.generateTablePopupMenu(rowAtPoint);
int rowAtPoint = table.rowAtPoint(new Point(x, y));
System.out.println(rowAtPoint);
super.show(invoker, x, y);
}
};
table.setComponentPopupMenu(popupMenu);
JPanel p = new JPanel(new BorderLayout());
p.add(new JScrollPane(table));
return p;
}
public static void main(String... args) {
EventQueue.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new JTablePopupMenuTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}