Window decorations: added window icon (issues #47 and #82)

This commit is contained in:
Karl Tauber
2020-05-27 11:36:11 +02:00
parent 9ad32125c0
commit 626601f6aa
13 changed files with 286 additions and 16 deletions

Binary file not shown.

View File

@@ -17,7 +17,11 @@
package com.formdev.flatlaf.testing;
import java.awt.*;
import java.awt.Dialog.ModalityType;
import java.awt.event.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.swing.*;
import net.miginfocom.swing.*;
@@ -39,10 +43,25 @@ public class FlatWindowDecorationsTest
// frame.setUndecorated( true );
// frame.getRootPane().setWindowDecorationStyle( JRootPane.FRAME );
Class<?> cls = FlatWindowDecorationsTest.class;
List<Image> images = Arrays.asList(
new ImageIcon( cls.getResource( "/com/formdev/flatlaf/testing/test16.png" ) ).getImage(),
new ImageIcon( cls.getResource( "/com/formdev/flatlaf/testing/test24.png" ) ).getImage(),
new ImageIcon( cls.getResource( "/com/formdev/flatlaf/testing/test32.png" ) ).getImage(),
new ImageIcon( cls.getResource( "/com/formdev/flatlaf/testing/test48.png" ) ).getImage(),
new ImageIcon( cls.getResource( "/com/formdev/flatlaf/testing/test64.png" ) ).getImage(),
new ImageIcon( cls.getResource( "/com/formdev/flatlaf/testing/test128.png" ) ).getImage()
);
// shuffle to test whether FlatLaf chooses the right size
Collections.shuffle( images );
frame.setIconImages( images );
frame.showFrame( FlatWindowDecorationsTest::new, panel -> ((FlatWindowDecorationsTest)panel).menuBar );
} );
}
private List<Image> images;
FlatWindowDecorationsTest() {
initComponents();
}
@@ -51,6 +70,14 @@ public class FlatWindowDecorationsTest
public void addNotify() {
super.addNotify();
Window window = SwingUtilities.windowForComponent( this );
menuBarCheckBox.setEnabled( window instanceof JFrame );
boolean windowHasIcons = (window != null && !window.getIconImages().isEmpty());
iconNoneRadioButton.setEnabled( windowHasIcons );
iconTestAllRadioButton.setEnabled( windowHasIcons );
iconTestRandomRadioButton.setEnabled( windowHasIcons );
JRootPane rootPane = getWindowRootPane();
if( rootPane != null ) {
int style = rootPane.getWindowDecorationStyle();
@@ -89,7 +116,12 @@ public class FlatWindowDecorationsTest
}
private void openDialog() {
JOptionPane.showMessageDialog( this, new FlatWindowDecorationsTest() );
Window owner = SwingUtilities.windowForComponent( this );
JDialog dialog = new JDialog( owner, "Dialog", ModalityType.APPLICATION_MODAL );
dialog.add( new FlatWindowDecorationsTest() );
dialog.pack();
dialog.setLocationRelativeTo( this );
dialog.setVisible( true );
}
private void decorationStyleChanged() {
@@ -118,6 +150,22 @@ public class FlatWindowDecorationsTest
rootPane.setWindowDecorationStyle( style );
}
private void iconChanged() {
Window window = SwingUtilities.windowForComponent( this );
if( window == null )
return;
if( images == null )
images = window.getIconImages();
if( iconNoneRadioButton.isSelected() )
window.setIconImage( null );
else if( iconTestAllRadioButton.isSelected() )
window.setIconImages( images );
else if( iconTestRandomRadioButton.isSelected() )
window.setIconImage( images.get( (int) (Math.random() * images.size()) ) );
}
private JRootPane getWindowRootPane() {
Window window = SwingUtilities.windowForComponent( this );
if( window instanceof JFrame )
@@ -132,6 +180,7 @@ public class FlatWindowDecorationsTest
menuBarCheckBox = new JCheckBox();
resizableCheckBox = new JCheckBox();
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
JPanel panel1 = new JPanel();
styleNoneRadioButton = new JRadioButton();
styleFrameRadioButton = new JRadioButton();
@@ -142,6 +191,10 @@ public class FlatWindowDecorationsTest
styleWarningRadioButton = new JRadioButton();
styleColorChooserRadioButton = new JRadioButton();
styleFileChooserRadioButton = new JRadioButton();
JPanel panel2 = new JPanel();
iconNoneRadioButton = new JRadioButton();
iconTestAllRadioButton = new JRadioButton();
iconTestRandomRadioButton = new JRadioButton();
JButton openDialogButton = new JButton();
menuBar = new JMenuBar();
JMenu fileMenu = new JMenu();
@@ -174,12 +227,13 @@ public class FlatWindowDecorationsTest
setLayout(new MigLayout(
"ltr,insets dialog,hidemode 3",
// columns
"[left]para",
"[left]para" +
"[fill]",
// rows
"para[]" +
"[]" +
"para[]0" +
"[]" +
"[]" +
"[top]" +
"[]"));
//---- menuBarCheckBox ----
@@ -198,6 +252,10 @@ public class FlatWindowDecorationsTest
label1.setText("Style:");
add(label1, "cell 0 2");
//---- label2 ----
label2.setText("Icon:");
add(label2, "cell 1 2");
//======== panel1 ========
{
panel1.setLayout(new MigLayout(
@@ -263,6 +321,35 @@ public class FlatWindowDecorationsTest
}
add(panel1, "cell 0 3");
//======== panel2 ========
{
panel2.setLayout(new MigLayout(
"ltr,insets 0,hidemode 3,gap 0 0",
// columns
"[fill]",
// rows
"[]" +
"[]" +
"[]"));
//---- iconNoneRadioButton ----
iconNoneRadioButton.setText("none");
iconNoneRadioButton.addActionListener(e -> iconChanged());
panel2.add(iconNoneRadioButton, "cell 0 0");
//---- iconTestAllRadioButton ----
iconTestAllRadioButton.setText("test all");
iconTestAllRadioButton.setSelected(true);
iconTestAllRadioButton.addActionListener(e -> iconChanged());
panel2.add(iconTestAllRadioButton, "cell 0 1");
//---- iconTestRandomRadioButton ----
iconTestRandomRadioButton.setText("test random");
iconTestRandomRadioButton.addActionListener(e -> iconChanged());
panel2.add(iconTestRandomRadioButton, "cell 0 2");
}
add(panel2, "cell 1 3");
//---- openDialogButton ----
openDialogButton.setText("Open Dialog");
openDialogButton.addActionListener(e -> openDialog());
@@ -447,6 +534,12 @@ public class FlatWindowDecorationsTest
styleButtonGroup.add(styleWarningRadioButton);
styleButtonGroup.add(styleColorChooserRadioButton);
styleButtonGroup.add(styleFileChooserRadioButton);
//---- iconButtonGroup ----
ButtonGroup iconButtonGroup = new ButtonGroup();
iconButtonGroup.add(iconNoneRadioButton);
iconButtonGroup.add(iconTestAllRadioButton);
iconButtonGroup.add(iconTestRandomRadioButton);
// JFormDesigner - End of component initialization //GEN-END:initComponents
}
@@ -462,6 +555,9 @@ public class FlatWindowDecorationsTest
private JRadioButton styleWarningRadioButton;
private JRadioButton styleColorChooserRadioButton;
private JRadioButton styleFileChooserRadioButton;
private JRadioButton iconNoneRadioButton;
private JRadioButton iconTestAllRadioButton;
private JRadioButton iconTestRandomRadioButton;
private JMenuBar menuBar;
// JFormDesigner - End of variables declaration //GEN-END:variables
}

View File

@@ -8,8 +8,8 @@ new FormModel {
}
add( new FormContainer( "com.formdev.flatlaf.testing.FlatTestPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
"$layoutConstraints": "ltr,insets dialog,hidemode 3"
"$columnConstraints": "[left]para"
"$rowConstraints": "para[][][][][]"
"$columnConstraints": "[left]para[fill]"
"$rowConstraints": "para[]0[][][top][]"
} ) {
name: "this"
add( new FormComponent( "javax.swing.JCheckBox" ) {
@@ -40,6 +40,12 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 2"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "label2"
"text": "Icon:"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 2"
} )
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
"$columnConstraints": "[fill]"
"$rowConstraints": "[][][][][][][][][]"
@@ -149,6 +155,49 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 3"
} )
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
"$columnConstraints": "[fill]"
"$rowConstraints": "[][][]"
"$layoutConstraints": "ltr,insets 0,hidemode 3,gap 0 0"
} ) {
name: "panel2"
add( new FormComponent( "javax.swing.JRadioButton" ) {
name: "iconNoneRadioButton"
"text": "none"
"$buttonGroup": new FormReference( "iconButtonGroup" )
auxiliary() {
"JavaCodeGenerator.variableLocal": false
}
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "iconChanged", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 0"
} )
add( new FormComponent( "javax.swing.JRadioButton" ) {
name: "iconTestAllRadioButton"
"text": "test all"
"selected": true
"$buttonGroup": new FormReference( "iconButtonGroup" )
auxiliary() {
"JavaCodeGenerator.variableLocal": false
}
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "iconChanged", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 1"
} )
add( new FormComponent( "javax.swing.JRadioButton" ) {
name: "iconTestRandomRadioButton"
"text": "test random"
"$buttonGroup": new FormReference( "iconButtonGroup" )
auxiliary() {
"JavaCodeGenerator.variableLocal": false
}
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "iconChanged", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 2"
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 3"
} )
add( new FormComponent( "javax.swing.JButton" ) {
name: "openDialogButton"
"text": "Open Dialog"
@@ -332,5 +381,10 @@ new FormModel {
}, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 450 )
} )
add( new FormNonVisual( "javax.swing.ButtonGroup" ) {
name: "iconButtonGroup"
}, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 502 )
} )
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 428 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 624 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB