TextField: support leading and trailing icons (issue #368)

This commit is contained in:
Karl Tauber
2021-09-02 17:45:33 +02:00
parent 51d7bc2c37
commit dc33c26960
21 changed files with 571 additions and 73 deletions

View File

@@ -19,6 +19,9 @@ package com.formdev.flatlaf.demo;
import java.awt.Component;
import javax.swing.*;
import javax.swing.text.DefaultEditorKit;
import com.formdev.flatlaf.FlatClientProperties;
import com.formdev.flatlaf.extras.FlatSVGIcon;
import com.formdev.flatlaf.icons.FlatSearchIcon;
import net.miginfocom.swing.*;
/**
@@ -123,6 +126,10 @@ class BasicComponentsPanel
JTextField warningHintsTextField = new JTextField();
JComboBox<String> warningHintsComboBox = new JComboBox<>();
JSpinner warningHintsSpinner = new JSpinner();
JLabel iconsLabel = new JLabel();
JTextField leadingIconTextField = new JTextField();
JTextField trailingIconTextField = new JTextField();
JTextField iconsTextField = new JTextField();
JPopupMenu popupMenu1 = new JPopupMenu();
JMenuItem cutMenuItem = new JMenuItem();
JMenuItem copyMenuItem = new JMenuItem();
@@ -132,7 +139,7 @@ class BasicComponentsPanel
setLayout(new MigLayout(
"insets dialog,hidemode 3",
// columns
"[sizegroup 1]" +
"[]" +
"[sizegroup 1]" +
"[sizegroup 1]" +
"[sizegroup 1]" +
@@ -152,6 +159,7 @@ class BasicComponentsPanel
"[]" +
"[]para" +
"[]" +
"[]" +
"[]"));
//---- labelLabel ----
@@ -646,6 +654,19 @@ class BasicComponentsPanel
warningHintsSpinner.putClientProperty("JComponent.outline", "warning");
add(warningHintsSpinner, "cell 3 13,growx");
//---- iconsLabel ----
iconsLabel.setText("Leading/trailing icons:");
add(iconsLabel, "cell 0 14");
add(leadingIconTextField, "cell 1 14,growx");
//---- trailingIconTextField ----
trailingIconTextField.setText("text");
add(trailingIconTextField, "cell 2 14,growx");
//---- iconsTextField ----
iconsTextField.setText("text");
add(iconsTextField, "cell 3 14,growx");
//======== popupMenu1 ========
{
@@ -670,8 +691,20 @@ class BasicComponentsPanel
copyMenuItem.addActionListener( new DefaultEditorKit.CopyAction() );
pasteMenuItem.addActionListener( new DefaultEditorKit.PasteAction() );
// add leading/trailing icons to text fields
leadingIconTextField.putClientProperty( FlatClientProperties.PLACEHOLDER_TEXT, "Search" );
leadingIconTextField.putClientProperty( FlatClientProperties.TEXT_FIELD_LEADING_ICON,
new FlatSearchIcon() );
trailingIconTextField.putClientProperty( FlatClientProperties.TEXT_FIELD_TRAILING_ICON,
new FlatSVGIcon( "com/formdev/flatlaf/demo/icons/DataTables.svg" ) );
iconsTextField.putClientProperty( FlatClientProperties.TEXT_FIELD_LEADING_ICON,
new FlatSVGIcon( "com/formdev/flatlaf/demo/icons/user.svg" ) );
iconsTextField.putClientProperty( FlatClientProperties.TEXT_FIELD_TRAILING_ICON,
new FlatSVGIcon( "com/formdev/flatlaf/demo/icons/bookmarkGroup.svg" ) );
if( FlatLafDemo.screenshotsMode ) {
Component[] components = {
// hide some components
Component[] hiddenComponents = {
button13, button14, button15, button16, comboBox5, comboBox6,
textField6, passwordField5,
@@ -683,18 +716,26 @@ class BasicComponentsPanel
errorHintsLabel, errorHintsTextField, errorHintsComboBox, errorHintsSpinner,
warningHintsLabel, warningHintsTextField, warningHintsComboBox, warningHintsSpinner,
};
for( Component c : components )
for( Component c : hiddenComponents )
c.setVisible( false );
// move password fields one row up
// move leading/trailing icon fields and password fields some rows up
Component[] formattedTextFields = { formattedTextFieldLabel, formattedTextField1, formattedTextField2, formattedTextField3, formattedTextField4 };
Component[] passwordFields = { passwordFieldLabel, passwordField1, passwordField2, passwordField3, passwordField4 };
Component[] iconsFields = { iconsLabel, leadingIconTextField, trailingIconTextField, iconsTextField };
MigLayout layout = (MigLayout) getLayout();
for( int i = 0; i < iconsFields.length; i++ ) {
Object cons = layout.getComponentConstraints( passwordFields[i] );
layout.setComponentConstraints( iconsFields[i], cons );
}
for( int i = 0; i < passwordFields.length; i++ ) {
Object cons = layout.getComponentConstraints( formattedTextFields[i] );
layout.setComponentConstraints( passwordFields[i], cons );
}
// make "Not editable disabled" combobox smaller
Object cons = layout.getComponentConstraints( comboBox4 );
layout.setComponentConstraints( comboBox4, cons + ",width 50:50" );
}
}

View File

@@ -1,4 +1,4 @@
JFDML JFormDesigner: "7.0.2.0.298" Java: "14" encoding: "UTF-8"
JFDML JFormDesigner: "7.0.4.0.360" Java: "16" encoding: "UTF-8"
new FormModel {
contentType: "form/swing"
@@ -8,8 +8,8 @@ new FormModel {
}
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
"$layoutConstraints": "insets dialog,hidemode 3"
"$columnConstraints": "[sizegroup 1][sizegroup 1][sizegroup 1][sizegroup 1][][]"
"$rowConstraints": "[][][][][][][][][][][][]para[][]"
"$columnConstraints": "[][sizegroup 1][sizegroup 1][sizegroup 1][][]"
"$rowConstraints": "[][][][][][][][][][][][]para[][][]"
} ) {
name: "this"
add( new FormComponent( "javax.swing.JLabel" ) {
@@ -648,9 +648,32 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 3 13,growx"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "iconsLabel"
"text": "Leading/trailing icons:"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 14"
} )
add( new FormComponent( "javax.swing.JTextField" ) {
name: "leadingIconTextField"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 14,growx"
} )
add( new FormComponent( "javax.swing.JTextField" ) {
name: "trailingIconTextField"
"text": "text"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 2 14,growx"
} )
add( new FormComponent( "javax.swing.JTextField" ) {
name: "iconsTextField"
"text": "text"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 3 14,growx"
} )
}, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 0 )
"size": new java.awt.Dimension( 920, 440 )
"size": new java.awt.Dimension( 920, 480 )
} )
add( new FormContainer( "javax.swing.JPopupMenu", new FormLayoutManager( class javax.swing.JPopupMenu ) ) {
name: "popupMenu1"

View File

@@ -0,0 +1,8 @@
<!-- Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -->
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
<g fill="none" fill-rule="evenodd">
<polygon fill="#9AA7B0" points="2 8 15 8 15 7 2 7"/>
<polygon fill="#9AA7B0" points="2 11 15 11 15 10 2 10"/>
<path fill="#9AA7B0" d="M11,13 L14,13 L14,5 L11,5 L11,13 Z M6,13 L10,13 L10,5 L6,5 L6,13 Z M2,13 L5,13 L5,5 L2,5 L2,13 Z M1,14 L15,14 L15,2 L1,2 L1,14 Z"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 561 B

View File

@@ -0,0 +1,4 @@
<!-- Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -->
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M3 3H1V5H3V3ZM3 7H1V9H3V7ZM1 11H3V13H1V11ZM15 3H5V5H15V3ZM5 7H15V9H5V7ZM15 11H5V13H15V11Z" fill="#9AA7B0" fill-opacity="0.8"/>
</svg>

After

Width:  |  Height:  |  Size: 444 B

View File

@@ -0,0 +1,4 @@
<!-- Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -->
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
<path fill="#9AA7B0" fill-opacity=".8" fill-rule="evenodd" d="M8,8 C9.93375,8 11.5,6.43375 11.5,4.5 C11.5,2.56625 9.93375,1 8,1 C6.06625,1 4.5,2.56625 4.5,4.5 C4.5,6.43375 6.06625,8 8,8 Z M8,9.75 C5.66375,9.75 1,10.9225 1,13.25 L1,15 L15,15 L15,13.25 C15,10.9225 10.33625,9.75 8,9.75 Z"/>
</svg>

After

Width:  |  Height:  |  Size: 529 B