-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Display placeholder even when focused
- Loading branch information
Showing
7 changed files
with
197 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 0 additions & 69 deletions
69
src/main/java/net/sf/jabref/gui/fieldeditors/JTextAreaWithUnfocusedText.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/main/java/net/sf/jabref/gui/util/component/JTextAreaWithPlaceholder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package net.sf.jabref.gui.util.component; | ||
|
||
import java.awt.Color; | ||
import java.awt.Font; | ||
import java.awt.Graphics; | ||
import java.awt.Graphics2D; | ||
import java.awt.RenderingHints; | ||
import java.awt.event.KeyEvent; | ||
import java.awt.event.KeyListener; | ||
|
||
import javax.swing.JTextArea; | ||
import javax.swing.UIManager; | ||
|
||
/** | ||
* A text area which displays a predefined text the same way as {@link JTextFieldWithPlaceholder} does. | ||
*/ | ||
public class JTextAreaWithPlaceholder extends JTextArea implements KeyListener { | ||
|
||
private final String textWhenNotFocused; | ||
|
||
public JTextAreaWithPlaceholder() { | ||
this(""); | ||
} | ||
|
||
/** | ||
* Additionally to {@link JTextAreaWithPlaceholder#JTextAreaWithPlaceholder(String)} | ||
* this also sets the initial text of the text field component. | ||
* | ||
* @param content as the text of the textfield | ||
* @param placeholder as the placeholder of the textfield | ||
*/ | ||
public JTextAreaWithPlaceholder(String content, String placeholder) { | ||
this(placeholder); | ||
setText(content); | ||
} | ||
|
||
/** | ||
* This will create a {@link JTextArea} with a placeholder text. The placeholder | ||
* will always be displayed if the text of the {@link JTextArea} is empty. | ||
* | ||
* @param placeholder as the placeholder of the textarea | ||
*/ | ||
public JTextAreaWithPlaceholder(String placeholder) { | ||
super(); | ||
this.setEditable(true); | ||
this.setText(""); | ||
this.textWhenNotFocused = placeholder; | ||
} | ||
|
||
@Override | ||
protected void paintComponent(Graphics graphics) { | ||
super.paintComponent(graphics); | ||
|
||
if (this.getText().isEmpty()) { | ||
Font prev = graphics.getFont(); | ||
Color prevColor = graphics.getColor(); | ||
graphics.setColor(UIManager.getColor("textInactiveText")); | ||
int textHeight = graphics.getFontMetrics().getHeight(); | ||
int x = this.getInsets().left; | ||
Graphics2D g2d = (Graphics2D) graphics; | ||
RenderingHints hints = g2d.getRenderingHints(); | ||
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); | ||
g2d.drawString(textWhenNotFocused, x, textHeight + this.getInsets().top); | ||
g2d.setRenderingHints(hints); | ||
graphics.setFont(prev); | ||
graphics.setColor(prevColor); | ||
} | ||
} | ||
|
||
@Override | ||
public void keyTyped(KeyEvent e) { | ||
if (this.getText().isEmpty()) { | ||
this.repaint(); | ||
} | ||
} | ||
|
||
@Override | ||
public void keyPressed(KeyEvent e) { | ||
if (this.getText().isEmpty()) { | ||
this.repaint(); | ||
} | ||
} | ||
|
||
@Override | ||
public void keyReleased(KeyEvent e) { | ||
if (this.getText().isEmpty()) { | ||
this.repaint(); | ||
} | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
src/main/java/net/sf/jabref/gui/util/component/JTextFieldWithPlaceholder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package net.sf.jabref.gui.util.component; | ||
|
||
import java.awt.Color; | ||
import java.awt.Font; | ||
import java.awt.Graphics; | ||
import java.awt.Graphics2D; | ||
import java.awt.RenderingHints; | ||
import java.awt.event.KeyEvent; | ||
import java.awt.event.KeyListener; | ||
|
||
import javax.swing.JTextField; | ||
import javax.swing.UIManager; | ||
|
||
/** | ||
* A text field which displays a predefined text (e.g. "Search") if the text field is empty. | ||
* This is similar to a html5 input element with a defined placeholder attribute. | ||
* Implementation based on https://gmigdos.wordpress.com/2010/03/30/java-a-custom-jtextfield-for-searching/ | ||
*/ | ||
public class JTextFieldWithPlaceholder extends JTextField implements KeyListener { | ||
|
||
private final String textWhenNotFocused; | ||
|
||
/** | ||
* Additionally to {@link JTextFieldWithPlaceholder#JTextFieldWithPlaceholder(String)} | ||
* this also sets the initial text of the text field component. | ||
* | ||
* @param content as the text of the textfield | ||
* @param placeholder as the placeholder of the textfield | ||
*/ | ||
public JTextFieldWithPlaceholder(String content, String placeholder) { | ||
this(placeholder); | ||
setText(content); | ||
} | ||
|
||
/** | ||
* This will create a {@link JTextField} with a placeholder text. The placeholder | ||
* will always be displayed if the text of the {@link JTextField} is empty. | ||
* | ||
* @param placeholder as the placeholder of the textfield | ||
*/ | ||
public JTextFieldWithPlaceholder(String placeholder) { | ||
super(); | ||
this.setEditable(true); | ||
this.setText(""); | ||
this.textWhenNotFocused = placeholder; | ||
} | ||
|
||
@Override | ||
protected void paintComponent(Graphics graphics) { | ||
super.paintComponent(graphics); | ||
|
||
if (this.getText().isEmpty()) { | ||
int height = this.getHeight(); | ||
Font prev = graphics.getFont(); | ||
Color prevColor = graphics.getColor(); | ||
graphics.setColor(UIManager.getColor("textInactiveText")); | ||
int textHeight = graphics.getFontMetrics().getHeight(); | ||
int textBottom = (((height - textHeight) / 2) + textHeight) - 4; | ||
int x = this.getInsets().left; | ||
Graphics2D g2d = (Graphics2D) graphics; | ||
RenderingHints hints = g2d.getRenderingHints(); | ||
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); | ||
g2d.drawString(textWhenNotFocused, x, textBottom); | ||
g2d.setRenderingHints(hints); | ||
graphics.setFont(prev); | ||
graphics.setColor(prevColor); | ||
} | ||
} | ||
|
||
@Override | ||
public void keyTyped(KeyEvent e) { | ||
if (this.getText().isEmpty()) { | ||
this.repaint(); | ||
} | ||
} | ||
|
||
@Override | ||
public void keyPressed(KeyEvent e) { | ||
if (this.getText().isEmpty()) { | ||
this.repaint(); | ||
} | ||
} | ||
|
||
@Override | ||
public void keyReleased(KeyEvent e) { | ||
if (this.getText().isEmpty()) { | ||
this.repaint(); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.