From 1606c19bff4b687a7869090e91359b24c822eed9 Mon Sep 17 00:00:00 2001 From: SoyChai <37604308+SoyChai@users.noreply.github.com> Date: Sun, 12 Aug 2018 08:02:34 +1000 Subject: [PATCH] =?UTF-8?q?UI:=20Add=20a=20clear=20button=20(=C3=97)=20to?= =?UTF-8?q?=20IconTextField=20(#3451)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client/ui/components/IconTextField.java | 87 ++++++++++++++++--- 1 file changed, 76 insertions(+), 11 deletions(-) diff --git a/runelite-client/src/main/java/net/runelite/client/ui/components/IconTextField.java b/runelite-client/src/main/java/net/runelite/client/ui/components/IconTextField.java index 229dab15cb..25eb20efd2 100644 --- a/runelite-client/src/main/java/net/runelite/client/ui/components/IconTextField.java +++ b/runelite-client/src/main/java/net/runelite/client/ui/components/IconTextField.java @@ -26,6 +26,8 @@ */ package net.runelite.client.ui.components; +import net.runelite.client.ui.ColorScheme; +import net.runelite.client.ui.FontManager; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; @@ -35,37 +37,42 @@ import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.ImageIcon; +import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; import javax.swing.text.Document; /** - * This component is a FlatTextField with an icon on its left side. + * This component is a FlatTextField with an icon on its left side, and a clear button (×) on its right side. */ public class IconTextField extends JPanel { + // To support gifs, the icon needs to be wrapped in a JLabel + private final JLabel iconWrapperLabel; + private final FlatTextField textField; - //to support gifs, the icon needs to be wrapped in a JLabel - private final JLabel iconWrapperLabel; + private final JButton clearButton; public IconTextField() { setLayout(new BorderLayout()); - this.iconWrapperLabel = new JLabel(); - this.iconWrapperLabel.setPreferredSize(new Dimension(30, 0)); - this.iconWrapperLabel.setVerticalAlignment(JLabel.CENTER); - this.iconWrapperLabel.setHorizontalAlignment(JLabel.CENTER); + iconWrapperLabel = new JLabel(); + iconWrapperLabel.setPreferredSize(new Dimension(30, 0)); + iconWrapperLabel.setVerticalAlignment(JLabel.CENTER); + iconWrapperLabel.setHorizontalAlignment(JLabel.CENTER); textField = new FlatTextField(); textField.setBorder(null); - JTextField innerTxt = textField.getTextField(); + final JTextField innerTxt = textField.getTextField(); innerTxt.removeMouseListener(innerTxt.getMouseListeners()[innerTxt.getMouseListeners().length - 1]); - MouseListener hoverEffect = new MouseAdapter() + final MouseListener hoverEffect = new MouseAdapter() { @Override public void mouseEntered(MouseEvent mouseEvent) @@ -75,7 +82,7 @@ public class IconTextField extends JPanel return; } - Color hoverColor = textField.getHoverBackgroundColor(); + final Color hoverColor = textField.getHoverBackgroundColor(); if (hoverColor != null) { @@ -95,8 +102,66 @@ public class IconTextField extends JPanel textField.addMouseListener(hoverEffect); innerTxt.addMouseListener(hoverEffect); + clearButton = new JButton("×"); + clearButton.setPreferredSize(new Dimension(30, 0)); + clearButton.setFont(FontManager.getRunescapeBoldFont()); + clearButton.setForeground(ColorScheme.PROGRESS_ERROR_COLOR); + clearButton.setBorder(null); + clearButton.setBorderPainted(false); + clearButton.setContentAreaFilled(false); + clearButton.setVisible(false); + + // ActionListener for keyboard use (via Tab -> Space) + clearButton.addActionListener((l) -> setText(null)); + + // MouseListener for hover and click events + clearButton.addMouseListener(new MouseAdapter() + { + @Override + public void mousePressed(MouseEvent mouseEvent) + { + setText(null); + } + + @Override + public void mouseEntered(MouseEvent mouseEvent) + { + clearButton.setForeground(Color.PINK); + textField.dispatchEvent(mouseEvent); + } + + @Override + public void mouseExited(MouseEvent mouseEvent) + { + clearButton.setForeground(ColorScheme.PROGRESS_ERROR_COLOR); + textField.dispatchEvent(mouseEvent); + } + }); + + // Show the clear button when text is present, and hide again when empty + getDocument().addDocumentListener(new DocumentListener() + { + @Override + public void insertUpdate(DocumentEvent e) + { + clearButton.setVisible(true); + } + + @Override + public void removeUpdate(DocumentEvent e) + { + clearButton.setVisible(!getText().isEmpty()); + } + + @Override + public void changedUpdate(DocumentEvent e) + { + } + }); + add(iconWrapperLabel, BorderLayout.WEST); add(textField, BorderLayout.CENTER); + add(clearButton, BorderLayout.EAST); } public void addActionListener(ActionListener actionListener) @@ -178,4 +243,4 @@ public class IconTextField extends JPanel return textField.getDocument(); } -} \ No newline at end of file +}