Tree: fixed truncated node text and too small painted non-wide node background if custom cell renderer sets icon, but not disabled icon, and tree is disabled (issue #640)

This commit is contained in:
Karl Tauber
2023-02-02 11:50:13 +01:00
parent 29b801e13d
commit 28cdde3f17
4 changed files with 58 additions and 11 deletions

View File

@@ -34,6 +34,9 @@ FlatLaf Change Log
- Updated "Hiberbee Dark" and "Material Theme UI Lite" themes. - Updated "Hiberbee Dark" and "Material Theme UI Lite" themes.
- Styling: Fixed resolving of UI variables in styles that use other variables. - Styling: Fixed resolving of UI variables in styles that use other variables.
- MenuItem: Fixed horizontal alignment of icons. (issue #631) - MenuItem: Fixed horizontal alignment of icons. (issue #631)
- Tree: Fixed truncated node text and too small painted non-wide node background
if custom cell renderer sets icon, but not disabled icon, and tree is
disabled. (issue #640)
## 3.0 ## 3.0

View File

@@ -310,6 +310,21 @@ public class FlatTreeUI
tree.revalidate(); tree.revalidate();
tree.repaint(); tree.repaint();
break; break;
case "enabled":
// if default icons are not shown and the renderer is a subclass
// of DefaultTreeCellRenderer, then invalidate tree node sizes
// because the custom renderer may use an icon for enabled state
// but none for disabled state
if( !showDefaultIcons &&
currentCellRenderer instanceof DefaultTreeCellRenderer &&
currentCellRenderer.getClass() != DefaultTreeCellRenderer.class &&
treeState != null )
{
treeState.invalidateSizes();
updateSize();
}
break;
} }
} }
}; };
@@ -695,7 +710,7 @@ public class FlatTreeUI
if( rendererComponent instanceof JLabel ) { if( rendererComponent instanceof JLabel ) {
JLabel label = (JLabel) rendererComponent; JLabel label = (JLabel) rendererComponent;
Icon icon = label.getIcon(); Icon icon = label.isEnabled() ? label.getIcon() : label.getDisabledIcon();
imageOffset = (icon != null && label.getText() != null) imageOffset = (icon != null && label.getText() != null)
? icon.getIconWidth() + Math.max( label.getIconTextGap() - 1, 0 ) ? icon.getIconWidth() + Math.max( label.getIconTextGap() - 1, 0 )
: 0; : 0;

View File

@@ -439,6 +439,11 @@ public class FlatComponents2Test
tree.setCellRenderer( new TestDefaultTreeCellRenderer() ); tree.setCellRenderer( new TestDefaultTreeCellRenderer() );
break; break;
case "defaultWithIcon":
for( JTree tree : trees )
tree.setCellRenderer( new TestDefaultWithIconTreeCellRenderer() );
break;
case "label": case "label":
for( JTree tree : trees ) for( JTree tree : trees )
tree.setCellRenderer( new TestLabelTreeCellRenderer() ); tree.setCellRenderer( new TestLabelTreeCellRenderer() );
@@ -495,11 +500,13 @@ public class FlatComponents2Test
tree.setEditable( editable ); tree.setEditable( editable );
} }
private void showDefaultIcons() { private void treeShowDefaultIconsChanged() {
boolean showDefaultIcons = showDefaultIconsCheckBox.isSelected(); boolean showDefaultIcons = treeShowDefaultIconsCheckBox.isSelected();
UIManager.put( "Tree.showDefaultIcons", showDefaultIcons ? true : null ); UIManager.put( "Tree.showDefaultIcons", showDefaultIcons ? true : null );
for( JTree tree : allTrees ) for( JTree tree : allTrees )
tree.updateUI(); tree.updateUI();
treeRendererChanged();
} }
private void treeMouseClicked( MouseEvent e ) { private void treeMouseClicked( MouseEvent e ) {
@@ -601,7 +608,7 @@ public class FlatComponents2Test
treePaintLinesCheckBox = new JCheckBox(); treePaintLinesCheckBox = new JCheckBox();
treeRedLinesCheckBox = new JCheckBox(); treeRedLinesCheckBox = new JCheckBox();
treeEditableCheckBox = new JCheckBox(); treeEditableCheckBox = new JCheckBox();
showDefaultIconsCheckBox = new JCheckBox(); treeShowDefaultIconsCheckBox = new JCheckBox();
JPanel tableOptionsPanel = new JPanel(); JPanel tableOptionsPanel = new JPanel();
JLabel autoResizeModeLabel = new JLabel(); JLabel autoResizeModeLabel = new JLabel();
autoResizeModeField = new JComboBox<>(); autoResizeModeField = new JComboBox<>();
@@ -953,6 +960,7 @@ public class FlatComponents2Test
treeRendererComboBox.setModel(new DefaultComboBoxModel<>(new String[] { treeRendererComboBox.setModel(new DefaultComboBoxModel<>(new String[] {
"default", "default",
"defaultSubclass", "defaultSubclass",
"defaultWithIcon",
"label", "label",
"swingxDefault", "swingxDefault",
"jideCheckBox", "jideCheckBox",
@@ -988,10 +996,10 @@ public class FlatComponents2Test
treeEditableCheckBox.addActionListener(e -> treeEditableChanged()); treeEditableCheckBox.addActionListener(e -> treeEditableChanged());
treeOptionsPanel.add(treeEditableCheckBox, "cell 0 4"); treeOptionsPanel.add(treeEditableCheckBox, "cell 0 4");
//---- showDefaultIconsCheckBox ---- //---- treeShowDefaultIconsCheckBox ----
showDefaultIconsCheckBox.setText("show default icons"); treeShowDefaultIconsCheckBox.setText("show default icons");
showDefaultIconsCheckBox.addActionListener(e -> showDefaultIcons()); treeShowDefaultIconsCheckBox.addActionListener(e -> treeShowDefaultIconsChanged());
treeOptionsPanel.add(showDefaultIconsCheckBox, "cell 0 4"); treeOptionsPanel.add(treeShowDefaultIconsCheckBox, "cell 0 4");
} }
add(treeOptionsPanel, "cell 0 4 4 1"); add(treeOptionsPanel, "cell 0 4 4 1");
@@ -1128,7 +1136,7 @@ public class FlatComponents2Test
private JCheckBox treePaintLinesCheckBox; private JCheckBox treePaintLinesCheckBox;
private JCheckBox treeRedLinesCheckBox; private JCheckBox treeRedLinesCheckBox;
private JCheckBox treeEditableCheckBox; private JCheckBox treeEditableCheckBox;
private JCheckBox showDefaultIconsCheckBox; private JCheckBox treeShowDefaultIconsCheckBox;
private JComboBox<String> autoResizeModeField; private JComboBox<String> autoResizeModeField;
private JComboBox<String> sortIconPositionComboBox; private JComboBox<String> sortIconPositionComboBox;
private JCheckBox showHorizontalLinesCheckBox; private JCheckBox showHorizontalLinesCheckBox;
@@ -1604,6 +1612,26 @@ public class FlatComponents2Test
} }
} }
//---- class TestDefaultWithIconTreeCellRenderer --------------------------
private static class TestDefaultWithIconTreeCellRenderer
extends TestDefaultTreeCellRenderer
{
@Override
public Component getTreeCellRendererComponent( JTree tree, Object value, boolean sel, boolean expanded,
boolean leaf, int row, boolean hasFocus )
{
super.getTreeCellRendererComponent( tree, value, sel, expanded, leaf, row, hasFocus );
// set icon for enabled state, but not for disabled state,
// which allows testing whether tree node layout is updated correctly
// when enabled state changes
setIcon( UIManager.getIcon( "FileView.floppyDriveIcon" ) );
return this;
}
}
//---- class TestLabelTreeCellRenderer ------------------------------------ //---- class TestLabelTreeCellRenderer ------------------------------------
private static class TestLabelTreeCellRenderer private static class TestLabelTreeCellRenderer

View File

@@ -411,6 +411,7 @@ new FormModel {
selectedItem: "default" selectedItem: "default"
addElement( "default" ) addElement( "default" )
addElement( "defaultSubclass" ) addElement( "defaultSubclass" )
addElement( "defaultWithIcon" )
addElement( "label" ) addElement( "label" )
addElement( "swingxDefault" ) addElement( "swingxDefault" )
addElement( "jideCheckBox" ) addElement( "jideCheckBox" )
@@ -476,12 +477,12 @@ new FormModel {
"value": "cell 0 4" "value": "cell 0 4"
} ) } )
add( new FormComponent( "javax.swing.JCheckBox" ) { add( new FormComponent( "javax.swing.JCheckBox" ) {
name: "showDefaultIconsCheckBox" name: "treeShowDefaultIconsCheckBox"
"text": "show default icons" "text": "show default icons"
auxiliary() { auxiliary() {
"JavaCodeGenerator.variableLocal": false "JavaCodeGenerator.variableLocal": false
} }
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "showDefaultIcons", false ) ) addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "treeShowDefaultIconsChanged", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 4" "value": "cell 0 4"
} ) } )