当按下键盘按钮(即CTRL按钮)并选择一行时,是否有一种简单的方法来操纵JTable上的控件以提供不同的功能?我被要求创建一个表,其中行上的CTRL +单击(鼠标单击)将仅取消选择所选行,从不选择行.如果用户按CTRL +单击未选择的行,则不会发生任何事情.
我已经能够创建一个表,并禁用CTRL + A(全选)等功能,并且我已经能够检查生成MouseEvent时是否按下了控制按钮,但我似乎无法想象如何调整CTRL + Click.这是一些代码:
package nicky; import javax.swing.*; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.*; public class TableTester extends JPanel { public TableTester() { super(new GridLayout(1,0)); final String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"}; final Object[][] data = { {"Tom", "Roberts","Athletic", new Integer(5), new Boolean(false)}, {"Sarah", "Watt", "Football", new Integer(3), new Boolean(true)}, {"Laura", "Brown", "Swimming", new Integer(2), new Boolean(false)}, {"Simon", "Smith", "Tennis", new Integer(20), new Boolean(true)}, {"Paul", "Jones", "Rugby", new Integer(10), new Boolean(false)} }; JTable table = new JTable(data, columnNames); table.setPreferredScrollableViewportSize(new Dimension(500, 100)); table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); table.addMouseListener(new MouseListener(){ public void mouseEntered(MouseEvent me){} public void mouseExited(MouseEvent me){} public void mouseReleased(MouseEvent me){} public void mouseClicked(MouseEvent me){} public void mousePressed(MouseEvent me){ if (me.isControlDown()){ System.out.println("This is working "); } } }); InputMap inputMap = table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK); inputMap.put(keyStroke, "none"); JScrollPane scrollPane = new JScrollPane(table); add(scrollPane); } private static void createAndShowGUI() { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("TableTester"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); TableTester newContentPane = new TableTester(); newContentPane.setOpaque(true); frame.setContentPane(newContentPane); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
在mousePressed方法中,我玩弄了从表中获取所有选定的行,然后检查新单击的行是否在selectedRows中...但是,我不确定是否有办法查看与MouseEvent关联的行.
(另外,我知道这样的预期行为不应该用太多,但它是复制公司中的遗留系统)
任何想法/建议将不胜感激!
好的,第二次(我离开了第一个,因为它可能会引起某些人的兴趣,其他用法,谁知道?说它是出于教育目的...... :-)).
我看了一下JTable的源代码,发现鼠标事件是由外观处理的.知道它如何处理控制键,我可以安全地覆盖changeSelection方法来做你需要的.
我发现需求有点奇怪(你仍然可以使用Shift +点击,不是吗?)但我不知道上下文.
class SpecialTable extends JTable { SpecialTable(Object[][] data, String[] columnNames) { super(data, columnNames); // That's already the default // setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); } /** * Called by javax.swing.plaf.basic.BasicTableUI.Handler.adjustSelection(MouseEvent) * like: table.changeSelection(pressedRow, pressedCol, e.isControlDown(), e.isShiftDown()); */ @Override public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend) { if (toggle && !isRowSelected(rowIndex)) return; // Don't do the selection super.changeSelection(rowIndex, columnIndex, toggle, extend); } }
更简单,更准确的是你需要的!
顺便说一下,感谢提供这么简单的好测试用例,如果我不得不自己编写它,我可能没试过...... :-这是一个有趣的学习挑战.