在下面的例子中,我有一张桌子。如果将鼠标悬停在表标题列边缘上,则光标将更改为调整大小光标符号。但是,如果取消注释下面的行以将玻璃窗格设置为可见,则光标不再更改 - 但您仍然可以调整列的大小并以其他方式与表进行交互(例如,选择表格单元格等)。
使用玻璃窗格时,有没有办法保持相同的鼠标光标行为?
我需要一个适用于Java 6的解决方案。
public class Test extends JPanel
{
public Test()
{
super(new GridLayout(1, 0));
String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years", "Vegetarian" };
Object[][] data = { { "Kathy", "Smith", "Snowboarding", new Integer(5), new Boolean(false) },
{ "John", "Doe", "Rowing", new Integer(3), new Boolean(true) }, { "Sue", "Black", "Knitting", new Integer(2), new Boolean(false) },
{ "Jane", "White", "Speed reading", new Integer(20), new Boolean(true) }, { "Joe", "Brown", "Pool", new Integer(10), new Boolean(false) } };
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel glass = (JPanel)frame.getGlassPane();
//glass.setVisible(true);
Test newContentPane = new Test();
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();
}
});
}
}
答案 0 :(得分:3)
GlassPane
默认使用每MouseEvents
个,必须redispatch
这些事件,然后根据{{1}使用JLayer
(Java7) (Java6)而不是JXLayer
GlassPane
永远不会消费GlassPane
,必须添加KeyEvents
并致电KeyListener