Menus: fixed icon in top-level JMenu (issue #3)

This commit is contained in:
Karl Tauber
2020-04-26 13:40:49 +02:00
parent 41dd0acfa3
commit 2735185eb9
3 changed files with 82 additions and 23 deletions

View File

@@ -76,15 +76,13 @@ public class FlatMenuItemRenderer
boolean isTopLevelMenu = isTopLevelMenu( menuItem );
// icon size
if( !isTopLevelMenu ) {
Dimension iconSize = getIconSize();
width += iconSize.width;
height = Math.max( iconSize.height, height );
Dimension iconSize = getIconSize();
width += iconSize.width;
height = Math.max( iconSize.height, height );
// gap between icon and text
if( iconSize.width > 0 )
width += scale( menuItem.getIconTextGap() );
}
// gap between icon and text
if( iconSize.width > 0 )
width += scale( menuItem.getIconTextGap() );
// text size
View htmlView = (View) menuItem.getClientProperty( BasicHTML.propertyKey );
@@ -135,10 +133,8 @@ public class FlatMenuItemRenderer
private void layout( Rectangle viewRect, Rectangle iconRect, Rectangle textRect,
Rectangle accelRect, Rectangle arrowRect )
{
boolean isTopLevelMenu = isTopLevelMenu( menuItem );
// layout icon
iconRect.setSize( !isTopLevelMenu ? getIconSize() : new Dimension() );
iconRect.setSize( getIconSize() );
iconRect.y = viewRect.y + ((viewRect.height - iconRect.height) / 2);
// layout text
@@ -148,7 +144,7 @@ public class FlatMenuItemRenderer
textRect.y = viewRect.y + ((viewRect.height - textRect.height) / 2);
// layout arrow
Icon arrowIcon = !isTopLevelMenu ? this.arrowIcon : null;
Icon arrowIcon = !isTopLevelMenu( menuItem ) ? this.arrowIcon : null;
arrowRect.width = (arrowIcon != null) ? arrowIcon.getIconWidth() : 0;
arrowRect.height = (arrowIcon != null) ? arrowIcon.getIconHeight() : 0;
arrowRect.y = viewRect.y + ((viewRect.height - arrowRect.height) / 2);
@@ -168,7 +164,7 @@ public class FlatMenuItemRenderer
// left-to-right
iconRect.x = viewRect.x;
textRect.x = iconRect.x + iconRect.width
+ (!isTopLevelMenu && iconRect.width > 0 ? scale( menuItem.getIconTextGap() ) : 0);
+ (iconRect.width > 0 ? scale( menuItem.getIconTextGap() ) : 0);
arrowRect.x = viewRect.x + viewRect.width - arrowRect.width;
if( accelText != null )
accelRect.x = arrowRect.x - accelRect.width;
@@ -176,7 +172,7 @@ public class FlatMenuItemRenderer
// right-to-left
iconRect.x = viewRect.x + viewRect.width - iconRect.width;
textRect.x = iconRect.x - textRect.width
- (!isTopLevelMenu && iconRect.width > 0 ? scale( menuItem.getIconTextGap() ) : 0);
- (iconRect.width > 0 ? scale( menuItem.getIconTextGap() ) : 0);
arrowRect.x = viewRect.x;
if( accelText != null )
accelRect.x = arrowRect.x + arrowRect.width;
@@ -213,8 +209,7 @@ debug*/
boolean isTopLevelMenu = isTopLevelMenu( menuItem );
paintBackground( g, selectionBackground );
if( !isTopLevelMenu )
paintIcon( g, iconRect, getIconForPainting() );
paintIcon( g, iconRect, getIconForPainting() );
paintText( g, textRect, menuItem.getText(), selectionForeground, disabledForeground );
paintAccelerator( g, accelRect, getAcceleratorText(), acceleratorForeground, acceleratorSelectionForeground, disabledForeground );
if( !isTopLevelMenu )
@@ -300,12 +295,8 @@ debug*/
return menuItem instanceof JMenu && ((JMenu)menuItem).isTopLevelMenu();
}
private Icon getIcon() {
return (checkIcon != null) ? checkIcon : menuItem.getIcon();
}
private Icon getIconForPainting() {
if( checkIcon != null )
if( checkIcon != null && !isTopLevelMenu( menuItem ) )
return checkIcon;
Icon icon = menuItem.getIcon();
@@ -325,7 +316,14 @@ debug*/
}
private Dimension getIconSize() {
Icon icon = getIcon();
if( isTopLevelMenu( menuItem ) ) {
Icon icon = menuItem.getIcon();
return (icon != null)
? new Dimension( icon.getIconWidth(), icon.getIconHeight() )
: new Dimension();
}
Icon icon = (checkIcon != null) ? checkIcon : menuItem.getIcon();
int iconWidth = (icon != null) ? icon.getIconWidth() : 0;
int iconHeight = (icon != null) ? icon.getIconHeight() : 0;
return new Dimension(

View File

@@ -31,6 +31,7 @@ public class FlatMenusTest
public static void main( String[] args ) {
SwingUtilities.invokeLater( () -> {
FlatTestFrame frame = FlatTestFrame.create( args, "FlatMenusTest" );
frame.useApplyComponentOrientation = true;
frame.showFrame( FlatMenusTest::new );
} );
}
@@ -68,6 +69,10 @@ public class FlatMenusTest
JMenu menu5 = new JMenu();
JMenuItem menuItem7 = new JMenuItem();
JMenuItem menuItem8 = new JMenuItem();
JCheckBoxMenuItem checkBoxMenuItem6 = new JCheckBoxMenuItem();
JRadioButtonMenuItem radioButtonMenuItem5 = new JRadioButtonMenuItem();
JRadioButtonMenuItem radioButtonMenuItem6 = new JRadioButtonMenuItem();
JRadioButtonMenuItem radioButtonMenuItem7 = new JRadioButtonMenuItem();
JMenu menu6 = new JMenu();
JMenuItem menuItem5 = new JMenuItem();
JMenuItem menuItem6 = new JMenuItem();
@@ -127,16 +132,37 @@ public class FlatMenusTest
{
menu5.setText("text");
menu5.setMnemonic('T');
menu5.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-showReadAccess.png")));
//---- menuItem7 ----
menuItem7.setText("text");
menuItem7.setMnemonic('X');
menuItem7.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-showWriteAccess.png")));
menu5.add(menuItem7);
//---- menuItem8 ----
menuItem8.setText("text");
menuItem8.setMnemonic('E');
menuItem8.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-showReadAccess@2x.png")));
menu5.add(menuItem8);
//---- checkBoxMenuItem6 ----
checkBoxMenuItem6.setText("check");
checkBoxMenuItem6.setSelected(true);
menu5.add(checkBoxMenuItem6);
//---- radioButtonMenuItem5 ----
radioButtonMenuItem5.setText("radio 1");
radioButtonMenuItem5.setSelected(true);
menu5.add(radioButtonMenuItem5);
//---- radioButtonMenuItem6 ----
radioButtonMenuItem6.setText("radio 2");
menu5.add(radioButtonMenuItem6);
//---- radioButtonMenuItem7 ----
radioButtonMenuItem7.setText("radio 3");
menu5.add(radioButtonMenuItem7);
}
menuBar1.add(menu5);
@@ -361,6 +387,12 @@ public class FlatMenusTest
armedCheckBox.setMnemonic('A');
armedCheckBox.addActionListener(e -> armedChanged());
add(armedCheckBox, "cell 0 3");
//---- buttonGroup1 ----
ButtonGroup buttonGroup1 = new ButtonGroup();
buttonGroup1.add(radioButtonMenuItem5);
buttonGroup1.add(radioButtonMenuItem6);
buttonGroup1.add(radioButtonMenuItem7);
// JFormDesigner - End of component initialization //GEN-END:initComponents
}

View File

@@ -1,4 +1,4 @@
JFDML JFormDesigner: "7.0.0.0.194" Java: "13.0.1" encoding: "UTF-8"
JFDML JFormDesigner: "7.0.1.0.272" Java: "13.0.2" encoding: "UTF-8"
new FormModel {
contentType: "form/swing"
@@ -24,15 +24,39 @@ new FormModel {
name: "menu5"
"text": "text"
"mnemonic": 84
"icon": new com.jformdesigner.model.SwingIcon( 0, "/com/formdev/flatlaf/testing/disabled_icons_test/intellij-showReadAccess.png" )
add( new FormComponent( "javax.swing.JMenuItem" ) {
name: "menuItem7"
"text": "text"
"mnemonic": 88
"icon": new com.jformdesigner.model.SwingIcon( 0, "/com/formdev/flatlaf/testing/disabled_icons_test/intellij-showWriteAccess.png" )
} )
add( new FormComponent( "javax.swing.JMenuItem" ) {
name: "menuItem8"
"text": "text"
"mnemonic": 69
"icon": new com.jformdesigner.model.SwingIcon( 0, "/com/formdev/flatlaf/testing/disabled_icons_test/intellij-showReadAccess@2x.png" )
} )
add( new FormComponent( "javax.swing.JCheckBoxMenuItem" ) {
name: "checkBoxMenuItem6"
"text": "check"
"selected": true
} )
add( new FormComponent( "javax.swing.JRadioButtonMenuItem" ) {
name: "radioButtonMenuItem5"
"text": "radio 1"
"$buttonGroup": new FormReference( "buttonGroup1" )
"selected": true
} )
add( new FormComponent( "javax.swing.JRadioButtonMenuItem" ) {
name: "radioButtonMenuItem6"
"text": "radio 2"
"$buttonGroup": new FormReference( "buttonGroup1" )
} )
add( new FormComponent( "javax.swing.JRadioButtonMenuItem" ) {
name: "radioButtonMenuItem7"
"text": "radio 3"
"$buttonGroup": new FormReference( "buttonGroup1" )
} )
} )
add( new FormContainer( "javax.swing.JMenu", new FormLayoutManager( class javax.swing.JMenu ) ) {
@@ -321,5 +345,10 @@ new FormModel {
}, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 430 )
} )
add( new FormNonVisual( "javax.swing.ButtonGroup" ) {
name: "buttonGroup1"
}, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 564 )
} )
}
}