- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已使用 setComponentPopupMenu
将 JPopupMenu
添加到 JTable
。问题是,虽然 JPopupMenu 打开/可见,但当我左键单击弹出菜单外部的行时,菜单会关闭,但该行未被选中,因此我必须再次单击它才能突出显示它。有什么办法可以解决这个问题吗?
编辑
我添加了示例代码。
顺便说一句,此行为仅发生在 Windows LaF 中。我刚刚测试过它,看来默认的 Java LaF 允许在 JPopupMenu 打开时左键单击行选择。
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Point;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
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;
public class TableSTACK {
private static void createAndShowGUI() {
String[] headers = {"Column 1", "Column 2"};
Object[][] data = { {"Row", "1"}, {"Row", "2"},
{"Row", "3"}, {"Row", "4"}, {"Row", "5"},
{"Row", "6"}, {"Row", "7"}, {"Row", "8"}, };
JTable table = new JTable(data, headers);
table.setFillsViewportHeight(true);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
final JMenuItem item1 = new JMenuItem();
item1.setText("Menu Item 1");
final JMenuItem item2 = new JMenuItem();
item2.setText("Menu Item 2");
final JMenuItem item3 = new JMenuItem();
item3.setText("Menu Item 3");
final JPopupMenu popupMenu = new JPopupMenu();
popupMenu.add(item1);
popupMenu.addSeparator();
popupMenu.add(item2);
popupMenu.add(item3);
table.setComponentPopupMenu(popupMenu);
popupMenu.addPopupMenuListener(new PopupMenuListener() {
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// force selection of row upon right-click (it works)
int rowAtPoint = table.rowAtPoint(SwingUtilities.convertPoint(popupMenu, new Point(0, 0), table));
if (rowAtPoint > -1) {
table.setRowSelectionInterval(rowAtPoint, rowAtPoint);
}
}
});
}
@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// force row selection upon exiting popup menu
// does not work; rowAtPoint always returns -1
int rowAtPoint = table.rowAtPoint(SwingUtilities.convertPoint(null, new Point(0, 0), table));
//int rowAtPoint = table.rowAtPoint(SwingUtilities.convertPoint(popupMenu, new Point(0, 0), table));
if (rowAtPoint > -1) {
table.setRowSelectionInterval(rowAtPoint, rowAtPoint);
}
}
});
}
@Override
public void popupMenuCanceled(PopupMenuEvent e) {
// TODO
}
});
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
JScrollPane scrollPane = new JScrollPane(table);
JPanel contentPane = new JPanel();
contentPane.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
contentPane.setLayout(new GridBagLayout());
contentPane.add(scrollPane, gbc);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(contentPane);
frame.pack();
frame.setMinimumSize(new Dimension(500, 400));
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
//UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
createAndShowGUI();
}
});
}
}
最佳答案
这是 LAF 问题。
当我使用默认的 LAF 时,它适用于我,但当我使用平台 LAF 时,它不起作用,对我来说,该平台是 Windows。
Windows 上的一个潜在解决方案是使用 MouseListener
选择线路。请注意,代码已添加到 mouseReleased
事件。由于某种原因,该表没有收到 mousePressed
事件即使根据 AWTEventListener
该表是 mousePressed
的来源事件。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TablePopupListener extends JPanel
{
public TablePopupListener()
{
JTable table = new JTable(10, 5);
add( new JScrollPane( table ) );
JPopupMenu popup = new JPopupMenu();
popup.add( new JMenuItem("Do Something1") );
popup.add( new JMenuItem("Do Something2") );
table.setComponentPopupMenu( popup );
table.addMouseListener( new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
System.out.println("Pressed JTable");
}
public void mouseReleased(MouseEvent e)
{
System.out.println("Released JTable");
int row = table.rowAtPoint( e.getPoint() );
if (row != -1
&& !table.isRowSelected(row))
{
table.setRowSelectionInterval(row, row);
}
}
});
}
private static void createAndShowGUI()
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception ex) { System.out.println(ex); }
JFrame frame = new JFrame("TablePopupListener");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TablePopupListener());
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
Toolkit.getDefaultToolkit().addAWTEventListener( new AWTEventListener()
{
public void eventDispatched(AWTEvent e)
{
String event = null;
switch (e.getID())
{
case MouseEvent.MOUSE_PRESSED: event = "Pressed: " ; break;
case MouseEvent.MOUSE_RELEASED: event = "Released: " ; break;
case MouseEvent.MOUSE_ENTERED: event = "Entered: " ; break;
case MouseEvent.MOUSE_EXITED: event = "Exited: " ; break;
default: event = null; break;
}
if (event != null)
{
System.out.println();
System.out.println(event + e.getSource().getClass());
}
}
}, AWTEvent.MOUSE_EVENT_MASK);
}
public static void main(String[] args)
{
EventQueue.invokeLater( () -> createAndShowGUI() );
/*
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
*/
}
}
关于java - 如果单击可见 JPopupMenu 外部,则强制选择 JTable 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40273424/
我有一个 Java 小程序,它将由用户必须与之交互的几个弹出菜单组成。但是,添加时 JPopupMenu 不会显示。这是我的代码: public class Parser extends JApple
我正在尝试创建带有两个弹出菜单的系统托盘图标,一个应该用鼠标左键调用,另一个应该用鼠标右键调用。 AWT 提供了创建 PopupMenu 的简单方法,但在不创建不可见的 JFrame 或其他内容的情况
在上一个问题中,我向社区询问为什么我的 JPopupMenu 没有出现在屏幕上。 我无法想出一个简单的、可运行的、可编译的示例。 所以,这就是我为你们所做的: 区域太小,无法绘制弹出窗口吗? 我希望我
我有下一个结构:JPopupMenu包含JPanel,其中包含JMenuItems。问题是,我无法使用它,因为当鼠标进入任何菜单项时,JPopupMenu就会消失。 SSCCE: public cla
这是我的 JPopupMenu 的代码以及我如何添加它,当我右键单击表格时它应该做出响应: JMenuItem deleteRows = new JMenuItem("Delete Row")
我有一个弹出菜单,其中包含一个带有图标 (I) 的 JMenuItem 和一个 JList。见下图。 ------------------------ | I | Clear |
我正在制作一个程序,它有一个带有两个按钮的弹出菜单,其中一个按钮应该关闭弹出菜单,但我不知道如何做到这一点,而且谷歌搜索也不太顺利。 我尝试过使用 popup.hide() 但菜单不会回来,尽管当我尝
我想在翻转事件时更改 jMenuItems 的背景颜色: 现在它是蓝色的,我想要白色的,我该怎么办? (我使用netbeans GUI) 最佳答案 你可以使用类似的东西: UIManager.put(
我在 JTable 上的弹出菜单以及该 JTable 允许多个间隔选择这一事实上遇到问题。我将详细解释我的情况,希望尽可能清楚。 我有一个基本数据类,我们称之为 Item,带有一个字符串 id(名称)
我有一个用于运行锦标赛的 Java 应用程序,在其中我构建了一个自动建议功能,该功能从数据库中获取名称并将其显示在 JPopupMenu 中。我无法根据需要复制这个错误,但偶尔一个 JPopupMen
我编写了一个简单的程序来了解 JPopupMenu 的工作原理。但我的代码有问题,它没有正确显示弹出菜单。请问谁能告诉我原因吗? public class PopUpMenu extends JFra
有关 JPopupMenu 行为的问题。我希望 JPopupMenu 在出现时不要失去焦点。此外,当 JPopupMenu 获得焦点时,用户应该能够单击/更新 Applet 的其他部分。 这可能吗?原
因此,对于我的学校项目,我正在创建一个类图制作器。我已经完成了 95%,我所需要的就是让 Jpopup 菜单出现。在核心中我有 3 个文件。扩展 JFrame 的 ApplicationModel、扩
我有一个弹出菜单,当鼠标悬停在组件上时会打开该菜单。现在我希望能够在弹出窗口仍然打开时开始拖动组件,但弹出菜单关闭事件总是消耗第一次鼠标单击。有解决方法吗? 最佳答案 public clas
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 7 年前。 Improve this ques
我需要添加右键JPopupMenu到世界风显示。世界风显示位于 JPanel 。我几乎只是复制了ApplicationTemplate.AppPanel的成员变量和方法。 World Wind 示例中
下面我准备了一个小测试用例。我的问题是当我右键单击窗口时。 JPopupMenu 出现,但如果我单击 JWindow 菜单外的任何地方,它不会消失。我必须单击窗口上的某处才能摆脱它,这不是预期的行为。
我正在用 Java Swing 编写一个程序,在这个程序中会有一个搜索栏(这将是一个 JTextField),当在此字段中输入内容时,我想直接在弹出菜单中显示搜索结果在搜索栏下方,就像谷歌一样。我不必
如何获取当前显示在屏幕上的所有 JPopupMenu 组件的列表。 或者获取所有 JPopupMenu 组件,然后可以通过可见性、有效性等进行过滤。 我需要这个来编写测试。卡在 JPopupMenu-
我需要删除 JPopupMenu 中的背景和 Border。 JPopupMenu 应该是完全透明的。覆盖 paintComponent 不会给出肯定的结果。我也尝试在 BasicMenuItemUI
我是一名优秀的程序员,十分优秀!