禁用JTable上的手形光标

时间:2014-11-06 10:45:08

标签: java swing jtable jcomponent mouse-cursor

我在JTable中有一个JScrollPane使用enabled(false)禁用了table.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); ,但我还是希望在翻转时更改为手形光标。我试过手动设置表格的光标:

JTable table = new JTable(tableModel){
   @Override
   public Cursor getCursor() {
       return Cursor.getPredefinedCursor(Cursor.HAND_CURSOR); //DOESNT WORK
   }
};

我已经尝试覆盖JTable的getCursor()方法:

mouseMotionListener

但是这两种技术都不行。我必须使用的一种方法是在整个表中添加setEnabled,但问题是在实际应用程序中按下驻留在表中的按钮会导致显示“JOptionpane”;如果用户继续将光标移出表格并关闭'JOptionPane',则可能发生错误。肯定是'MouseMotionListener'过多..?

我还研究了覆盖JTable Component的方法(以及随后的import java.awt.*; import javax.swing.*; import javax.swing.table.DefaultTableModel; public class TableCursorTest { public TableCursorTest(){ JFrame frame = new JFrame(); JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints constraints = new GridBagConstraints(); String[] headers = {"Items"}; Object[][] data = new Object[][]{{"item 1"},{"item 2"}}; DefaultTableModel tableModel = new DefaultTableModel(data, headers); @SuppressWarnings("serial") @Override public Cursor getCursor() { return Cursor.getPredefinedCursor(Cursor.HAND_CURSOR); //DOESNT WORK } }; JScrollPane scrollPane = new JScrollPane(table); table.setEnabled(false); table.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); //DOESNT WORK constraints.gridx = 0; constraints.gridy = 0; constraints.weightx = 0; constraints.weighty = 0; panel.add(scrollPane, constraints); frame.setContentPane(panel); frame.setPreferredSize(new Dimension(500,500)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } public static void main(String[] args){ new TableCursorTest(); } } ),但是我正在努力找出哪些代码阻止了手形光标的显示。任何帮助将非常感激!代码如下:

{{1}}

更新:抱歉,问题是缺少一些信息:表格并不总是填充ScrollPane,因此设置滚动窗格的手形光标会导致手形光标覆盖不存在的行。如果可能,手形光标应仅显示在表格的行上。

3 个答案:

答案 0 :(得分:3)

table.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); //DOESNT WORK

虽然..

scrollPane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); //DOES WORK

答案 1 :(得分:0)

如果您打算disable editing,则可以覆盖表格模型的isCellEditable方法documentation

此外,如果您需要disable row selection,则可以将rowSelectionAllowed设置为false

您可以在表格中添加mouseListener并覆盖自定义游标的mouseEnteredmouseExited,例如

    table.addMouseListener(new MouseAdapter()
    {

        @Override
        public void mouseExited(MouseEvent e)
        {
            table.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        }

        @Override
        public void mouseEntered(MouseEvent e)
        {
            table.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        }

    });

您的完整工作示例:

public class TableCursorTest
{
    JFrame frame = new JFrame();
    JPanel panel = new JPanel(new GridBagLayout());
    GridBagConstraints constraints = new GridBagConstraints();
    String[] headers = { "Items" };
    Object[][] data = new Object[][] { { "item 1" }, { "item 2" } };
    @SuppressWarnings("serial")
    DefaultTableModel tableModel = new DefaultTableModel(data, headers)
    {
        public boolean isCellEditable(int row, int column)
        {
            return false;
        };
    };

    public TableCursorTest()
    {
        final JTable table = new JTable(tableModel);

        table.addMouseListener(new MouseAdapter()
        {

            @Override
            public void mouseExited(MouseEvent e)
            {
                table.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            }

            @Override
            public void mouseEntered(MouseEvent e)
            {
                table.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            }

        });

        table.setRowSelectionAllowed(false);

        JScrollPane scrollPane = new JScrollPane(table);
        // table.setEnabled(false);
        // scrollPane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        // // DOESNT
        // WORK

        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.weightx = 0;
        constraints.weighty = 0;
        panel.add(scrollPane, constraints);

        frame.setContentPane(panel);
        frame.setPreferredSize(new Dimension(500, 500));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new TableCursorTest();
            }
        });
    }
}

答案 2 :(得分:0)

此示例将正常工作(如果当前类扩展Jpanel):

ActionResult