Skip to content

Commit 63893c0

Browse files
Finish demo and releasing version 0.0.1
1 parent 53abbf1 commit 63893c0

File tree

7 files changed

+169
-24
lines changed

7 files changed

+169
-24
lines changed

README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,30 @@
1-
JTextFieldPlaceholder
1+
# JTextFieldPlaceholder
2+
3+
Custom component developer for JMars 5 application
4+
5+
## TODO
6+
This component has bad design, in the future should introduce the following effect
7+
8+
- [ ] Used a personal UI component as BasicTextFieldPlaceholderUI
9+
- [ ] Introduce a ruled to check the error inside the text field and paint the line with an error color
10+
- [ ] Introduce a toggle button with an icon, inside the icon
11+
12+
## Actual effect
13+
14+
<div align="center">
15+
<img src="https://i.ibb.co/vYpnd3B/Selection-055.png" />
16+
</div>
17+
18+
## Dependency
19+
20+
This component has a dependence from [Material-ui-swing](https://github.com/vincenzopalazzo/material-ui-swing)
21+
22+
## Author
23+
24+
This component is developer by [@vincenzopalazzo](https://github.com/vincenzopalazzo)
25+
26+
<p align="center" style="center">In addition, this component is developed in collaborations with Arizona State University. </p>
27+
28+
<div align="center">
29+
<img src="https://sundevilgymnastics.com/wp-content/uploads/2016/10/ASU-Womens-Gymnastics-Website.png" />
30+
</div>

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ dependencies {
2626

2727
// Use JUnit test framework
2828
implementation(files("$projectDir/devlib/material-ui-swing-1.1.1-rc2.jar"))
29+
implementation(files("$projectDir/devlib/SwingSnackBar-0.0.1.jar"))
2930
}

devlib/SwingSnackBar-0.0.1.jar

16.9 KB
Binary file not shown.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package io.vincenzopalazzo.placeholder;
2+
3+
import javax.swing.*;
4+
import javax.swing.plaf.basic.BasicTextFieldUI;
5+
import java.awt.event.FocusEvent;
6+
import java.awt.event.FocusListener;
7+
8+
class CustomTextField extends JTextField {
9+
10+
private JTextFieldPlaceholder textFieldPlaceholder;
11+
12+
public CustomTextField(JTextFieldPlaceholder placeholder) {
13+
setUI(new CustomTextFieldUI());
14+
this.textFieldPlaceholder = placeholder;
15+
}
16+
17+
@Override
18+
public void updateUI() {
19+
setUI(new CustomTextFieldUI());
20+
}
21+
22+
public class CustomTextFieldUI extends BasicTextFieldUI{
23+
24+
protected FocusListener focusListener = new LineFocusListener();
25+
26+
@Override
27+
protected void installListeners() {
28+
super.installListeners();
29+
this.getComponent().addFocusListener(focusListener);
30+
}
31+
32+
@Override
33+
protected void uninstallListeners() {
34+
super.uninstallListeners();
35+
this.getComponent().removeFocusListener(focusListener);
36+
}
37+
38+
public class LineFocusListener implements FocusListener{
39+
40+
@Override
41+
public void focusGained(FocusEvent e) {
42+
textFieldPlaceholder.doFocus();
43+
}
44+
45+
@Override
46+
public void focusLost(FocusEvent e) {
47+
textFieldPlaceholder.focusLose();
48+
}
49+
}
50+
}
51+
}
Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package io.vincenzopalazzo.placeholder;
22

3-
import mdlaf.shadows.RoundedCornerBorder;
3+
import mdlaf.utils.MaterialColors;
44

55
import javax.swing.*;
6-
import javax.swing.border.Border;
76
import java.awt.*;
87

98
public class JTextFieldPlaceholder extends JPanel {
109

1110
protected JLabel iconContainer;
1211
protected JLabel placeholder;
13-
private JSeparator separator;
14-
protected JTextField textField;
12+
protected CustomTextField textField;
13+
protected Color colorLine;
1514

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

2625
placeholder = new JLabel();
27-
separator = new JSeparator(JSeparator.VERTICAL);
2826
placeholder.setBorder(BorderFactory.createEmptyBorder(0,0,0,2));
2927
this.add(placeholder);
30-
this.add(separator);
3128

32-
textField = new JTextField();
29+
textField = new CustomTextField(this);
3330
textField.setMinimumSize(new Dimension(50, 20));
34-
textField.setPreferredSize(new Dimension(50, 20));
35-
textField.setSize(new Dimension(50, 20));
31+
textField.setPreferredSize(new Dimension(95, 20));
32+
textField.setSize(new Dimension(95, 20));
3633
super.add(textField);
3734

3835
textField.setBorder(BorderFactory.createEmptyBorder());
39-
iconContainer.setBorder(BorderFactory.createEmptyBorder());
40-
placeholder.setBorder(BorderFactory.createEmptyBorder());
4136
setBorder(new RoundedCornerBorder(getBackground(), 6));
4237
}
4338

4439
protected void initStyle(){
4540
setBackground(textField.getBackground());
46-
placeholder.setBackground(getBackground());
47-
iconContainer.setBackground(getBackground());
4841
}
4942

5043
@Override
@@ -53,21 +46,57 @@ protected void paintComponent(Graphics g) {
5346
this.paintLine(g);
5447
}
5548

56-
protected void paintLine(Graphics graphics){
57-
graphics.setColor(Color.CYAN);
58-
graphics.fillRect(iconContainer.getX(), this.getHeight() - this.getY(), this.getWidth() - iconContainer.getWidth(), 1);
59-
}
60-
6149
public JTextFieldPlaceholder setIcon(Icon icon){
6250
if(icon == null) throw new IllegalArgumentException("icon null");
6351
iconContainer.setIcon(icon);
6452
return this;
6553
}
6654

67-
public JTextFieldPlaceholder setText(String text){
55+
public JTextFieldPlaceholder setPlaceholderText(String text){
6856
if(text == null || text.isEmpty()) throw new IllegalArgumentException("Invalid text");
6957
placeholder.setText(text);
7058
return this;
7159
}
7260

61+
public JTextFieldPlaceholder setText(String text){
62+
if(text == null || text.isEmpty()) throw new IllegalArgumentException("Invalid text");
63+
this.textField.setText(text);
64+
return this;
65+
}
66+
67+
public String getText(){
68+
return textField.getText();
69+
}
70+
71+
public String getPlaceholderText(){
72+
return placeholder.getText();
73+
}
74+
75+
public Icon getIcon(){
76+
return this.iconContainer.getIcon();
77+
}
78+
79+
protected void paintLine(Graphics graphics){
80+
if(colorLine == null){
81+
if(textField.isFocusOwner()){
82+
this.colorLine = UIManager.getColor("TextField[Line].activeColor");;
83+
}else{
84+
this.colorLine = UIManager.getColor("TextField[Line].inactiveColor");
85+
}
86+
}
87+
graphics.setColor(this.colorLine);
88+
graphics.fillRect(iconContainer.getX(), this.getHeight() - this.getY(), this.getWidth() - iconContainer.getWidth(), 1);
89+
}
90+
91+
void doFocus(){
92+
this.colorLine = UIManager.getColor("TextField[Line].activeColor");
93+
this.repaint();
94+
}
95+
96+
void focusLose(){
97+
this.colorLine = UIManager.getColor("TextField[Line].inactiveColor");
98+
this.repaint();
99+
}
100+
101+
73102
}

src/main/java/io/vincenzopalazzo/placeholder/RoundedCornerBorder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
* SOFTWARE.
2323
*/
24-
package mdlaf.shadows;
24+
package io.vincenzopalazzo.placeholder;
2525

2626
import mdlaf.utils.MaterialColors;
2727

@@ -36,7 +36,7 @@
3636
/**
3737
* @author https://github.com/vincenzopalazzo
3838
*/
39-
public class RoundedCornerBorder extends AbstractBorder {
39+
class RoundedCornerBorder extends AbstractBorder {
4040

4141
protected int arch = 12; //default value
4242
protected Color colorLine;

src/test/java/io/vincenzopalazzo/placeholder/DemoFrame.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package io.vincenzopalazzo.placeholder;
22

3+
import io.swingsnackbar.SnackBar;
4+
import io.swingsnackbar.action.AbstractSnackBarAction;
35
import jiconfont.icons.google_material_design_icons.GoogleMaterialDesignIcons;
46
import mdlaf.MaterialLookAndFeel;
57
import mdlaf.themes.JMarsDarkTheme;
68
import mdlaf.utils.MaterialColors;
9+
import mdlaf.utils.MaterialFontFactory;
710
import mdlaf.utils.MaterialImageFactory;
811

912
import javax.swing.*;
1013
import java.awt.*;
14+
import java.awt.event.ActionEvent;
15+
import java.awt.event.MouseEvent;
1116

1217
public class DemoFrame extends JFrame {
1318

@@ -19,6 +24,7 @@ public class DemoFrame extends JFrame {
1924
}
2025
}
2126

27+
private JFrame frame = this;
2228
private JPanel container;
2329
private JTextFieldPlaceholder textFieldPlaceholder;
2430

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

3642
public void initComponent() {
3743
container = new JPanel();
44+
//Init component
3845
textFieldPlaceholder = new JTextFieldPlaceholder();
46+
47+
//configure component
3948
textFieldPlaceholder.setIcon(MaterialImageFactory.getInstance().getImage(
4049
GoogleMaterialDesignIcons.BOOKMARK,
4150
MaterialColors.COSMO_DARK_GRAY
4251
))
43-
.setText("Lan/Lon")
44-
.setVisible(true);
52+
.setPlaceholderText("Lan/Lon")
53+
.setVisible(true);
54+
4555
container.add(textFieldPlaceholder);
56+
JButton button = new JButton(MaterialImageFactory.getInstance().getImage(
57+
GoogleMaterialDesignIcons.SEND,
58+
MaterialColors.WHITE
59+
));
60+
61+
button.addActionListener(new AbstractAction() {
62+
private SnackBar snackBar;
63+
@Override
64+
public void actionPerformed(ActionEvent e) {
65+
snackBar = SnackBar.make(frame, "Lan/Lon: " + textFieldPlaceholder.getText(), "CLOSE")
66+
.setGap(80)
67+
.setIconTextStyle(MaterialFontFactory.getInstance().getFont(MaterialFontFactory.BOLD))
68+
.setIconTextColor(MaterialColors.COSMO_RED)
69+
.setDuration(SnackBar.LENGTH_LONG)
70+
.setAction(new AbstractSnackBarAction() {
71+
@Override
72+
public void mousePressed(MouseEvent e) {
73+
snackBar.dismiss();
74+
}
75+
})
76+
.run();
77+
}
78+
});
79+
80+
container.add(button);
4681
}
4782

4883
public static void main(String[] args) {

0 commit comments

Comments
 (0)