ComboBox: right-to-left fixes (#18)

This commit is contained in:
Karl Tauber
2019-10-22 12:07:23 +02:00
parent 4181759008
commit 0f38af5922
7 changed files with 80 additions and 5 deletions

View File

@@ -1,6 +1,12 @@
FlatLaf Change Log
==================
## Unreleased
- Made some fixes for right-to-left support in ComboBox, Slider and ToolTip.
(issue #18)
## 0.15
- ToolTip: Improved styling of dark tooltips (darker background, no border).

View File

@@ -19,6 +19,7 @@ package com.formdev.flatlaf.ui;
import static com.formdev.flatlaf.util.UIScale.scale;
import java.awt.Color;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
@@ -213,6 +214,9 @@ public class FlatComboBoxUI
{
// fix editor component colors
updateEditorColors();
} else if( editor != null && source == comboBox && propertyName == "componentOrientation" ) {
ComponentOrientation o = (ComponentOrientation) e.getNewValue();
editor.applyComponentOrientation( o );
}
}
};
@@ -235,6 +239,8 @@ public class FlatComboBoxUI
if( editor instanceof JTextComponent )
((JTextComponent)editor).setBorder( BorderFactory.createEmptyBorder() );
editor.applyComponentOrientation( comboBox.getComponentOrientation() );
updateEditorColors();
}
@@ -315,6 +321,7 @@ public class FlatComboBoxUI
CellPaddingBorder.uninstall( renderer );
Component c = renderer.getListCellRendererComponent( listBox, comboBox.getSelectedItem(), -1, false, false );
c.setFont( comboBox.getFont() );
c.applyComponentOrientation( comboBox.getComponentOrientation() );
CellPaddingBorder.uninstall( c );
boolean enabled = comboBox.isEnabled();
@@ -373,6 +380,15 @@ public class FlatComboBoxUI
FlatComboPopup( JComboBox combo ) {
super( combo );
// BasicComboPopup listens to JComboBox.componentOrientation and updates
// the component orientation of the list, scroller and popup, but when
// switching the LaF and a new combo popup is created, the component
// orientation is not applied.
ComponentOrientation o = comboBox.getComponentOrientation();
list.setComponentOrientation( o );
scroller.setComponentOrientation( o );
setComponentOrientation( o );
}
@Override
@@ -386,7 +402,13 @@ public class FlatComboBoxUI
comboBox.setPrototypeDisplayValue( prototype );
// make popup wider if necessary
pw = Math.max( pw, displaySize.width );
if( displaySize.width > pw ) {
int diff = displaySize.width - pw;
pw = displaySize.width;
if( !comboBox.getComponentOrientation().isLeftToRight() )
px -= diff;
}
return super.computePopupBounds( px, py, pw, ph );
}
@@ -433,6 +455,7 @@ public class FlatComboBoxUI
CellPaddingBorder.uninstall( renderer );
Component c = renderer.getListCellRendererComponent( list, value, index, isSelected, cellHasFocus );
c.applyComponentOrientation( comboBox.getComponentOrientation() );
if( c instanceof JComponent ) {
if( paddingBorder == null )

View File

@@ -243,6 +243,7 @@ public class FlatComponentsTest
//---- button5 ----
button5.setText("default");
button5.setDisplayedMnemonicIndex(0);
button5.setToolTipText("Tool tip with\nmultiple\nlines.");
add(button5, "cell 3 1");
//---- button3 ----
@@ -346,7 +347,15 @@ public class FlatComponentsTest
"editable",
"a",
"bb",
"ccc"
"ccc",
"dd",
"e",
"ff",
"ggg",
"hh",
"i",
"jj",
"kkk"
}));
add(comboBox1, "cell 1 5,growx");
@@ -366,7 +375,15 @@ public class FlatComponentsTest
"not editable",
"a",
"bb",
"ccc"
"ccc",
"dd",
"e",
"ff",
"ggg",
"hh",
"i",
"jj",
"kkk"
}));
add(comboBox3, "cell 3 5,growx");

View File

@@ -60,6 +60,7 @@ new FormModel {
name: "button5"
"text": "default"
"displayedMnemonicIndex": 0
"toolTipText": "Tool tip with\nmultiple\nlines."
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 3 1"
} )
@@ -205,6 +206,14 @@ new FormModel {
addElement( "a" )
addElement( "bb" )
addElement( "ccc" )
addElement( "dd" )
addElement( "e" )
addElement( "ff" )
addElement( "ggg" )
addElement( "hh" )
addElement( "i" )
addElement( "jj" )
addElement( "kkk" )
}
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 5,growx"
@@ -231,6 +240,14 @@ new FormModel {
addElement( "a" )
addElement( "bb" )
addElement( "ccc" )
addElement( "dd" )
addElement( "e" )
addElement( "ff" )
addElement( "ggg" )
addElement( "hh" )
addElement( "i" )
addElement( "jj" )
addElement( "kkk" )
}
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 3 5,growx"

View File

@@ -239,6 +239,7 @@ public class FlatInspector
text += "Enabled: " + c.isEnabled() + '\n';
text += "Opaque: " + c.isOpaque() + '\n';
text += "Focusable: " + c.isFocusable() + '\n';
text += "Left-to-right: " + c.getComponentOrientation().isLeftToRight() + '\n';
text += "Parent: " + c.getParent().getClass().getName();
return text;

View File

@@ -47,6 +47,8 @@ public class FlatTestFrame
private JComponent content;
private FlatInspector inspector;
public boolean useApplyComponentOrientation;
public static FlatTestFrame create( String[] args, String title ) {
Preferences prefs = Preferences.userRoot().node( PREFS_ROOT_PATH );
@@ -307,9 +309,17 @@ public class FlatTestFrame
}
private void rightToLeftChanged() {
contentPanel.applyComponentOrientation( rightToLeftCheckBox.isSelected()
ComponentOrientation orientation = rightToLeftCheckBox.isSelected()
? ComponentOrientation.RIGHT_TO_LEFT
: ComponentOrientation.LEFT_TO_RIGHT );
: ComponentOrientation.LEFT_TO_RIGHT;
if( useApplyComponentOrientation )
content.applyComponentOrientation( orientation );
else {
updateComponentsRecur( content, (c, type) -> {
c.setComponentOrientation( orientation );
} );
}
contentPanel.revalidate();
contentPanel.repaint();
}

View File

@@ -32,6 +32,7 @@ public class FlatSwingXTest
{
public static void main( String[] args ) {
FlatTestFrame frame = FlatTestFrame.create( args, "FlatSwingXTest" );
frame.useApplyComponentOrientation = true;
frame.showFrame( new FlatSwingXTest() );
}