setComponentPopupMenu
とJTable
にJPopupMenu
を追加しました。問題は、JPopupMenu
が開いている/見えているうちに、ポップアップメニューの外にある行を左クリックしたときにメニューが閉じても行が選択されていないため、再度強調表示する必要があることです。それを修正する方法はありますか?表示されているJPopupMenuの外側をクリックすると、JTableの行選択が強制的に実行されます
EDIT
私は、サンプルコードを追加しました。
ところでこの現象はWindows LaFでのみ発生します。私はちょうどそれをテストしたところで、JPopupMenuが開いている間、デフォルトのJava LaFが左クリックの行選択を許可するように見えます。
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();
}
});
}
}
[ 'TablePopupEditor'(http://stackoverflow.com/a/3591230/230513)のようなセルエディタを使用。重複していない場合は、あなたの修正されたアプローチを示す[mcve]を含めるように質問を編集してください。 – trashgod
これはもともとはhttp://stackoverflow.com/questions/3590897/jtable-design-to-synchronize-with-back-end-data-structureの複製としてクローズされていました。私はこの質問がポップアップエディタに関するものだとは思わない。質問は "JPopupMenu"を使用し、メニューが開いているときにテーブルをクリックすることです。私はその質問を再開した。 – camickr
質問を編集しました。 – AndroidX