1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| public class MouseScrollBarListener implements MouseListener
{
/** The timer second scroll. */
private Timer timerSecondScroll = null;
/** The scroll bar. */
private JScrollBar scrollBar = null;
/** The arrow button. */
private JButton arrow = null;
public MouseScrollBarListener(JScrollBar scroll, JButton arrowButton)
{
this.scrollBar = scroll;
this.arrow = arrowButton;
// Initialization of the timer
this.timerSecondScroll = new Timer(100, new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
if (arrow.equals(scrollBar.getComponent(0)))
scrollBar.setValue(scrollBar.getValue()
+ scrollBar.getBlockIncrement());
else
scrollBar.setValue(scrollBar.getValue()
- scrollBar.getBlockIncrement());
if (scrollBar.getValue() == scrollBar.getMinimum()
|| ((scrollBar.getValue() + scrollBar.getVisibleAmount()) == scrollBar
.getMaximum()))
{
timerSecondScroll.stop();
}
}
});
}
public void mousePressed(MouseEvent e)
{
JButton arrowButton = (JButton)e.getSource();
if (arrowButton.isEnabled())
{
//Change the background
arrowButton.setBackground(Color.red);
arrowButton.repaint();
}
//Increment the scrollBar and start the first timer
if (arrowButton.equals(scrollBar.getComponent(0)))
scrollBar.setValue(scrollBar.getValue() + scrollBar.getBlockIncrement());
else
scrollBar.setValue(scrollBar.getValue() - scrollBar.getBlockIncrement());
this.timerSecondScroll.start();
}
public void mouseReleased(MouseEvent e)
{
//Close the timer
timerSecondScroll.stop();
}
public void mouseEntered(MouseEvent e)
{
if (e.getSource() instanceof JButton)
{
JButton arrowButton = (JButton)e.getSource();
arrowButton.setBackground(Color.green);
arrowButton.repaint();
}
}
public void mouseExited(MouseEvent e)
{
if (e.getSource() instanceof JButton)
{
JButton arrowButton = (JButton)e.getSource();
if (arrowButton.isEnabled())
{
arrowButton.setBackground(Color.green);
arrowButton.repaint();
}
}
}
} |
Partager