Skip to content

Commit eabd241

Browse files
committed
Allows for JLabel in JMenu
1 parent 3bda224 commit eabd241

File tree

9 files changed

+71
-41
lines changed

9 files changed

+71
-41
lines changed
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20230301203155
1+
20230304111725
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20230301203155
1+
20230304111725
Binary file not shown.

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

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -149,46 +149,17 @@ protected DOMNode createItem(String type, DOMNode buttonNode) {
149149
// separator masquerading as a menu item
150150
text = null;
151151
}
152-
itemNode = newDOMObject("li", id);
153-
if (text == null && icon == null)
154-
return itemNode;
155-
DOMNode.setStyle(itemNode, "outline", "none");
156-
menuAnchorNode = newDOMObject("div", id + "_a");// this needed? , "tabindex", "8");
157-
if (type != "_bar") {
158-
addClass(menuAnchorNode, "a");
159-
// DOMNode.setStyles(menuAnchorNode, "margin", "1px 2px 1px 2px", "height", "1em");
160-
}
161-
itemNode.appendChild(menuAnchorNode);
162-
setDoPropagate();
163-
if (buttonNode == null) {
164-
// not a radio or checkbox
165-
addCentering(menuAnchorNode);
166-
enableNode = itemNode;
167-
setIconAndText("btn", icon, gap, text);
168-
} else {
169-
menuAnchorNode.appendChild(buttonNode);
170-
setMenuItem(buttonNode);
171-
}
172-
setMenuItem(menuAnchorNode);
173-
setMenuItem();
152+
if (createItemNode(type, icon, gap, text, buttonNode)) {
174153
// j2sMenu.js will set the mouse-up event for the <a> tag with the
175154
// role=menuitem
176155
// attribute via j2sApplet.setMouse().
177156
// That event will then fire handleJSEvent
178157
setDataComponent(menuAnchorNode);
179158
setDataComponent(itemNode);
159+
}
180160
return itemNode;
181161
}
182162

183-
protected void setMenuItem() {
184-
setMenuItem(itemNode);
185-
setMenuItem(iconNode);
186-
if (actionNode != null && actionNode != iconNode)
187-
setMenuItem(actionNode);
188-
setMenuItem(textNode);
189-
setMenuItem(centeringNode);
190-
}
191-
192163
@SuppressWarnings("unused")
193164
@Override
194165
protected void enableNode(DOMNode node, boolean b) {

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

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,8 @@ protected void restoreCellNodes(DOMNode td) {
504504
*
505505
*/
506506
protected boolean isSimpleButton, isLabel;
507+
508+
protected boolean isMenuBarLabel;
507509

508510
protected int x, y;
509511

@@ -1734,7 +1736,7 @@ public Dimension getHTMLSize(DOMNode obj) {
17341736
}
17351737

17361738
private Dimension getIconSize(AbstractButton b) {
1737-
1739+
//
17381740
return (iconNode == null || imageNode == null || b.getIcon() == null ? null
17391741
: new Dimension(b.getIcon().getIconWidth(), b.getIcon().getIconHeight()));
17401742
}
@@ -3035,10 +3037,10 @@ protected void setAlignments(AbstractButton b, boolean justGetPreferred) {
30353037
itop = 70;
30363038
iscale = "scale(0.8,0.8)";
30373039
}
3038-
30393040
break;
30403041
}
30413042
if (itop >= 0) {
3043+
if (!isLabel || !isMenuBarLabel)
30423044
addJSKeyVal(cssTxt, "top", top + "%", "transform",
30433045
"translateY(" + (yoff == null ? "-" + top + "%" : yoff) + ")" + voff);
30443046
addJSKeyVal(cssIcon, "top", top + "%", "transform", "translateY(-" + itop + "%)" + voff + iscale);
@@ -3067,6 +3069,10 @@ protected void setAlignments(AbstractButton b, boolean justGetPreferred) {
30673069
}
30683070

30693071
private void setMenuAnchorAndAccelerator(AbstractButton b, int wCtr, boolean ltr, Insets margins) {
3072+
if (isLabel) {
3073+
// allowing label here -- not nec AbstractButton
3074+
return;
3075+
}
30703076
int wAccel = 0;
30713077
if (isMenu) {
30723078
// Correct for dimText calc losing position:absolute
@@ -3847,5 +3853,40 @@ public void setPasteHandler(JSFunction handler) {
38473853
}
38483854
}
38493855

3856+
public boolean createItemNode(String type, Icon icon, int gap, String text, DOMNode buttonNode) {
3857+
itemNode = newDOMObject("li", id);
3858+
if (text == null && icon == null)
3859+
return false;
3860+
DOMNode.setStyle(itemNode, "outline", "none");
3861+
menuAnchorNode = newDOMObject("div", id + "_a");// this needed? , "tabindex", "8");
3862+
if (type != "_bar") {
3863+
addClass(menuAnchorNode, "a");
3864+
// DOMNode.setStyles(menuAnchorNode, "margin", "1px 2px 1px 2px", "height", "1em");
3865+
}
3866+
itemNode.appendChild(menuAnchorNode);
3867+
setDoPropagate();
3868+
if (buttonNode == null) {
3869+
// not a radio or checkbox
3870+
addCentering(menuAnchorNode);
3871+
enableNode = itemNode;
3872+
setIconAndText("btn", icon, gap, text);
3873+
} else {
3874+
menuAnchorNode.appendChild(buttonNode);
3875+
setMenuItem(buttonNode);
3876+
}
3877+
setMenuItem(menuAnchorNode);
3878+
setMenuItem();
3879+
return true;
3880+
}
3881+
3882+
protected void setMenuItem() {
3883+
setMenuItem(itemNode);
3884+
setMenuItem(iconNode);
3885+
if (actionNode != null && actionNode != iconNode)
3886+
setMenuItem(actionNode);
3887+
setMenuItem(textNode);
3888+
setMenuItem(centeringNode);
3889+
}
3890+
38503891

38513892
}

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import javax.swing.AbstractButton;
1010
import javax.swing.JComponent;
1111
import javax.swing.JLabel;
12+
import javax.swing.JMenuBar;
13+
import javax.swing.JPopupMenu;
1214
import javax.swing.LookAndFeel;
1315

1416
import swingjs.api.js.DOMNode;
@@ -34,15 +36,26 @@ public JSLabelUI() {
3436
public DOMNode updateDOMNode() {
3537
// if (jc.getTopLevelAncestor() == null)
3638
// return domNode;
39+
isMenuItem = jc.getParent() instanceof JPopupMenu;
40+
isMenuBarLabel = jc.getParent() instanceof JMenuBar;
3741
if (domNode == null) {
38-
enableNode = domNode = newDOMObject("label", id);
39-
textNode = iconNode = null;
40-
addCentering(domNode);
42+
if (isMenuItem) {
43+
createItemNode("_item", icon, 4, label.getText(), null);
44+
domNode = itemNode;
45+
// TODO -- still not positioned correctly
46+
} else {
47+
enableNode = domNode = newDOMObject("label", id);
48+
textNode = iconNode = null;
49+
addCentering(domNode);
50+
}
4151
}
4252
getIconAndText(); // could be ToolTip
4353
setIconAndText("label", icon, gap, text);
44-
DOMNode.setStyles(domNode, "position", "absolute", "width", c.getWidth() + "px", "height",
45-
c.getHeight() + "px");
54+
if (isMenuItem) {
55+
} else {
56+
DOMNode.setStyles(domNode, "position", "absolute", "width", c.getWidth() + "px", "height",
57+
c.getHeight() + "px");
58+
}
4659
updateCenteringNode();
4760
if (allowTextAlignment) {
4861
// not for JToolTip

sources/net.sf.j2s.java.core/src/test/Test_Menu.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import javax.swing.JDesktopPane;
4343
import javax.swing.JFrame;
4444
import javax.swing.JInternalFrame;
45+
import javax.swing.JLabel;
4546
import javax.swing.JMenu;
4647
import javax.swing.JMenuBar;
4748
import javax.swing.JMenuItem;
@@ -87,7 +88,7 @@ public Test_Menu() {
8788
private JMenuBar getMenuBar() {
8889
JMenuBar mb = new JMenuBar();
8990
JMenu mb1 = new JMenu("Test1");
90-
JMenu mb2 = new JMenu("Test2");
91+
JMenu mb2 = new JMenu("Testing2");
9192
JMenuItem mb1a = new JMenuItem("test-1");
9293
mb1a.addActionListener((e)->{
9394
System.out.println("test-1 clicked");
@@ -128,6 +129,10 @@ public void menuCanceled(MenuEvent e) {
128129

129130
mb.add(mb1);
130131
mb.add(mb2);
132+
JLabel l = new JLabel("label1");
133+
mb.add(l);
134+
l = new JLabel("label2");
135+
mb1.add(l);
131136

132137
ActionListener al = new ActionListener() {
133138

0 commit comments

Comments
 (0)