ComboBox: if not editable, then hover highlight arrow even if mouse is not in arrow button

This commit is contained in:
Karl Tauber
2019-10-04 18:55:13 +02:00
parent 37c70f6c9e
commit 7ef598ded6
2 changed files with 39 additions and 3 deletions

View File

@@ -80,6 +80,10 @@ public class FlatArrowButton
} }
} }
protected boolean isHover() {
return hover;
}
public int getXOffset() { public int getXOffset() {
return xOffset; return xOffset;
} }
@@ -116,7 +120,7 @@ public class FlatArrowButton
boolean enabled = isEnabled(); boolean enabled = isEnabled();
// paint hover background // paint hover background
if( enabled && hover && hoverBackground != null ) { if( enabled && isHover() && hoverBackground != null ) {
g.setColor( hoverBackground ); g.setColor( hoverBackground );
g.fillRect( 0, 0, width, height ); g.fillRect( 0, 0, width, height );
} }
@@ -139,7 +143,7 @@ public class FlatArrowButton
// paint arrow // paint arrow
g.setColor( enabled g.setColor( enabled
? (hover && hoverForeground != null ? hoverForeground : foreground) ? (isHover() && hoverForeground != null ? hoverForeground : foreground)
: disabledForeground ); : disabledForeground );
g.translate( x, y ); g.translate( x, y );
Shape arrowShape = createArrowShape( direction, chevron, w, h ); Shape arrowShape = createArrowShape( direction, chevron, w, h );

View File

@@ -29,6 +29,7 @@ import java.awt.Rectangle;
import java.awt.Shape; import java.awt.Shape;
import java.awt.event.FocusEvent; import java.awt.event.FocusEvent;
import java.awt.event.FocusListener; import java.awt.event.FocusListener;
import java.awt.event.MouseListener;
import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D;
import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener; import java.beans.PropertyChangeListener;
@@ -88,10 +89,35 @@ public class FlatComboBoxUI
protected Color buttonDisabledArrowColor; protected Color buttonDisabledArrowColor;
protected Color buttonHoverArrowColor; protected Color buttonHoverArrowColor;
private MouseListener hoverListener;
private boolean hover;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
return new FlatComboBoxUI(); return new FlatComboBoxUI();
} }
@Override
protected void installListeners() {
super.installListeners();
hoverListener = new FlatUIUtils.HoverListener( null, h -> {
if( !comboBox.isEditable() ) {
hover = h;
if( arrowButton != null )
arrowButton.repaint();
}
} );
comboBox.addMouseListener( hoverListener );
}
@Override
protected void uninstallListeners() {
super.uninstallListeners();
comboBox.removeMouseListener( hoverListener );
hoverListener = null;
}
@Override @Override
protected void installDefaults() { protected void installDefaults() {
super.installDefaults(); super.installDefaults();
@@ -228,7 +254,13 @@ public class FlatComboBoxUI
@Override @Override
protected JButton createArrowButton() { protected JButton createArrowButton() {
return new FlatArrowButton( SwingConstants.SOUTH, arrowType, buttonArrowColor, return new FlatArrowButton( SwingConstants.SOUTH, arrowType, buttonArrowColor,
buttonDisabledArrowColor, buttonHoverArrowColor, null ); buttonDisabledArrowColor, buttonHoverArrowColor, null )
{
@Override
protected boolean isHover() {
return super.isHover() || (!comboBox.isEditable() ? hover : false);
}
};
} }
@Override @Override