Skip to content

Commit

Permalink
Finish demo and releasing version 0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
vincenzopalazzo committed May 8, 2020
1 parent 53abbf1 commit 63893c0
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 24 deletions.
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,30 @@
JTextFieldPlaceholder
# JTextFieldPlaceholder

Custom component developer for JMars 5 application

## TODO
This component has bad design, in the future should introduce the following effect

- [ ] Used a personal UI component as BasicTextFieldPlaceholderUI
- [ ] Introduce a ruled to check the error inside the text field and paint the line with an error color
- [ ] Introduce a toggle button with an icon, inside the icon

## Actual effect

<div align="center">
<img src="https://i.ibb.co/vYpnd3B/Selection-055.png" />
</div>

## Dependency

This component has a dependence from [Material-ui-swing](https://github.com/vincenzopalazzo/material-ui-swing)

## Author

This component is developer by [@vincenzopalazzo](https://github.com/vincenzopalazzo)

<p align="center" style="center">In addition, this component is developed in collaborations with Arizona State University. </p>

<div align="center">
<img src="https://sundevilgymnastics.com/wp-content/uploads/2016/10/ASU-Womens-Gymnastics-Website.png" />
</div>
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ dependencies {

// Use JUnit test framework
implementation(files("$projectDir/devlib/material-ui-swing-1.1.1-rc2.jar"))
implementation(files("$projectDir/devlib/SwingSnackBar-0.0.1.jar"))
}
Binary file added devlib/SwingSnackBar-0.0.1.jar
Binary file not shown.
51 changes: 51 additions & 0 deletions src/main/java/io/vincenzopalazzo/placeholder/CustomTextField.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.vincenzopalazzo.placeholder;

import javax.swing.*;
import javax.swing.plaf.basic.BasicTextFieldUI;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

class CustomTextField extends JTextField {

private JTextFieldPlaceholder textFieldPlaceholder;

public CustomTextField(JTextFieldPlaceholder placeholder) {
setUI(new CustomTextFieldUI());
this.textFieldPlaceholder = placeholder;
}

@Override
public void updateUI() {
setUI(new CustomTextFieldUI());
}

public class CustomTextFieldUI extends BasicTextFieldUI{

protected FocusListener focusListener = new LineFocusListener();

@Override
protected void installListeners() {
super.installListeners();
this.getComponent().addFocusListener(focusListener);
}

@Override
protected void uninstallListeners() {
super.uninstallListeners();
this.getComponent().removeFocusListener(focusListener);
}

public class LineFocusListener implements FocusListener{

@Override
public void focusGained(FocusEvent e) {
textFieldPlaceholder.doFocus();
}

@Override
public void focusLost(FocusEvent e) {
textFieldPlaceholder.focusLose();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package io.vincenzopalazzo.placeholder;

import mdlaf.shadows.RoundedCornerBorder;
import mdlaf.utils.MaterialColors;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;

public class JTextFieldPlaceholder extends JPanel {

protected JLabel iconContainer;
protected JLabel placeholder;
private JSeparator separator;
protected JTextField textField;
protected CustomTextField textField;
protected Color colorLine;

public JTextFieldPlaceholder() {
super(new FlowLayout());
Expand All @@ -24,27 +23,21 @@ protected void initView(){
this.add(iconContainer);

placeholder = new JLabel();
separator = new JSeparator(JSeparator.VERTICAL);
placeholder.setBorder(BorderFactory.createEmptyBorder(0,0,0,2));
this.add(placeholder);
this.add(separator);

textField = new JTextField();
textField = new CustomTextField(this);
textField.setMinimumSize(new Dimension(50, 20));
textField.setPreferredSize(new Dimension(50, 20));
textField.setSize(new Dimension(50, 20));
textField.setPreferredSize(new Dimension(95, 20));
textField.setSize(new Dimension(95, 20));
super.add(textField);

textField.setBorder(BorderFactory.createEmptyBorder());
iconContainer.setBorder(BorderFactory.createEmptyBorder());
placeholder.setBorder(BorderFactory.createEmptyBorder());
setBorder(new RoundedCornerBorder(getBackground(), 6));
}

protected void initStyle(){
setBackground(textField.getBackground());
placeholder.setBackground(getBackground());
iconContainer.setBackground(getBackground());
}

@Override
Expand All @@ -53,21 +46,57 @@ protected void paintComponent(Graphics g) {
this.paintLine(g);
}

protected void paintLine(Graphics graphics){
graphics.setColor(Color.CYAN);
graphics.fillRect(iconContainer.getX(), this.getHeight() - this.getY(), this.getWidth() - iconContainer.getWidth(), 1);
}

public JTextFieldPlaceholder setIcon(Icon icon){
if(icon == null) throw new IllegalArgumentException("icon null");
iconContainer.setIcon(icon);
return this;
}

public JTextFieldPlaceholder setText(String text){
public JTextFieldPlaceholder setPlaceholderText(String text){
if(text == null || text.isEmpty()) throw new IllegalArgumentException("Invalid text");
placeholder.setText(text);
return this;
}

public JTextFieldPlaceholder setText(String text){
if(text == null || text.isEmpty()) throw new IllegalArgumentException("Invalid text");
this.textField.setText(text);
return this;
}

public String getText(){
return textField.getText();
}

public String getPlaceholderText(){
return placeholder.getText();
}

public Icon getIcon(){
return this.iconContainer.getIcon();
}

protected void paintLine(Graphics graphics){
if(colorLine == null){
if(textField.isFocusOwner()){
this.colorLine = UIManager.getColor("TextField[Line].activeColor");;
}else{
this.colorLine = UIManager.getColor("TextField[Line].inactiveColor");
}
}
graphics.setColor(this.colorLine);
graphics.fillRect(iconContainer.getX(), this.getHeight() - this.getY(), this.getWidth() - iconContainer.getWidth(), 1);
}

void doFocus(){
this.colorLine = UIManager.getColor("TextField[Line].activeColor");
this.repaint();
}

void focusLose(){
this.colorLine = UIManager.getColor("TextField[Line].inactiveColor");
this.repaint();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package mdlaf.shadows;
package io.vincenzopalazzo.placeholder;

import mdlaf.utils.MaterialColors;

Expand All @@ -36,7 +36,7 @@
/**
* @author https://github.com/vincenzopalazzo
*/
public class RoundedCornerBorder extends AbstractBorder {
class RoundedCornerBorder extends AbstractBorder {

protected int arch = 12; //default value
protected Color colorLine;
Expand Down
39 changes: 37 additions & 2 deletions src/test/java/io/vincenzopalazzo/placeholder/DemoFrame.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package io.vincenzopalazzo.placeholder;

import io.swingsnackbar.SnackBar;
import io.swingsnackbar.action.AbstractSnackBarAction;
import jiconfont.icons.google_material_design_icons.GoogleMaterialDesignIcons;
import mdlaf.MaterialLookAndFeel;
import mdlaf.themes.JMarsDarkTheme;
import mdlaf.utils.MaterialColors;
import mdlaf.utils.MaterialFontFactory;
import mdlaf.utils.MaterialImageFactory;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;

public class DemoFrame extends JFrame {

Expand All @@ -19,6 +24,7 @@ public class DemoFrame extends JFrame {
}
}

private JFrame frame = this;
private JPanel container;
private JTextFieldPlaceholder textFieldPlaceholder;

Expand All @@ -35,14 +41,43 @@ public void initView() {

public void initComponent() {
container = new JPanel();
//Init component
textFieldPlaceholder = new JTextFieldPlaceholder();

//configure component
textFieldPlaceholder.setIcon(MaterialImageFactory.getInstance().getImage(
GoogleMaterialDesignIcons.BOOKMARK,
MaterialColors.COSMO_DARK_GRAY
))
.setText("Lan/Lon")
.setVisible(true);
.setPlaceholderText("Lan/Lon")
.setVisible(true);

container.add(textFieldPlaceholder);
JButton button = new JButton(MaterialImageFactory.getInstance().getImage(
GoogleMaterialDesignIcons.SEND,
MaterialColors.WHITE
));

button.addActionListener(new AbstractAction() {
private SnackBar snackBar;
@Override
public void actionPerformed(ActionEvent e) {
snackBar = SnackBar.make(frame, "Lan/Lon: " + textFieldPlaceholder.getText(), "CLOSE")
.setGap(80)
.setIconTextStyle(MaterialFontFactory.getInstance().getFont(MaterialFontFactory.BOLD))
.setIconTextColor(MaterialColors.COSMO_RED)
.setDuration(SnackBar.LENGTH_LONG)
.setAction(new AbstractSnackBarAction() {
@Override
public void mousePressed(MouseEvent e) {
snackBar.dismiss();
}
})
.run();
}
});

container.add(button);
}

public static void main(String[] args) {
Expand Down

0 comments on commit 63893c0

Please sign in to comment.