Java Swing: change background color on mouse over

后端 未结 4 1668
半阙折子戏
半阙折子戏 2020-12-11 08:27

I\'ve implemented a simple mouse listener where the background color changes whenever the mouse enters the component (a JPanel), and it reverts back whenever the mouse leave

4条回答
  •  眼角桃花
    2020-12-11 08:47

    After trying various approaches on a container, without success, I ended up using a Timer. It didn't help that my container contained elements that already needed mouse listeners on them.

    The timer approach also meant that I could delay the change for a short time. (In my case, I show additional buttons in a tree node (a container), as well as changing the background.)

    On a mouseEntered() on the container, a Timer is created (if not there already) which repeats every 260 milliseconds. On each call of the Timer, it determines whether the mouse is inside the container. If so, on the first time it signals mouse-over. If not, it signals non-mouse-over and stops the timer.

    In Scala, this is as follows, where the method call to entryExit() encodes whether the mouse is over or not (where multiple calls with the same value have no affect):

    abstract class MouseInterpreter(component: JComponent) extends MouseAdapter {
      ...
      private var mouseOverAction: () => Unit   = () => {}
      private var mouseOverTimer: Option[Timer] = None
      ...
      def entryExit(entered: Boolean) // this is an abstract method
    
      override def mouseEntered(e: MouseEvent) {
        if (mouseOverTimer.isEmpty) {
          val aTimer = new Timer(260, new ActionListener {
            def actionPerformed(e: ActionEvent) {
              mouseOverAction()
            }
          })
          mouseOverTimer = Some(aTimer)
          mouseOverAction = () => {
            mouseOverAction = () => {
              val point = MouseInfo.getPointerInfo.getLocation
              SwingUtilities.convertPointFromScreen(point, component)
              if (component.getVisibleRect.contains(point))
                entryExit(entered = true)
              else {
                entryExit(entered = false)
                aTimer.stop()
                mouseOverTimer = None
                mouseOverAction = () => {}
              }
            }
          }
          aTimer.setRepeats(true)
          aTimer.start()
        }
      }
    ...
    }
    

提交回复
热议问题