UI: Add a clear button (×) to IconTextField (#3451)
This commit is contained in:
@@ -26,6 +26,8 @@
|
|||||||
*/
|
*/
|
||||||
package net.runelite.client.ui.components;
|
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.BorderLayout;
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.awt.Dimension;
|
import java.awt.Dimension;
|
||||||
@@ -35,37 +37,42 @@ import java.awt.event.MouseAdapter;
|
|||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
import java.awt.event.MouseListener;
|
import java.awt.event.MouseListener;
|
||||||
import javax.swing.ImageIcon;
|
import javax.swing.ImageIcon;
|
||||||
|
import javax.swing.JButton;
|
||||||
import javax.swing.JLabel;
|
import javax.swing.JLabel;
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
import javax.swing.JTextField;
|
import javax.swing.JTextField;
|
||||||
|
import javax.swing.event.DocumentEvent;
|
||||||
|
import javax.swing.event.DocumentListener;
|
||||||
import javax.swing.text.Document;
|
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
|
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;
|
private final FlatTextField textField;
|
||||||
|
|
||||||
//to support gifs, the icon needs to be wrapped in a JLabel
|
private final JButton clearButton;
|
||||||
private final JLabel iconWrapperLabel;
|
|
||||||
|
|
||||||
public IconTextField()
|
public IconTextField()
|
||||||
{
|
{
|
||||||
setLayout(new BorderLayout());
|
setLayout(new BorderLayout());
|
||||||
|
|
||||||
this.iconWrapperLabel = new JLabel();
|
iconWrapperLabel = new JLabel();
|
||||||
this.iconWrapperLabel.setPreferredSize(new Dimension(30, 0));
|
iconWrapperLabel.setPreferredSize(new Dimension(30, 0));
|
||||||
this.iconWrapperLabel.setVerticalAlignment(JLabel.CENTER);
|
iconWrapperLabel.setVerticalAlignment(JLabel.CENTER);
|
||||||
this.iconWrapperLabel.setHorizontalAlignment(JLabel.CENTER);
|
iconWrapperLabel.setHorizontalAlignment(JLabel.CENTER);
|
||||||
|
|
||||||
textField = new FlatTextField();
|
textField = new FlatTextField();
|
||||||
textField.setBorder(null);
|
textField.setBorder(null);
|
||||||
|
|
||||||
JTextField innerTxt = textField.getTextField();
|
final JTextField innerTxt = textField.getTextField();
|
||||||
innerTxt.removeMouseListener(innerTxt.getMouseListeners()[innerTxt.getMouseListeners().length - 1]);
|
innerTxt.removeMouseListener(innerTxt.getMouseListeners()[innerTxt.getMouseListeners().length - 1]);
|
||||||
|
|
||||||
MouseListener hoverEffect = new MouseAdapter()
|
final MouseListener hoverEffect = new MouseAdapter()
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
public void mouseEntered(MouseEvent mouseEvent)
|
public void mouseEntered(MouseEvent mouseEvent)
|
||||||
@@ -75,7 +82,7 @@ public class IconTextField extends JPanel
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Color hoverColor = textField.getHoverBackgroundColor();
|
final Color hoverColor = textField.getHoverBackgroundColor();
|
||||||
|
|
||||||
if (hoverColor != null)
|
if (hoverColor != null)
|
||||||
{
|
{
|
||||||
@@ -95,8 +102,66 @@ public class IconTextField extends JPanel
|
|||||||
textField.addMouseListener(hoverEffect);
|
textField.addMouseListener(hoverEffect);
|
||||||
innerTxt.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(iconWrapperLabel, BorderLayout.WEST);
|
||||||
add(textField, BorderLayout.CENTER);
|
add(textField, BorderLayout.CENTER);
|
||||||
|
add(clearButton, BorderLayout.EAST);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addActionListener(ActionListener actionListener)
|
public void addActionListener(ActionListener actionListener)
|
||||||
@@ -178,4 +243,4 @@ public class IconTextField extends JPanel
|
|||||||
return textField.getDocument();
|
return textField.getDocument();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user