Skip to content

Commit ef16f56

Browse files
author
atarw
committed
added support for jtables, fixed a color selection bug for jtextfields
1 parent e26ea0a commit ef16f56

14 files changed

+472
-149
lines changed

.idea/workspace.xml

Lines changed: 217 additions & 102 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/material-ui-swing.jar

5.83 KB
Binary file not shown.

src/mdlaf/DropShadowBorder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public class DropShadowBorder extends AbstractBorder implements Border {
6161
private boolean showLeftShadow;
6262
private boolean showBottomShadow;
6363
private boolean showRightShadow;
64+
6465
public DropShadowBorder () {
6566
this (UIManager.getColor ("Control"), 1, 5);
6667
}
@@ -98,7 +99,7 @@ public void paintBorder (Component c, Graphics graphics, int x, int y, int width
9899
Map<Position, BufferedImage> images = getImages (null);
99100

100101
//compute the edges of the component -- not including the border
101-
//Insets borderInsets = getBorderInsets(c);
102+
//Insets borderInsets = getBorderInsets (c);
102103
// int leftEdge = x + borderInsets.left - lineWidth;
103104
// int rightEdge = x + width - borderInsets.right;
104105
// int topEdge = y + borderInsets.top - lineWidth;

src/mdlaf/MaterialBorders.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package mdlaf;
2+
3+
import javax.swing.BorderFactory;
4+
import javax.swing.border.Border;
5+
import java.awt.Color;
6+
7+
public class MaterialBorders {
8+
9+
public static final Border LIGHT_LINE_BORDER = BorderFactory.createLineBorder (MaterialColors.LIGHT_GRAY, 1);
10+
11+
public static final Border LIGHT_SHADOW_BORDER = new DropShadowBorder (Color.BLACK, 0, 4, 0.3f, 12, true, true, true, true);
12+
public static final Border DEFAULT_SHADOW_BORDER = new DropShadowBorder (Color.BLACK, 5, 5, 0.3f, 12, true, true, true, true);
13+
14+
private MaterialBorders () {}
15+
}

src/mdlaf/MaterialFonts.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ private static Font loadFont (String fontPath) {
3131
throw new RuntimeException ("Font " + fontPath + " wasn't loaded");
3232
}
3333
}
34+
35+
private MaterialFonts () {}
3436
}

src/mdlaf/MaterialLookAndFeel.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
import mdlaf.button.MaterialButtonUI;
44
import mdlaf.password.MaterialPasswordFieldUI;
5+
import mdlaf.table.MaterialTableHeaderUI;
6+
import mdlaf.table.MaterialTableUI;
57
import mdlaf.textfield.MaterialTextFieldUI;
68

79
import javax.swing.BorderFactory;
810
import javax.swing.UIDefaults;
9-
import javax.swing.border.Border;
1011
import javax.swing.plaf.basic.BasicLookAndFeel;
1112
import java.awt.Color;
1213

@@ -15,6 +16,8 @@ public class MaterialLookAndFeel extends BasicLookAndFeel {
1516
private static final String buttonUI = MaterialButtonUI.class.getCanonicalName ();
1617
private static final String textfieldUI = MaterialTextFieldUI.class.getCanonicalName ();
1718
private static final String passwordFieldUI = MaterialPasswordFieldUI.class.getCanonicalName ();
19+
private static final String tableUI = MaterialTableUI.class.getCanonicalName ();
20+
private static final String tableHeaderUI = MaterialTableHeaderUI.class.getCanonicalName ();
1821

1922
@Override
2023

@@ -48,6 +51,8 @@ protected void initClassDefaults (UIDefaults table) {
4851
table.put ("ButtonUI", buttonUI);
4952
table.put ("TextFieldUI", textfieldUI);
5053
table.put ("PasswordFieldUI", passwordFieldUI);
54+
table.put ("TableUI", tableUI);
55+
table.put ("TableHeaderUI", tableHeaderUI);
5156
}
5257

5358
@Override
@@ -63,7 +68,6 @@ protected void initComponentDefaults (UIDefaults table) {
6368
System.setProperty ("swing.aatext", "true");
6469
System.setProperty ("sun.java2d.xrender", "true");
6570

66-
table.put ("Button.font", MaterialFonts.MEDIUM);
6771
table.put ("RadioButton.font", MaterialFonts.REGULAR);
6872
table.put ("CheckBox.font", MaterialFonts.REGULAR);
6973
table.put ("ComboBox.font", MaterialFonts.REGULAR);
@@ -74,14 +78,8 @@ protected void initComponentDefaults (UIDefaults table) {
7478
table.put ("OptionPane.font", MaterialFonts.REGULAR);
7579
table.put ("Panel.font", MaterialFonts.REGULAR);
7680
table.put ("ScrollPane.font", MaterialFonts.REGULAR);
77-
table.put ("Table.font", MaterialFonts.REGULAR);
78-
table.put ("TableHeader.font", MaterialFonts.REGULAR);
79-
table.put ("TextField.font", MaterialFonts.REGULAR);
8081
table.put ("TextArea.font", MaterialFonts.REGULAR);
8182

82-
Border menuBorder = new DropShadowBorder (Color.BLACK, 0, 5, 0.3f, 12, true, true, true, true);
83-
Border defaultBorder = new DropShadowBorder (Color.BLACK, 5, 5, 0.3f, 12, true, true, true, true);
84-
8583
table.put ("Panel.background", Color.WHITE);
8684
table.put ("Panel.border", BorderFactory.createEmptyBorder ());
8785

@@ -94,6 +92,7 @@ protected void initComponentDefaults (UIDefaults table) {
9492

9593
table.put ("PopupMenu.border", BorderFactory.createLineBorder (MaterialColors.LIGHT_GRAY, 1));
9694
table.put ("PopupMenu.background", Color.WHITE);
95+
9796
table.put ("Menu.border", BorderFactory.createEmptyBorder (5, 5, 5, 5));
9897
table.put ("Menu.selectionBackground", MaterialColors.LIGHT_GRAY);
9998
table.put ("Menu.selectionForeground", Color.BLACK);
@@ -104,7 +103,7 @@ protected void initComponentDefaults (UIDefaults table) {
104103
table.put ("Menu.menuPopupOffsetY", 10);
105104

106105
table.put ("MenuBar.background", Color.WHITE);
107-
table.put ("MenuBar.border", menuBorder);
106+
table.put ("MenuBar.border", MaterialBorders.LIGHT_SHADOW_BORDER);
108107

109108
table.put ("SplitPane.border", BorderFactory.createEmptyBorder ());
110109
table.put ("SplitPane.background", Color.WHITE);
@@ -114,20 +113,13 @@ protected void initComponentDefaults (UIDefaults table) {
114113
table.put ("ScrollPane.background", Color.WHITE);
115114
table.put ("ScrollPane.border", BorderFactory.createEmptyBorder ());
116115

117-
table.put ("TextField.background", MaterialColors.LIGHT_BLUE);
118-
119-
table.put ("PasswordField.background", MaterialColors.LIGHT_BLUE);
120-
121116
table.put ("TextArea.background", MaterialColors.LIGHT_GRAY);
122117
table.put ("TextArea.border", BorderFactory.createEmptyBorder ());
123118
table.put ("TextArea.foreground", Color.BLACK);
124119

125120
table.put ("OptionPane.background", Color.WHITE);
126-
table.put ("OptionPane.border", defaultBorder);
121+
table.put ("OptionPane.border", MaterialBorders.DEFAULT_SHADOW_BORDER);
127122

128-
table.put ("Button.background", MaterialColors.LIGHT_BLUE);
129-
table.put ("Button.foreground", Color.WHITE);
130123
table.put ("Button.highlight", MaterialColors.LIGHT_GRAY);
131-
//table.put ("Button.border", BorderFactory.createEmptyBorder (10, 10, 10, 10));
132124
}
133125
}

src/mdlaf/button/MaterialButtonUI.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
package mdlaf.button;
22

3+
import mdlaf.MaterialColors;
4+
import mdlaf.MaterialFonts;
5+
36
import javax.swing.AbstractButton;
47
import javax.swing.BorderFactory;
58
import javax.swing.JComponent;
6-
import javax.swing.Timer;
79
import javax.swing.plaf.ComponentUI;
810
import javax.swing.plaf.basic.BasicButtonUI;
11+
import java.awt.Color;
912
import java.awt.Graphics;
1013
import java.awt.Graphics2D;
1114
import java.awt.RenderingHints;
1215

1316
public class MaterialButtonUI extends BasicButtonUI {
1417

15-
private Timer timer;
16-
private AbstractButton button;
17-
private Graphics g;
18-
private int animationRadius = 0;
19-
2018
public static ComponentUI createUI (final JComponent c) {
2119
return new MaterialButtonUI ();
2220
}
@@ -28,6 +26,10 @@ public void installUI (JComponent c) {
2826
AbstractButton button = (AbstractButton) c;
2927
button.setOpaque (false);
3028
button.setBorder (BorderFactory.createEmptyBorder (7, 17, 7, 17));
29+
30+
button.setBackground (MaterialColors.LIGHT_BLUE);
31+
button.setForeground (Color.WHITE);
32+
button.setFont (MaterialFonts.MEDIUM);
3133
}
3234

3335
@Override

src/mdlaf/password/MaterialPasswordFieldUI.java

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
import java.awt.event.FocusEvent;
2424
import java.awt.event.FocusListener;
2525
import java.awt.event.KeyEvent;
26+
import java.beans.PropertyChangeEvent;
27+
import java.beans.PropertyChangeListener;
2628

27-
public class MaterialPasswordFieldUI extends BasicPasswordFieldUI implements FocusListener {
29+
public class MaterialPasswordFieldUI extends BasicPasswordFieldUI implements FocusListener, PropertyChangeListener {
2830

2931
private Color focusedBackground;
3032
private Color unfocusedBackground;
31-
private Color focusedSelectedBackground;
32-
private Color unfocusedSelectedBackground;
33+
private Color focusedSelectionBackground;
34+
private Color unfocusedSelectionBackground;
3335

3436
public static ComponentUI createUI (final JComponent c) {
3537
return new MaterialPasswordFieldUI ();
@@ -39,20 +41,22 @@ public static ComponentUI createUI (final JComponent c) {
3941
public void installUI (JComponent c) {
4042
super.installUI (c);
4143

42-
JPasswordField textField = (JPasswordField) c;
43-
textField.setOpaque (false);
44-
textField.setBorder (BorderFactory.createEmptyBorder (5, 2, 10, 0));
44+
JPasswordField passwordField = (JPasswordField) c;
45+
passwordField.setOpaque (false);
46+
passwordField.setBorder (BorderFactory.createEmptyBorder (5, 2, 10, 0));
47+
passwordField.setBackground (MaterialColors.LIGHT_BLUE);
4548

46-
this.focusedBackground = textField.getBackground ();
49+
this.focusedBackground = passwordField.getBackground ();
4750
this.unfocusedBackground = MaterialColors.LIGHT_GRAY;
4851

49-
this.focusedSelectedBackground = MaterialColors.bleach (focusedBackground, 0.3f);
50-
this.unfocusedSelectedBackground = unfocusedBackground;
52+
this.focusedSelectionBackground = MaterialColors.bleach (focusedBackground, 0.3f);
53+
this.unfocusedSelectionBackground = unfocusedBackground;
5154
}
5255

5356
@Override
5457
protected void installListeners () {
5558
getComponent ().addFocusListener (this);
59+
getComponent ().addPropertyChangeListener (this);
5660
}
5761

5862
@Override
@@ -118,15 +122,19 @@ public void paintSafely (Graphics g) {
118122

119123
if (getComponent ().hasFocus ()) {
120124
c.setBackground (focusedBackground);
121-
c.setSelectionColor (focusedSelectedBackground);
125+
c.setSelectionColor (focusedSelectionBackground);
122126
}
123127
else {
124128
c.setBackground (unfocusedBackground);
125-
c.setSelectionColor (unfocusedSelectedBackground);
129+
c.setSelectionColor (unfocusedSelectionBackground);
126130
}
127131

132+
int x = getComponent ().getInsets ().left;
133+
int y = getComponent ().getInsets ().top;
134+
int w = getComponent ().getWidth () - getComponent ().getInsets ().left - getComponent ().getInsets ().right;
135+
128136
g.setColor (c.getBackground ());
129-
g.fillRect (0, c.getHeight () - c.getY (), c.getWidth (), 2);
137+
g.fillRect (x, c.getHeight () - y, w, 2);
130138

131139
super.paintSafely (g);
132140
}
@@ -151,6 +159,18 @@ public String getPropertyPrefix () {
151159
return "PasswordField";
152160
}
153161

162+
@Override
163+
public void propertyChange (PropertyChangeEvent pce) {
164+
if (pce.getPropertyName ().equals ("background")) {
165+
Color newColor = (Color) pce.getNewValue ();
166+
167+
if (!newColor.equals (focusedBackground) && !newColor.equals (unfocusedBackground)) {
168+
this.focusedBackground = (Color) pce.getNewValue ();
169+
this.focusedSelectionBackground = MaterialColors.bleach (this.focusedBackground, 0.3f);
170+
}
171+
}
172+
}
173+
154174
@Override
155175
public View create (Element elem) {
156176
return new MaterialPasswordView (elem);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package mdlaf.table;
2+
3+
import javax.swing.DefaultCellEditor;
4+
import javax.swing.JTable;
5+
import javax.swing.JTextField;
6+
import java.awt.Component;
7+
8+
public class MaterialTableCellEditor extends DefaultCellEditor {
9+
10+
@Override
11+
public Component getTableCellEditorComponent (JTable table, Object value, boolean isSelected, int rowIndex, int vColIndex) {
12+
JTextField textField = (JTextField) super.getTableCellEditorComponent (table, value, isSelected, rowIndex, vColIndex);
13+
textField.setText ((String) value);
14+
15+
return textField;
16+
}
17+
18+
public MaterialTableCellEditor () {
19+
super (new JTextField ());
20+
}
21+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package mdlaf.table;
2+
3+
import javax.swing.BorderFactory;
4+
import javax.swing.JComponent;
5+
import javax.swing.JTable;
6+
import javax.swing.SwingConstants;
7+
import javax.swing.table.DefaultTableCellRenderer;
8+
import java.awt.Component;
9+
10+
public class MaterialTableCellRenderer extends DefaultTableCellRenderer {
11+
12+
@Override
13+
public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
14+
JComponent component = (JComponent) super.getTableCellRendererComponent (table, value, isSelected, hasFocus, row, column);
15+
16+
// hides yellow selection highlight
17+
component.setBorder (BorderFactory.createEmptyBorder ());
18+
19+
this.setHorizontalAlignment (SwingConstants.CENTER);
20+
this.setVerticalAlignment (SwingConstants.CENTER);
21+
22+
return component;
23+
}
24+
}

0 commit comments

Comments
 (0)