Skip to content

Commit 02d0e72

Browse files
committed
JComboBox, JLabel, JTextField
JSComboBoxUI fixes for font and background JSTextFieldUI preferred width miscalculated in HTML; use CSS "ch" option JSLabelUI fix for background when also overriding paintComponent
1 parent 0668e32 commit 02d0e72

File tree

5 files changed

+109
-72
lines changed

5 files changed

+109
-72
lines changed

sources/net.sf.j2s.java.core/src/swingjs/jquery/j2sComboBox.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ J2S.__makeComboBox = function() {
1717

1818
$( function() {
1919
$('head').append('<style>.j2scb-sel {background-color:#B8CFE5;}'
20-
+'\n.j2scb-unsel {background-color:white;}'
20+
+'\n.j2scb-unsel {background-color:inherit;}'
2121
+'\n.j2scb-hov {background-color:lightblue;}'
2222
+'\n.j2scbcont {position:absolute; left:0px;top:0px;}'
2323
+'\n.j2scbhead {position:absolute; left:0px;top:0px;text-align:left;overflow:hidden;padding:0px 2px 1px 2px}'
24-
+'\n.j2scbbtn {position:absolute; leftbackground-color:white;:100px;top:0px; width:20px;text-align:center;cursor:pointer;background-color:lightblue;padding:0px}'
24+
+'\n.j2scbbtn {position:absolute; leftbackground-color:inherit;:100px;top:0px; width:20px;text-align:center;cursor:pointer;background-color:lightblue;padding:0px}'
2525
+'\n.j2scbpopup {position:absolute; list-style:none}'
26-
+'\n.j2scblist {background-color:white;position:absolute; left:0px;top:0px;margin:0;border:black solid 1px;cursor:pointer;text-align:left;padding:0em;scrollbar-width:thin;cursor:pointer;}</style>'
26+
+'\n.j2scblist {background-color:inherit;position:absolute; left:0px;top:0px;margin:0;border:black solid 1px;cursor:pointer;text-align:left;padding:0em;scrollbar-width:thin;cursor:pointer;}</style>'
2727
);
2828

2929
var CLOSE_DELAY = 100; // BH 2019.10.04 50 was just a bit too fast; could close early
@@ -40,7 +40,7 @@ J2S.__makeComboBox = function() {
4040
disabled: false,
4141
popupVisible: false,
4242
selectedIndex: -1,
43-
backgroundColor: "white",
43+
backgroundColor: "blue",
4444
// z-index
4545
zIndex:999999,
4646
name:null,
@@ -185,10 +185,14 @@ J2S.__makeComboBox = function() {
185185
},
186186
updateCSS: function() {
187187
var cbox = this.cont.parent();
188+
var bg = cbox.css("background-color");
189+
this.options.backgroundColor = bg;
188190
var font = {"font-family": cbox.css("font-family")
189191
, "font-size": cbox.css("font-size")
190192
, "font-weight": cbox.css("font-weight")
191193
, "font-style": cbox.css("font-style")
194+
, "font-style": cbox.css("font-style")
195+
, backgroundColor: bg
192196
};
193197
var w = this.element.width();
194198
if (w == 0)
@@ -197,23 +201,23 @@ J2S.__makeComboBox = function() {
197201
this.cont.css({
198202
width: (w - 2) + 'px',
199203
height: h,
200-
backgroundColor: this.options.backgroundColor
204+
backgroundColor: bg
201205
});
202206
this.head.css({
203207
width: (w - 20) + 'px',
204-
height: h,
205-
backgroundColor: this.options.backgroundColor
208+
height: h
206209
});
207210
this.head.css(font);
208211
this.btn.css({
209212
left: (w - 20) + 'px',
210-
height: h
213+
height: h,
214+
backgroundColor: bg
211215
});
212216
h = (this.options.height ? this.options.height + 'px' : null);
213217
this.popup.css({
214218
width: w + 'px',
215219
height: h
216-
});
220+
});
217221
this.popup.css(font);
218222
this.list.css({
219223
width: w + 'px',

sources/net.sf.j2s.java.core/src/swingjs/plaf/JSComboBoxUI.java

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,12 @@ public DOMNode updateDOMNode() {
9898
if (domNode == null) {
9999
domNode = focusNode = newDOMObject("div", id);
100100
setDataShadowKeyComponent(domNode, listBox);
101+
101102
}
102103
addFocusHandler();
103104
setCssFont(domNode, c.getFont());
104105
popup.updateEnabled();
106+
setBackgroundImpl(jc.isOpaque() ? getBackground() : null);
105107
return updateDOMNodeCUI();
106108
}
107109

@@ -261,7 +263,7 @@ protected void installDefaults() {
261263
// NOTE: this needs to default to true if not specified
262264
Boolean b = (Boolean) UIManager.get("ComboBox.squareButton");
263265
squareButton = b == null ? true : b;
264-
266+
265267
padding = UIManager.getInsets("ComboBox.padding");
266268
}
267269

@@ -1745,8 +1747,10 @@ public void itemStateChanged(ItemEvent e) {
17451747
//
17461748
@Override
17471749
public void propertyChange(PropertyChangeEvent e) {
1750+
Component editor = JSComboBoxUI.this.editor;
17481751
String propertyName = e.getPropertyName();
1749-
//System.out.println("handler prop " + propertyName);
1752+
JSComboPopupList popup = JSComboBoxUI.this.popup;
1753+
// System.out.println("handler prop " + propertyName);
17501754
if (e.getSource() == editor) {
17511755
// If the border of the editor changes then this can effect
17521756
// the size of the editor which can cause the combo's size to
@@ -1758,7 +1762,8 @@ public void propertyChange(PropertyChangeEvent e) {
17581762
}
17591763
} else {
17601764
JComboBox comboBox = (JComboBox) e.getSource();
1761-
if (propertyName == "model") {
1765+
switch (propertyName) {
1766+
case "model":
17621767
ComboBoxModel newModel = (ComboBoxModel) e.getNewValue();
17631768
ComboBoxModel oldModel = (ComboBoxModel) e.getOldValue();
17641769

@@ -1783,10 +1788,14 @@ public void propertyChange(PropertyChangeEvent e) {
17831788
popup.updateSelectedIndex();
17841789
popup.updateCSS();
17851790
repaint();
1786-
} else if (propertyName == "editor" && comboBox.isEditable()) {
1787-
addEditor();
1788-
comboBox.revalidate();
1789-
} else if (propertyName == "editable") {
1791+
break;
1792+
case "editor":
1793+
if (comboBox.isEditable()) {
1794+
addEditor();
1795+
comboBox.revalidate();
1796+
}
1797+
break;
1798+
case "editable":
17901799
if (comboBox.isEditable()) {
17911800
comboBox.setRequestFocusEnabled(false);
17921801
addEditor();
@@ -1796,47 +1805,74 @@ public void propertyChange(PropertyChangeEvent e) {
17961805
}
17971806
updateToolTipTextForChildren();
17981807
comboBox.revalidate();
1799-
} else if (propertyName == "enabled") {
1808+
break;
1809+
case "enabled":
18001810
boolean enabled = comboBox.isEnabled();
18011811
if (editor != null)
18021812
editor.setEnabled(enabled);
18031813
// if (arrowButton != null)
18041814
// arrowButton.setEnabled(enabled);
18051815
repaint();
1806-
} else if (propertyName == "focusable") {
1816+
break;
1817+
case "focusable":
18071818
boolean focusable = comboBox.isFocusable();
18081819
if (editor != null)
18091820
editor.setFocusable(focusable);
18101821
// if (arrowButton != null)
18111822
// arrowButton.setFocusable(focusable);
18121823
repaint();
1813-
} else if (propertyName == "maximumRowCount") {
1824+
break;
1825+
case "maximumRowCount":
18141826
if (isPopupVisible(comboBox)) {
18151827
setPopupVisible(comboBox, false);
18161828
setPopupVisible(comboBox, true);
18171829
}
1818-
} else if (propertyName == "font") {
1830+
break;
1831+
case "background":
1832+
Color c = comboBox.getBackground();
1833+
listBox.setBackground(c);
1834+
if (editor != null) {
1835+
editor.setBackground(c);
1836+
}
1837+
popup.updateCSS();
1838+
break;
1839+
case "foreground":
18191840
listBox.setFont(comboBox.getFont());
18201841
if (editor != null) {
18211842
editor.setFont(comboBox.getFont());
18221843
}
18231844
isMinimumSizeDirty = true;
18241845
isDisplaySizeDirty = true;
1846+
popup.updateCSS();
1847+
break;
1848+
case "font":
1849+
listBox.setFont(comboBox.getFont());
1850+
if (editor != null) {
1851+
editor.setFont(comboBox.getFont());
1852+
}
1853+
isMinimumSizeDirty = true;
1854+
isDisplaySizeDirty = true;
1855+
popup.updateCSS();
18251856
comboBox.validate();
1826-
} else if (propertyName == JComponent.TOOL_TIP_TEXT_KEY) {
1857+
break;
1858+
case JComponent.TOOL_TIP_TEXT_KEY:
18271859
updateToolTipTextForChildren();
1828-
} else if (propertyName == JSComboBoxUI.IS_TABLE_CELL_EDITOR) {
1860+
break;
1861+
case JSComboBoxUI.IS_TABLE_CELL_EDITOR:
18291862
Boolean inTable = (Boolean) e.getNewValue();
18301863
isTableCellEditor = inTable.equals(Boolean.TRUE) ? true : false;
1831-
} else if (propertyName == "prototypeDisplayValue") {
1864+
break;
1865+
case "prototypeDisplayValue":
18321866
isMinimumSizeDirty = true;
18331867
isDisplaySizeDirty = true;
18341868
comboBox.revalidate();
1835-
} else if (propertyName == "renderer") {
1869+
break;
1870+
case "renderer":
18361871
isMinimumSizeDirty = true;
18371872
isDisplaySizeDirty = true;
18381873
popup.updateList();
18391874
comboBox.revalidate();
1875+
break;
18401876
}
18411877
}
18421878
}

sources/net.sf.j2s.java.core/src/swingjs/plaf/JSComboPopupList.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,9 @@ void updateText() {
141141

142142
void updateCSS() {
143143
DOMNode.setSize(cbui.domNode, cbui.width, cbui.height);
144-
if (j2scb != null)
144+
if (j2scb != null) {
145145
j2scb.j2sCB("updateCSS");
146+
}
146147
}
147148

148149
void updateList() {

sources/net.sf.j2s.java.core/src/swingjs/plaf/JSComponentUI.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3736,7 +3736,11 @@ public void paintBackground(JSGraphics2D g) {
37363736
if (!paintsSelf)
37373737
setBackgroundDOM(domNode, color);
37383738
// preliminary -- DOM only, when the background is set
3739-
} else if (allowPaintedBackground && (isOpaque && (isPanel || cellComponent != null || jc.getComponentCount() > 0) || jc.秘g != null)) {
3739+
} else if (allowPaintedBackground && (isOpaque && (isPanel
3740+
|| isLabel && paintsSelf // overridden paintComponent
3741+
|| cellComponent != null
3742+
|| jc.getComponentCount() > 0)
3743+
|| jc.秘g != null)) {
37403744
// all opaque components must paint their background
37413745
// just in case they have painted CHILDREN or if they are table cell components
37423746
if (isOpaque == (color.getAlpha() == 255)) {

sources/net.sf.j2s.java.core/src/swingjs/plaf/JSSpinnerUI.java

Lines changed: 38 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@
5656
*/
5757
public class JSSpinnerUI extends JSPanelUI {
5858

59-
private DOMNode up, dn;
60-
6159
@Override
6260
public DOMNode updateDOMNode() {
6361
spinner = (JSpinner) jc;
@@ -855,60 +853,54 @@ public void propertyChange(PropertyChangeEvent e) {
855853
JSSpinnerUI ui = this;
856854
if (e.getSource() instanceof JSpinner) {
857855
JSpinner spinner = (JSpinner) (e.getSource());
858-
// JSSpinnerUI spinnerUI = spinner.getUI();
859-
860-
// if (spinnerUI instanceof JSSpinnerUI) {
861-
// JSSpinnerUI ui = (JSSpinnerUI) spinnerUI;
862-
863-
if ("editor".equals(prop)) {
864-
JComponent oldEditor = (JComponent) e.getOldValue();
865-
JComponent newEditor = (JComponent) e.getNewValue();
866-
ui.replaceEditor(oldEditor, newEditor);
867-
ui.updateEnabledState();
868-
if (oldEditor instanceof JSpinner.DefaultEditor) {
869-
JTextField tf = ((JSpinner.DefaultEditor) oldEditor).getTextField();
870-
if (tf != null) {
871-
tf.removeFocusListener(nextButtonHandler);
872-
tf.removeFocusListener(previousButtonHandler);
873-
}
856+
switch (prop) {
857+
case "editor":
858+
JComponent oldEditor = (JComponent) e.getOldValue();
859+
JComponent newEditor = (JComponent) e.getNewValue();
860+
ui.replaceEditor(oldEditor, newEditor);
861+
ui.updateEnabledState();
862+
if (oldEditor instanceof JSpinner.DefaultEditor) {
863+
JTextField tf = ((JSpinner.DefaultEditor) oldEditor).getTextField();
864+
if (tf != null) {
865+
tf.removeFocusListener(nextButtonHandler);
866+
tf.removeFocusListener(previousButtonHandler);
874867
}
875-
if (newEditor instanceof JSpinner.DefaultEditor) {
876-
JTextField tf = ((JSpinner.DefaultEditor) newEditor).getTextField();
877-
if (tf != null) {
878-
if (tf.getFont() instanceof UIResource) {
879-
tf.setFont(spinner.getFont());
880-
}
881-
tf.addFocusListener(nextButtonHandler);
882-
tf.addFocusListener(previousButtonHandler);
868+
}
869+
if (newEditor instanceof JSpinner.DefaultEditor) {
870+
JTextField tf = ((JSpinner.DefaultEditor) newEditor).getTextField();
871+
if (tf != null) {
872+
if (tf.getFont() instanceof UIResource) {
873+
tf.setFont(spinner.getFont());
883874
}
875+
tf.addFocusListener(nextButtonHandler);
876+
tf.addFocusListener(previousButtonHandler);
884877
}
885-
} else if ("enabled".equals(prop) || "model".equals(prop)) {
886-
ui.updateEnabledState();
887-
} else if ("font".equals(prop)) {
888-
JComponent editor = spinner.getEditor();
889-
if (editor != null && editor instanceof JSpinner.DefaultEditor) {
890-
JTextField tf = ((JSpinner.DefaultEditor) editor).getTextField();
891-
if (tf != null) {
892-
if (tf.getFont() instanceof UIResource) {
893-
tf.setFont(spinner.getFont());
894-
}
878+
}
879+
break;
880+
case "enabled":
881+
case "model":
882+
ui.updateEnabledState();
883+
break;
884+
case "font":
885+
JComponent editor = spinner.getEditor();
886+
if (editor != null && editor instanceof JSpinner.DefaultEditor) {
887+
JTextField tf = ((JSpinner.DefaultEditor) editor).getTextField();
888+
if (tf != null) {
889+
if (tf.getFont() instanceof UIResource) {
890+
tf.setFont(spinner.getFont());
895891
}
896892
}
897-
} else if (JComponent.TOOL_TIP_TEXT_KEY.equals(prop)) {
898-
updateToolTipTextForChildren(spinner);
899893
}
900-
// }
894+
break;
895+
case JComponent.TOOL_TIP_TEXT_KEY:
896+
updateToolTipTextForChildren(spinner);
897+
break;
898+
}
901899
} else if (e.getSource() instanceof JComponent) {
902900
JComponent c = (JComponent) e.getSource();
903901
if ((c.getParent() instanceof JPanel) && (c.getParent().getParent() instanceof JSpinner)
904902
&& "border".equals(prop)) {
905-
906-
JSpinner spinner = (JSpinner) c.getParent().getParent();
907-
// SpinnerUI spinnerUI = spinner.getUI();
908-
// if (spinnerUI instanceof JSSpinnerUI) {
909-
// JSSpinnerUI ui = (JSSpinnerUI) spinnerUI;
910-
ui.maybeRemoveEditorBorder(c);
911-
// }
903+
ui.maybeRemoveEditorBorder(c);
912904
}
913905
}
914906
super.propertyChange(e);

0 commit comments

Comments
 (0)