Java 通过鼠标右键单击使用JPOppMenu在JTable中选择行
下面的代码创建一个简单的Java 通过鼠标右键单击使用JPOppMenu在JTable中选择行,java,popup,jtable,point,Java,Popup,Jtable,Point,下面的代码创建一个简单的JFrame,其中包括一个JTable和jpopmpmenu。右键单击选择单击的行并显示弹出窗口 问题是: 当用户最大化屏幕的JFrame并右键单击最后1-2-3-4-5或第6行时,弹出窗口出现在错误的位置并选择错误的行,因为弹出窗口高度对应于屏幕高度 我怎样才能做到这一点 MCVE package popuptestapp; import java.awt.BorderLayout; import java.awt.Container; import java.awt
JFrame
,其中包括一个JTable
和jpopmpmenu
。右键单击选择单击的行并显示弹出窗口
问题是:
当用户最大化屏幕的JFrame并右键单击最后1-2-3-4-5或第6行时,弹出窗口出现在错误的位置并选择错误的行,因为弹出窗口高度对应于屏幕高度
我怎样才能做到这一点
MCVE强>
package popuptestapp;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.MouseInfo;
import java.awt.Point;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import javax.swing.table.DefaultTableModel;
public class PopupTestApp {
public static boolean RIGHT_TO_LEFT = false;
public static void addComponentsToPane(Container pane) {
if (!(pane.getLayout() instanceof BorderLayout)) {
pane.add(new JLabel("Container doesn't use BorderLayout!"));
return;
}
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(
java.awt.ComponentOrientation.RIGHT_TO_LEFT);
}
JScrollPane jScrollPane = new JScrollPane();
JTable table = new JTable();
table.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{"", "", "", "", "", "", "", ""}
},
new String [] {
"Column1", "Column2", "Column3", "Column4", "Column5", "Column6", "Column7", "Column8"
}
) {
boolean[] canEdit = new boolean [] {
false, false, false, false, false, false, false, false
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
DefaultTableModel model1 = (DefaultTableModel) table.getModel();
model1.setRowCount(0);
//Fill the table with rows
for (Integer i = 0; i<=250; i++){
model1.addRow(new String[]{"", "", "", "", "", "", "", ""});
}
jScrollPane.setViewportView(table);
pane.add(jScrollPane, BorderLayout.PAGE_END);
//creating JPopupMenu
JPopupMenu popupForTable = new JPopupMenu();
JMenuItem menuItem1;
JMenuItem menuItem2;
JMenuItem menuItem3;
JMenuItem menuItem4;
JMenuItem menuItem5;
JMenuItem menuItem6;
JMenuItem menuItem7;
JMenuItem menuItem8;
JMenuItem menuItem9;
JMenuItem menuItem10;
popupForTable.add(menuItem1 = new JMenuItem ("menuItem1"));
popupForTable.add(menuItem2 = new JMenuItem ("menuItem2"));
popupForTable.add(menuItem3 = new JMenuItem ("menuItem3"));
popupForTable.add(menuItem4 = new JMenuItem ("menuItem4"));
popupForTable.add(menuItem5 = new JMenuItem ("menuItem5"));
popupForTable.add(menuItem6 = new JMenuItem ("menuItem6"));
popupForTable.add(menuItem7 = new JMenuItem ("menuItem7"));
popupForTable.add(menuItem8 = new JMenuItem ("menuItem8"));
popupForTable.add(menuItem9 = new JMenuItem ("menuItem9"));
popupForTable.add(menuItem10 = new JMenuItem ("menuItem10"));
popupForTable.addPopupMenuListener(new PopupMenuListener() {
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Point mousePoint = new Point();
mousePoint = MouseInfo.getPointerInfo().getLocation();
int rowAtPoint = table.rowAtPoint(SwingUtilities.convertPoint(popupForTable, new Point(0, 0), table));
if (rowAtPoint > -1) {
table.setRowSelectionInterval(rowAtPoint, rowAtPoint);
}
}
});
}
@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
// TODO Auto-generated method stub
}
@Override
public void popupMenuCanceled(PopupMenuEvent e) {
// TODO Auto-generated method stub
}
});
table.setComponentPopupMenu(popupForTable);
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("BorderLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(500, 500));
//Set up the content pane.
addComponentsToPane(frame.getContentPane());
//Use the content pane's default BorderLayout. No need for
//setLayout(new BorderLayout());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
/* Use an appropriate Look and Feel */
try {
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
/* Turn off metal's use bold fonts */
UIManager.put("swing.boldMetal", Boolean.FALSE);
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
package-popuptestapp;
导入java.awt.BorderLayout;
导入java.awt.Container;
导入java.awt.Dimension;
导入java.awt.MouseInfo;
导入java.awt.Point;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JMenuItem;
导入javax.swing.jpopmenu;
导入javax.swing.JScrollPane;
导入javax.swing.JTable;
导入javax.swing.SwingUtilities;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
导入javax.swing.event.PopupMenuEvent;
导入javax.swing.event.PopupMenuListener;
导入javax.swing.table.DefaultTableModel;
公共类PopupTestApp{
公共静态布尔值从右到左=false;
公共静态void addComponentsToPane(容器窗格){
if(!(pane.getLayout()实例of BorderLayout)){
添加(新的JLabel(“容器不使用BorderLayout!”);
返回;
}
如果(从右到左){
pane.setComponentOrientation(
java.awt.ComponentOrientation.RIGHT\u TO\u LEFT);
}
JScrollPane JScrollPane=新的JScrollPane();
JTable table=新的JTable();
table.setModel(新的javax.swing.table.DefaultTableModel(
新对象[][]{
{"", "", "", "", "", "", "", ""}
},
新字符串[]{
“第1栏”、“第2栏”、“第3栏”、“第4栏”、“第5栏”、“第6栏”、“第7栏”、“第8栏”
}
) {
boolean[]canEdit=新的boolean[]{
假,假,假,假,假,假,假,假,假,假
};
公共布尔值isCellEditable(int-rowIndex、int-columnIndex){
返回canEdit[columnIndex];
}
});
table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
DefaultTableModel1=(DefaultTableModel)table.getModel();
model1.setRowCount(0);
//用行填充表格
对于(整数i=0;i-1){
表.setRowSelectionInterval(rowAtPoint,rowAtPoint);
}
}
});
}
@凌驾
public void PopupMenu将变得不可见(PopupMenuEvent e){
//TODO自动生成的方法存根
}
@凌驾
公共无效popupMenuCanceled(PopupMenuEvent e){
//TODO自动生成的方法存根
}
});
table.setComponentPopupMenu(popupForTable);
}
私有静态void createAndShowGUI(){
//创建并设置窗口。
JFrame=新JFrame(“BorderLayoutDemo”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(新尺寸(500500));
//设置内容窗格。
addComponentsToPane(frame.getContentPane());
//使用内容窗格的默认边框布局。无需
//setLayout(新的BorderLayout());
//显示窗口。
frame.pack();
frame.setVisible(true);
}
公共静态void main(字符串[]args){
/*使用合适的外观和感觉*/
试一试{
//UIManager.setLookAndFeel(“com.sun.java.swing.plaf.windows.WindowsLookAndFeel”);
setLookAndFeel(“javax.swing.plaf.metal.MetalLookAndFeel”);
}捕获(无支持的LookandFeelexception ex){
例如printStackTrace();
}捕获(非法访问例外){
例如printStackTrace();
}catch(实例化异常){
例如printStackTrace();
}捕获(ClassNotFoundException ex){
例如printStackTrace();
}
/*禁用metal的“使用粗体字体”*/
UIManager.put(“swing.boldMetal”,Boolean.FALSE);
//为事件分派线程计划作业:
//创建并显示此应用程序的GUI。
javax.swing.SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
createAndShowGUI();
}
});
}
}
我认为这是一个很好的示例,说明了如何使用/创建jpopupmenu 看一看
因此,请更正您的转换点()
我解决了这个问题。首先要感谢有问题的@clamp
---已解决---
我已经从代码中删除了以下行
popupForTable.addPopupMenuListener(new PopupMenuListener() {
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Point mousePoint = new Point();
mousePoint = MouseInfo.getPointerInfo().getLocation();
int rowAtPoint = table.rowAtPoint(SwingUtilities.convertPoint(popupForTable, new Point(0, 0), table));
if (rowAtPoint > -1) {
table.setRowSelectionInterval(rowAtPoint, rowAtPoint);
}
}
});
}
@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
// TODO Auto-generated method stub
}
@Override
public void popupMenuCanceled(PopupMenuEvent e) {
// TODO Auto-generated method stub
}
});
table.setComponentPopupMenu(popupForTable);
并将此MouseListener
添加到表中
table.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
int r = table.rowAtPoint(e.getPoint());
if (r >= 0 && r < table.getRowCount()) {
table.setRowSelectionInterval(r, r);
} else {
table.clearSelection();
}
int rowindex = table.getSelectedRow();
if (rowindex < 0)
return;
if (e.isPopupTrigger() && e.getComponent() instanceof JTable ) {
JPopupMenu popup = popupForTable;
popup.show(e.getComponent(), e.getX(), e.getY());
table.setRowSelectionInterval(r, r);
}
}
});
table.addMouseListener(新的MouseAdapter(){
公共无效MouseEvent(MouseEvent e){
intr=table.rowAtPoint(如getPoint());
如果(r>=0&&r
谢谢所有对这个问题感兴趣的人。也许这个解决方案对将来的人有帮助。我试过这个。这是工作,但不稳定。例如运行应用程序并将其最大化。然后右键单击第二行上li的1-2像素下方
table.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
int r = table.rowAtPoint(e.getPoint());
if (r >= 0 && r < table.getRowCount()) {
table.setRowSelectionInterval(r, r);
} else {
table.clearSelection();
}
int rowindex = table.getSelectedRow();
if (rowindex < 0)
return;
if (e.isPopupTrigger() && e.getComponent() instanceof JTable ) {
JPopupMenu popup = popupForTable;
popup.show(e.getComponent(), e.getX(), e.getY());
table.setRowSelectionInterval(r, r);
}
}
});