Skip to content

Commit 1d27a62

Browse files
hansonrhansonr
authored andcommitted
Fixes AWT Label("") not still having height
1 parent 78ee834 commit 1d27a62

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

sources/net.sf.j2s.java.core/src/swingjs/a2s/Label.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
import javax.swing.JLabel;
66
import javax.swing.SwingConstants;
7+
import javax.swing.plaf.ComponentUI;
8+
9+
import swingjs.plaf.JSLabelUI;
710

811
public class Label extends JLabel {
912

@@ -17,10 +20,19 @@ public Label(String text) {
1720

1821
public Label(String text, int center) {
1922
super(text);
23+
/**
24+
* @j2sNative
25+
* this.getUI$().isAWT = true;
26+
*/
2027
super.setBackground(null);
2128
setAlignment(center);
2229
}
2330

31+
@Override
32+
public void setText(String text) {
33+
super.setText(text);
34+
}
35+
2436
@Override
2537
public void setBackground(Color c) {
2638
super.setBackground(c);

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,12 @@ public void setDraggable(JSFunction f) {
411411
*/
412412
protected int num;
413413

414+
/**
415+
* a flag to indicate that this is an AWT component
416+
*
417+
*/
418+
public boolean isAWT;
419+
414420
// /**
415421
// * a flag to indicate that it is not visible, but not according to Java
416422
// */
@@ -1450,9 +1456,12 @@ private Dimension getIconSize(AbstractButton b) {
14501456
}
14511457

14521458
private Dimension getTextSize(AbstractButton b) {
1453-
String t;
1454-
return (textNode == null || (t = b.getText()) == null
1455-
|| t == "" ? null : getHTMLSize(textNode));
1459+
if (textNode == null)
1460+
return null;
1461+
String t = b.getText();
1462+
if (isAWT && t == "")
1463+
t = "\u00A0"; // AWT labels do not hide if ""
1464+
return (t == null || t == "" ? null : getHTMLSize(textNode));
14561465
}
14571466

14581467
/**
@@ -2145,9 +2154,13 @@ protected void setIconAndText(String prop, Icon icon, int gap, String text) {
21452154
DOMNode.setStyles(iconNode, "height", iconHeight + "px", "width", icon.getIconWidth() + "px");
21462155
}
21472156
}
2148-
if (text == null || text.length() == 0) {
2157+
if (text == null) {
21492158
text = "";
2150-
} else {
2159+
} else if (text == "") {
2160+
if (isAWT)
2161+
text = "\u00A0"; // AWT labels do not hide if ""
2162+
}
2163+
if (text != "") {
21512164
if (text == "\0") {
21522165
isPaintedOnly = true; // this cannot be undone
21532166
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ public static Dimension getPreferredButtonSize(AbstractButton b, int textIconGap
272272
ui.setTainted();
273273
Icon icon = b.getIcon();
274274
String text = b.getText();
275+
if (ui.isAWT && text == "")
276+
text = "\u00A0"; // nonbreaking space for java.awt.Label
275277

276278
Font font = b.getFont();
277279
FontMetrics fm = b.getFontMetrics(font);

0 commit comments

Comments
 (0)