With a little help from a StackOverflow thread we are able to get the frame for Processing’s default window (https://stackoverflow.com/questions/39107750/java-and-processing-3-0-frame-class-deprecated-is-there-an-alternative). When asked to println(frame) it’s reported to be a javax.swing.JFrame, but apparently it’s really a java.awt.Frame. Therefore we can add awt components to the window as shown below. I was expecting the menubar to show up on the window but it appeared on my Mac’s main menubar. The push button and text field work as expected as long as you use an ActionListener for event handling.
/*
Demonstrates menuBar, button, and text field components in default Processing window.
*/
import java.awt.*;
import java.awt.event.*;
java.awt.Frame frame;
java.awt.Canvas canvas;
void buildMenu() {
// Added to Mac's main menubar not to app's window
MenuBar menubar = new MenuBar();
Menu menu = new Menu("menu");
MenuItem item = new MenuItem("item");
menu.add(item);
menubar.add(menu);
frame.setMenuBar(menubar);
item.addActionListener(new ActionListener() {
void actionPerformed(ActionEvent actionEvent) {
println("menu item selected");
}
});
}
void buildWnd() {
// **** Text field **** //
TextField ef = new TextField("Enter text:");
ef.setBounds(50, 30, 280, 30);
frame.add(ef);
// **** Push button **** //
Button btn = new Button("Press me.");
btn.setBounds(100, 90, 100, 30);
frame.add(btn);
btn.addActionListener(new ActionListener() {
void actionPerformed(ActionEvent actionEvent) {
println("btn hit.");
ef.setText("Don't forget to check out the menubar.");
}
});
}
void setup() {
frame = ( (processing.awt.PSurfaceAWT.SmoothCanvas) ((processing.awt.PSurfaceAWT)surface).getNative()).getFrame();
println("frame = " + frame); // says it's a javax.swing.JFrame but apparently not true!
canvas = (processing.awt.PSurfaceAWT.SmoothCanvas) ((processing.awt.PSurfaceAWT)surface).getNative();
println("canvas = " + canvas);
frame.setBounds(500, 300, 400, 200);
// To avoid seeing canvas square set it to same size as frame
canvas.setBounds(500, 300, 400, 200);
buildMenu();
buildWnd();
}
void draw() {
}
Very helpful; thanks for looking at it. I tried changing the declaration, but didn’t get the casts made correctly, therefore made the erroneous assumption that it must not be a swing frame. I’ll try adding swing components next. I’m aware that the trend is to JavaFX, but unclear if Processing has made the switch.
Thank you for the reply. I’m aware of that line of code, but unfortunately it didn’t make a difference in this demo. I have put the menubar in a JFrame window using pure Java, so I know it works. As you point out, if we try and compete with the Processing runtime things aren’t always what they seem.
No change. Still doesn’t suppress it. I’m going to try JMenuBar now that I know frame is a swing frame and see if that makes a difference.
That is very debatable!
Java is not my native tongue and actually this is my second time around; first attempt was several years ago and the pundits were talking about it way back then. Apparently switch hasn’t occurred yet which is ok with me because most of the books that I have are for Swing and only one for FX.
Sorry, missed the obvious problem! Yes, that apple.laf.useScreenMenuBar property is specific to the Swing Apple look and feel. Glad you got it working.