Skip to content

Commit 1af7bd8

Browse files
authored
Merge pull request #67 from vincenzopalazzo/mousehovereffect
Fixing the issue and added the support JOptionPane
2 parents 7793a77 + c43a954 commit 1af7bd8

17 files changed

+258
-53
lines changed

src/main/java/MaterialUISwingDemo.java

+101-33
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,12 @@
33
import mdlaf.utils.MaterialColors;
44
import org.jdesktop.swingx.JXTaskPane;
55

6-
import javax.swing.AbstractAction;
7-
import javax.swing.Action;
8-
import javax.swing.JButton;
9-
import javax.swing.JCheckBox;
10-
import javax.swing.JCheckBoxMenuItem;
11-
import javax.swing.JColorChooser;
12-
import javax.swing.JComboBox;
13-
import javax.swing.JDialog;
14-
import javax.swing.JEditorPane;
15-
import javax.swing.JFileChooser;
16-
import javax.swing.JFrame;
17-
import javax.swing.JLabel;
18-
import javax.swing.JMenu;
19-
import javax.swing.JMenuBar;
20-
import javax.swing.JMenuItem;
21-
import javax.swing.JPanel;
22-
import javax.swing.JPasswordField;
23-
import javax.swing.JProgressBar;
24-
import javax.swing.JRadioButton;
25-
import javax.swing.JRadioButtonMenuItem;
26-
import javax.swing.JScrollPane;
27-
import javax.swing.JSlider;
28-
import javax.swing.JSpinner;
29-
import javax.swing.JTabbedPane;
30-
import javax.swing.JTable;
31-
import javax.swing.JTextField;
32-
import javax.swing.JTextPane;
33-
import javax.swing.JToggleButton;
34-
import javax.swing.JToolBar;
35-
import javax.swing.JTree;
36-
import javax.swing.SpinnerListModel;
37-
import javax.swing.UIManager;
38-
import javax.swing.UnsupportedLookAndFeelException;
6+
import javax.swing.*;
397
import java.awt.BorderLayout;
408
import java.awt.Color;
419
import java.awt.Dimension;
4210
import java.awt.event.ActionEvent;
11+
import java.util.ArrayList;
4312

4413
public class MaterialUISwingDemo {
4514

@@ -210,6 +179,105 @@ public void actionPerformed (ActionEvent e) {
210179
testButtonHoverTwo.addMouseListener(MaterialUIMovement.getMovement(testButtonHoverTwo, MaterialColors.LIGHT_BLUE_200));
211180
pn.add(testButtonHoverTwo);
212181

182+
JPanel panel3 = new JPanel();
183+
tp.addTab("Panel 3", panel3);
184+
185+
JButton buttonInfo = new JButton("Test INFO");
186+
buttonInfo.setBackground(MaterialColors.LIGHT_BLUE_500);
187+
buttonInfo.addMouseListener(MaterialUIMovement.getMovement(buttonInfo, MaterialColors.LIGHT_BLUE_200));
188+
class InfoMessage extends AbstractAction{
189+
190+
public InfoMessage() {
191+
putValue(Action.NAME, "Info option pane");
192+
}
193+
194+
@Override
195+
public void actionPerformed(ActionEvent e) {
196+
197+
JOptionPane optionPane = new JOptionPane();
198+
optionPane.showMessageDialog(frame, "This is message info", "Message info", JOptionPane.INFORMATION_MESSAGE);
199+
}
200+
}
201+
buttonInfo.setAction(new InfoMessage());
202+
203+
panel3.add(buttonInfo);
204+
205+
JButton buttonError = new JButton();
206+
buttonError.setBackground(MaterialColors.RED_800);
207+
buttonError.addMouseListener(MaterialUIMovement.getMovement(buttonError, MaterialColors.RED_400));
208+
class ErrorMassage extends AbstractAction{
209+
210+
public ErrorMassage() {
211+
putValue(Action.NAME, "Error option pane");
212+
}
213+
214+
@Override
215+
public void actionPerformed(ActionEvent e) {
216+
217+
JOptionPane optionPane = new JOptionPane();
218+
optionPane.showMessageDialog(frame, "This is message error", "Message error", JOptionPane.ERROR_MESSAGE);
219+
}
220+
}
221+
buttonError.setAction(new ErrorMassage());
222+
223+
panel3.add(buttonError);
224+
225+
JButton buttonQuestion = new JButton();
226+
buttonQuestion.setBackground(MaterialColors.BLUE_600);
227+
buttonQuestion.addMouseListener(MaterialUIMovement.getMovement(buttonQuestion, MaterialColors.BLUE_300));
228+
class QuesuionMessage extends AbstractAction{
229+
230+
public QuesuionMessage() {
231+
putValue(Action.NAME, "Info question pane");
232+
}
233+
234+
@Override
235+
public void actionPerformed(ActionEvent e) {
236+
237+
JOptionPane optionPane = new JOptionPane();
238+
optionPane.showMessageDialog(frame, "This is message question", "Message question", JOptionPane.QUESTION_MESSAGE);
239+
}
240+
}
241+
242+
buttonQuestion.setAction(new QuesuionMessage());
243+
244+
JButton buttonWarning = new JButton();
245+
buttonWarning.setBackground(MaterialColors.YELLOW_800);
246+
buttonWarning.addMouseListener(MaterialUIMovement.getMovement(buttonWarning, MaterialColors.YELLOW_500));
247+
class WarningMessage extends AbstractAction {
248+
249+
public WarningMessage() {
250+
putValue(Action.NAME, "Info warning pane");
251+
}
252+
253+
@Override
254+
public void actionPerformed(ActionEvent e) {
255+
256+
JOptionPane optionPane = new JOptionPane();
257+
optionPane.showMessageDialog(frame, "This is message warning", "Message warning", JOptionPane.WARNING_MESSAGE);
258+
}
259+
260+
}
261+
262+
buttonWarning.setAction(new WarningMessage());
263+
264+
panel3.add(buttonQuestion);
265+
panel3.add(buttonWarning);
266+
267+
JSpinner spinnerDate = new JSpinner(new SpinnerDateModel());
268+
269+
JSpinner spinnerNumbar = new JSpinner(new SpinnerNumberModel());
270+
271+
ArrayList<Object> objectList = new ArrayList<>();
272+
objectList.add(new Object());
273+
objectList.add(new Object());
274+
objectList.add(new Object());
275+
JSpinner spinnerList = new JSpinner(new SpinnerListModel(objectList));
276+
277+
panel3.add(spinnerDate);
278+
panel3.add(spinnerNumbar);
279+
panel3.add(spinnerList);
280+
213281
// make everything visible to the world
214282
frame.pack ();
215283
frame.setVisible (true);

src/main/java/mdlaf/MaterialLookAndFeel.java

+22
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import mdlaf.components.checkboxmenuitem.MaterialCheckBoxMenuItemUI;
66
import mdlaf.components.combobox.MaterialComboBoxUI;
77
import mdlaf.components.filechooser.MaterialFileChooserUI;
8+
import mdlaf.components.formattertextfield.MaterialFormattedTextFieldUI;
89
import mdlaf.components.label.MaterialLabelUI;
910
import mdlaf.components.menu.MaterialMenuUI;
1011
import mdlaf.components.menubar.MaterialMenuBarUI;
1112
import mdlaf.components.menuitem.MaterialMenuItemUI;
13+
import mdlaf.components.optionpane.MaterialOptionPaneUI;
1214
import mdlaf.components.panel.MaterialPanelUI;
1315
import mdlaf.components.password.MaterialPasswordFieldUI;
1416
import mdlaf.components.popupmenu.MaterialPopupMenuUI;
@@ -73,6 +75,8 @@ public class MaterialLookAndFeel extends MetalLookAndFeel {
7375
private static final String fileChooserUI = MaterialFileChooserUI.class.getCanonicalName ();
7476
private static final String toolTipUI = MaterialToolTipUI.class.getCanonicalName ();
7577
private static final String taskPaneUI = MaterialTaskPaneUI.class.getCanonicalName ();
78+
private static final String optionPaneUI = MaterialOptionPaneUI.class.getCanonicalName();
79+
private static final String formattedTextFieldUI = MaterialFormattedTextFieldUI.class.getCanonicalName();
7680

7781
@Override
7882
public String getName () {
@@ -132,6 +136,8 @@ protected void initClassDefaults (UIDefaults table) {
132136
table.put ("FileChooserUI", fileChooserUI);
133137
table.put ("ToolTipUI", toolTipUI);
134138
table.put ("swingx/TaskPaneUI", taskPaneUI);
139+
table.put("OptionPaneUI", optionPaneUI);
140+
table.put("FormattedTextFieldUI", formattedTextFieldUI);
135141
}
136142

137143
@Override
@@ -161,6 +167,8 @@ protected void initComponentDefaults (UIDefaults table) {
161167
table.put ("ComboBox.selectionBackground", Color.WHITE);
162168
table.put ("ComboBox.selectionForeground", Color.BLACK);
163169
table.put ("ComboBox.selectedInDropDownBackground", MaterialColors.GRAY_200);
170+
table.put("ComboBox.mouseHoverColor", MaterialColors.GRAY_400);
171+
table.put("ComboBox.mouseHoverEnabled", true);
164172

165173
table.put ("Label.font", MaterialFonts.REGULAR);
166174
table.put ("Label.background", Color.WHITE);
@@ -214,6 +222,8 @@ protected void initComponentDefaults (UIDefaults table) {
214222
table.put ("Spinner.border", MaterialBorders.LIGHT_LINE_BORDER);
215223
table.put ("Spinner.arrowButtonBackground", MaterialColors.GRAY_200);
216224
table.put ("Spinner.arrowButtonBorder", BorderFactory.createEmptyBorder ());
225+
table.put("Spinner.mouseHoverEnabled", true);
226+
table.put("Spinner.mouseHoverColor", MaterialColors.GRAY_400);
217227

218228
table.put ("ScrollBar.font", MaterialFonts.REGULAR);
219229
table.put ("ScrollBar.track", MaterialColors.GRAY_200);
@@ -266,6 +276,7 @@ protected void initComponentDefaults (UIDefaults table) {
266276
table.put ("TextField.inactiveBackground", MaterialColors.GRAY_200);
267277
table.put ("TextField.selectionBackground", MaterialColors.LIGHT_BLUE_400);
268278
table.put ("TextField.selectionForeground", Color.BLACK);
279+
table.put ("TextField.border", BorderFactory.createEmptyBorder(3, 5, 2, 5));
269280

270281
table.put ("ToggleButton.border", BorderFactory.createEmptyBorder ());
271282
table.put ("ToggleButton.font", MaterialFonts.REGULAR);
@@ -344,5 +355,16 @@ protected void initComponentDefaults (UIDefaults table) {
344355
table.put("TaskPane.contentBackground", MaterialColors.GRAY_50);
345356
table.put("TaskPane.yesCollassed", new ImageIcon(MaterialImages.YES_COLLASSED));
346357
table.put("TaskPane.noCollassed", new ImageIcon(MaterialImages.NO_COLLASSED));
358+
359+
table.put("OptionPaneUI.warningIcon", new ImageIcon(MaterialImages.WARNING));
360+
table.put("OptionPaneUI.errorIcon", new ImageIcon(MaterialImages.ERROR));
361+
table.put("OptionPaneUI.questionIcon", new ImageIcon(MaterialImages.QUESTION));
362+
table.put("OptionPaneUI.informationIcon", new ImageIcon(MaterialImages.INFORMATION));
363+
364+
table.put ("FormattedTextField.inactiveForeground", MaterialColors.GRAY_800);
365+
table.put ("FormattedTextField.inactiveBackground", MaterialColors.GRAY_200);
366+
table.put ("FormattedTextField.selectionBackground", MaterialColors.LIGHT_BLUE_400);
367+
table.put ("FormattedTextField.selectionForeground", Color.BLACK);
368+
table.put("FormattedTextField.border", BorderFactory.createEmptyBorder(3, 5, 2, 5));
347369
}
348370
}

src/main/java/mdlaf/components/combobox/MaterialComboBoxUI.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package mdlaf.components.combobox;
22

3+
import mdlaf.animation.MaterialUIMovement;
34
import mdlaf.utils.MaterialDrawingUtils;
45

56
import javax.swing.BorderFactory;
@@ -47,7 +48,9 @@ protected JButton createArrowButton () {
4748
button.setOpaque (true);
4849
button.setBackground (UIManager.getColor ("ComboBox.buttonBackground"));
4950
button.setBorder (BorderFactory.createEmptyBorder ());
50-
51+
if(UIManager.getBoolean("ComboBox.mouseHoverEnabled")){
52+
button.addMouseListener(MaterialUIMovement.getMovement(button, UIManager.getColor ("ComboBox.mouseHoverColor")));
53+
}
5154
return button;
5255
}
5356

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* MIT License
3+
*
4+
* Copyright (c) 2017 atharva washimkar
5+
* Copyright (c) 2018 Vincent Palazzo
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
package mdlaf.components.formattertextfield;
26+
27+
import mdlaf.utils.MaterialDrawingUtils;
28+
29+
import javax.swing.*;
30+
import javax.swing.plaf.ComponentUI;
31+
import javax.swing.plaf.basic.BasicFormattedTextFieldUI;
32+
import java.awt.*;
33+
34+
/**
35+
* @author https://github.com/vincenzopalazzo
36+
*/
37+
public class MaterialFormattedTextFieldUI extends BasicFormattedTextFieldUI {
38+
39+
40+
@SuppressWarnings({"MethodOverridesStaticMethodOfSuperclass", "UnusedDeclaration"})
41+
public static ComponentUI createUI(JComponent c) {
42+
return new MaterialFormattedTextFieldUI();
43+
}
44+
45+
@Override
46+
public void installUI(JComponent c) {
47+
super.installUI(c);
48+
JFormattedTextField formattedTextField = (JFormattedTextField) c;
49+
formattedTextField.setSelectionColor(UIManager.getColor("FormattedTextField.selectionBackground"));
50+
formattedTextField.setSelectedTextColor(UIManager.getColor("FormattedTextField.selectionForeground"));
51+
formattedTextField.setBackground(UIManager.getColor("FormattedTextField.inactiveBackground"));
52+
formattedTextField.setForeground(UIManager.getColor("FormattedTextField.inactiveForeground"));
53+
formattedTextField.setFont(UIManager.getFont("FormattedTextField.font"));
54+
formattedTextField.setBorder(UIManager.getBorder("FormattedTextField.border"));
55+
}
56+
57+
@Override
58+
protected void paintBackground(Graphics g) {
59+
super.paintBackground(MaterialDrawingUtils.getAliasedGraphics(g));
60+
}
61+
62+
@Override
63+
public void update(Graphics g, JComponent c) {
64+
super.update(MaterialDrawingUtils.getAliasedGraphics(g), c);
65+
}
66+
67+
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package mdlaf.components.optionpane;
2+
3+
4+
import javax.swing.*;
5+
import javax.swing.plaf.ComponentUI;
6+
import javax.swing.plaf.basic.BasicOptionPaneUI;
7+
import java.awt.*;
8+
9+
/**
10+
* @author https://github.com/vincenzopalazzo
11+
*/
12+
public class MaterialOptionPaneUI extends BasicOptionPaneUI {
13+
14+
15+
@SuppressWarnings({"MethodOverridesStaticMethodOfSuperclass", "UnusedDeclaration"})
16+
public static ComponentUI createUI(JComponent c) {
17+
return new MaterialOptionPaneUI();
18+
}
19+
20+
21+
@Override
22+
public void update(Graphics g, JComponent c) {
23+
super.update(g, c);
24+
}
25+
26+
@Override
27+
protected Icon getIconForType(int messageType) {
28+
if(messageType == JOptionPane.ERROR_MESSAGE){
29+
return UIManager.getIcon("OptionPaneUI.errorIcon");
30+
}
31+
if(messageType == JOptionPane.INFORMATION_MESSAGE){
32+
return UIManager.getIcon("OptionPaneUI.informationIcon");
33+
}
34+
if(messageType == JOptionPane.QUESTION_MESSAGE){
35+
return UIManager.getIcon("OptionPaneUI.questionIcon");
36+
}
37+
if(messageType == JOptionPane.WARNING_MESSAGE){
38+
return UIManager.getIcon("OptionPaneUI.warningIcon");
39+
}
40+
return super.getIconForType(messageType);
41+
}
42+
}

src/main/java/mdlaf/components/password/MaterialPasswordFieldUI.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void installUI (JComponent c) {
4444

4545
JPasswordField passwordField = (JPasswordField) c;
4646
passwordField.setOpaque (false);
47-
passwordField.setBorder (BorderFactory.createEmptyBorder (5, 2, 10, 0));
47+
passwordField.setBorder (BorderFactory.createEmptyBorder (5, 2, 2, 0));
4848
passwordField.setBackground (MaterialColors.LIGHT_BLUE_400);
4949

5050
this.focusedBackground = passwordField.getBackground ();
@@ -133,7 +133,7 @@ public void paintSafely (Graphics g) {
133133
int w = getComponent ().getWidth () - getComponent ().getInsets ().left - getComponent ().getInsets ().right;
134134

135135
g.setColor (c.getBackground ());
136-
g.fillRect (x, c.getHeight () - y, w, 2);
136+
g.fillRect (x, c.getHeight () - y + 1, w, 1);
137137

138138
super.paintSafely (g);
139139
}

0 commit comments

Comments
 (0)