mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2026-02-12 06:57:13 -06:00
ScrollBar: show decrease/increase arrow buttons if client property "JScrollBar.showButtons" is set to true on JScrollPane or JScrollBar (issue #25)
This commit is contained in:
@@ -30,6 +30,14 @@ public interface FlatClientProperties
|
||||
String SELECTED_STATE = "JButton.selectedState";
|
||||
String SELECTED_STATE_INDETERMINATE = "indeterminate";
|
||||
|
||||
/**
|
||||
* Specifies whether the decrease/increase arrow buttons of a scrollbar are shown.
|
||||
* <p>
|
||||
* <strong>Component</strong> {@link javax.swing.JScrollBar} or {@link javax.swing.JScrollPane}<br>
|
||||
* <strong>Value type</strong> {@link java.lang.Boolean}
|
||||
*/
|
||||
String SCROLL_BAR_SHOW_BUTTONS = "JScrollBar.showButtons";
|
||||
|
||||
String TABBED_PANE_HAS_FULL_BORDER = "JTabbedPane.hasFullBorder";
|
||||
|
||||
/**
|
||||
|
||||
@@ -38,12 +38,15 @@ public class FlatArrowButton
|
||||
extends BasicArrowButton
|
||||
implements UIResource
|
||||
{
|
||||
public static final int DEFAULT_ARROW_WIDTH = 8;
|
||||
|
||||
private final boolean chevron;
|
||||
private final Color foreground;
|
||||
private final Color disabledForeground;
|
||||
private final Color hoverForeground;
|
||||
private final Color hoverBackground;
|
||||
|
||||
private int arrowWidth = DEFAULT_ARROW_WIDTH;
|
||||
private int xOffset = 0;
|
||||
private int yOffset = 0;
|
||||
|
||||
@@ -80,6 +83,14 @@ public class FlatArrowButton
|
||||
}
|
||||
}
|
||||
|
||||
public int getArrowWidth() {
|
||||
return arrowWidth;
|
||||
}
|
||||
|
||||
public void setArrowWidth( int arrowWidth ) {
|
||||
this.arrowWidth = arrowWidth;
|
||||
}
|
||||
|
||||
protected boolean isHover() {
|
||||
return hover;
|
||||
}
|
||||
@@ -128,8 +139,8 @@ public class FlatArrowButton
|
||||
int direction = getDirection();
|
||||
boolean vert = (direction == NORTH || direction == SOUTH);
|
||||
|
||||
int w = scale( chevron ? 8 : 9 );
|
||||
int h = scale( chevron ? 4 : 5 );
|
||||
int w = scale( arrowWidth + (chevron ? 0 : 1) );
|
||||
int h = scale( (arrowWidth / 2) + (chevron ? 0 : 1) );
|
||||
int rw = vert ? w : h;
|
||||
int rh = vert ? h : w;
|
||||
int x = Math.round( (width - rw) / 2f + scale( (float) xOffset ) );
|
||||
|
||||
@@ -22,11 +22,16 @@ import java.awt.Graphics;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.util.Objects;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.plaf.ComponentUI;
|
||||
import javax.swing.plaf.basic.BasicScrollBarUI;
|
||||
import com.formdev.flatlaf.FlatClientProperties;
|
||||
import com.formdev.flatlaf.util.UIScale;
|
||||
|
||||
/**
|
||||
@@ -38,13 +43,20 @@ import com.formdev.flatlaf.util.UIScale;
|
||||
* @uiDefault ScrollBar.foreground Color
|
||||
* @uiDefault ScrollBar.track Color
|
||||
* @uiDefault ScrollBar.thumb Color
|
||||
* @uiDefault ScrollBar.hoverTrackColor Color
|
||||
* @uiDefault ScrollBar.hoverThumbColor Color
|
||||
* @uiDefault ScrollBar.width int
|
||||
* @uiDefault ScrollBar.minimumThumbSize Dimension
|
||||
* @uiDefault ScrollBar.maximumThumbSize Dimension
|
||||
* @uiDefault ScrollBar.allowsAbsolutePositioning boolean
|
||||
*
|
||||
* <!-- FlatScrollBarUI -->
|
||||
*
|
||||
* @uiDefault ScrollBar.hoverTrackColor Color
|
||||
* @uiDefault ScrollBar.hoverThumbColor Color
|
||||
* @uiDefault Component.arrowType String triangle (default) or chevron
|
||||
* @uiDefault ScrollBar.showButtons boolean
|
||||
* @uiDefault ScrollBar.buttonArrowColor Color
|
||||
* @uiDefault ScrollBar.buttonDisabledArrowColor Color
|
||||
*
|
||||
* @author Karl Tauber
|
||||
*/
|
||||
public class FlatScrollBarUI
|
||||
@@ -53,6 +65,11 @@ public class FlatScrollBarUI
|
||||
protected Color hoverTrackColor;
|
||||
protected Color hoverThumbColor;
|
||||
|
||||
protected boolean showButtons;
|
||||
protected String arrowType;
|
||||
protected Color buttonArrowColor;
|
||||
protected Color buttonDisabledArrowColor;
|
||||
|
||||
private MouseAdapter hoverListener;
|
||||
private boolean hoverTrack;
|
||||
private boolean hoverThumb;
|
||||
@@ -85,6 +102,11 @@ public class FlatScrollBarUI
|
||||
|
||||
hoverTrackColor = UIManager.getColor( "ScrollBar.hoverTrackColor" );
|
||||
hoverThumbColor = UIManager.getColor( "ScrollBar.hoverThumbColor" );
|
||||
|
||||
showButtons = UIManager.getBoolean( "ScrollBar.showButtons" );
|
||||
arrowType = UIManager.getString( "Component.arrowType" );
|
||||
buttonArrowColor = UIManager.getColor( "ScrollBar.buttonArrowColor" );
|
||||
buttonDisabledArrowColor = UIManager.getColor( "ScrollBar.buttonDisabledArrowColor" );
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -93,6 +115,24 @@ public class FlatScrollBarUI
|
||||
|
||||
hoverTrackColor = null;
|
||||
hoverThumbColor = null;
|
||||
|
||||
buttonArrowColor = null;
|
||||
buttonDisabledArrowColor = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PropertyChangeListener createPropertyChangeListener() {
|
||||
return new BasicScrollBarUI.PropertyChangeHandler() {
|
||||
@Override
|
||||
public void propertyChange( PropertyChangeEvent e ) {
|
||||
super.propertyChange( e );
|
||||
|
||||
if( FlatClientProperties.SCROLL_BAR_SHOW_BUTTONS.equals( e.getPropertyName() ) ) {
|
||||
scrollbar.revalidate();
|
||||
scrollbar.repaint();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -102,24 +142,50 @@ public class FlatScrollBarUI
|
||||
|
||||
@Override
|
||||
protected JButton createDecreaseButton( int orientation ) {
|
||||
return createInvisibleButton();
|
||||
return createArrowButton( orientation );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JButton createIncreaseButton( int orientation ) {
|
||||
return createInvisibleButton();
|
||||
return createArrowButton( orientation );
|
||||
}
|
||||
|
||||
private JButton createInvisibleButton() {
|
||||
JButton button = new JButton();
|
||||
button.setMinimumSize( new Dimension() );
|
||||
button.setMaximumSize( new Dimension() );
|
||||
button.setPreferredSize( new Dimension() );
|
||||
private JButton createArrowButton( int orientation ) {
|
||||
FlatArrowButton button = new FlatArrowButton( orientation,
|
||||
arrowType, buttonArrowColor, buttonDisabledArrowColor, null, hoverTrackColor )
|
||||
{
|
||||
@Override
|
||||
public Dimension getPreferredSize() {
|
||||
if( isShowButtons() ) {
|
||||
int w = UIScale.scale( scrollBarWidth );
|
||||
return new Dimension( w, w );
|
||||
} else
|
||||
return new Dimension();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dimension getMinimumSize() {
|
||||
return isShowButtons() ? super.getMinimumSize() : new Dimension();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dimension getMaximumSize() {
|
||||
return isShowButtons() ? super.getMaximumSize() : new Dimension();
|
||||
}
|
||||
};
|
||||
button.setArrowWidth( FlatArrowButton.DEFAULT_ARROW_WIDTH - 2 );
|
||||
button.setFocusable( false );
|
||||
button.setRequestFocusEnabled( false );
|
||||
return button;
|
||||
}
|
||||
|
||||
private boolean isShowButtons() {
|
||||
Object showButtons = scrollbar.getClientProperty( FlatClientProperties.SCROLL_BAR_SHOW_BUTTONS );
|
||||
if( showButtons == null && scrollbar.getParent() instanceof JScrollPane )
|
||||
showButtons = ((JScrollPane)scrollbar.getParent()).getClientProperty( FlatClientProperties.SCROLL_BAR_SHOW_BUTTONS );
|
||||
return (showButtons != null) ? Objects.equals( showButtons, true ) : this.showButtons;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintDecreaseHighlight( Graphics g ) {
|
||||
// do not paint
|
||||
|
||||
@@ -24,13 +24,16 @@ import java.awt.event.ContainerListener;
|
||||
import java.awt.event.FocusEvent;
|
||||
import java.awt.event.FocusListener;
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JScrollBar;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JViewport;
|
||||
import javax.swing.LookAndFeel;
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.plaf.ComponentUI;
|
||||
import javax.swing.plaf.basic.BasicScrollPaneUI;
|
||||
import com.formdev.flatlaf.FlatClientProperties;
|
||||
|
||||
/**
|
||||
* Provides the Flat LaF UI delegate for {@link javax.swing.JScrollPane}.
|
||||
@@ -87,6 +90,29 @@ public class FlatScrollPaneUI
|
||||
handler = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PropertyChangeListener createPropertyChangeListener() {
|
||||
return new BasicScrollPaneUI.PropertyChangeHandler() {
|
||||
@Override
|
||||
public void propertyChange( PropertyChangeEvent e ) {
|
||||
super.propertyChange( e );
|
||||
|
||||
if( FlatClientProperties.SCROLL_BAR_SHOW_BUTTONS.equals( e.getPropertyName() ) ) {
|
||||
JScrollBar vsb = scrollpane.getVerticalScrollBar();
|
||||
JScrollBar hsb = scrollpane.getHorizontalScrollBar();
|
||||
if( vsb != null ) {
|
||||
vsb.revalidate();
|
||||
vsb.repaint();
|
||||
}
|
||||
if( hsb != null ) {
|
||||
hsb.revalidate();
|
||||
hsb.repaint();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public Handler getHandler() {
|
||||
if( handler == null )
|
||||
handler = new Handler();
|
||||
|
||||
@@ -262,6 +262,10 @@ RadioButtonMenuItem.margin=2,2,2,2
|
||||
#---- ScrollBar ----
|
||||
|
||||
ScrollBar.width=10
|
||||
ScrollBar.showButtons=false
|
||||
ScrollBar.squareButtons=false
|
||||
ScrollBar.buttonArrowColor=@@ComboBox.buttonArrowColor
|
||||
ScrollBar.buttonDisabledArrowColor=@@ComboBox.buttonDisabledArrowColor
|
||||
|
||||
|
||||
#---- ScrollPane ----
|
||||
|
||||
Reference in New Issue
Block a user