Point of reference - I'm new to Java and new to Macs.
I'm coding up a Java Swing application... practicing, practicing, practicing.
I can add a JMenuBar to my JFrame, but....
Question 1: how do I add the JMenu items to the Mac OS menu bar (the topmost, forever present, menubar)?
Then, some other questions:
Question 2: Why doesn't a new instance of this app launch when I click File>New? Obvioulsy, "new RPM();" is not doing what I want it to.
Question 3: Why don't I get a visible separator bar in the File pulldown between OPEN and QUIT?
Here's the code I have so far. File name is RPM.java.
Thanks for any insight. Todd (OS X 10.4.8)
I'm coding up a Java Swing application... practicing, practicing, practicing.
I can add a JMenuBar to my JFrame, but....
Question 1: how do I add the JMenu items to the Mac OS menu bar (the topmost, forever present, menubar)?
Then, some other questions:
Question 2: Why doesn't a new instance of this app launch when I click File>New? Obvioulsy, "new RPM();" is not doing what I want it to.
Question 3: Why don't I get a visible separator bar in the File pulldown between OPEN and QUIT?
Here's the code I have so far. File name is RPM.java.
Thanks for any insight. Todd (OS X 10.4.8)
Code:
public class RPM extends javax.swing.JFrame implements
java.awt.event.ActionListener,
java.awt.event.ComponentListener,
java.awt.event.FocusListener {
javax.swing.JFrame frame ;
static final String HNS = "Hide & Seek" ;
public static void main(String[] args) {
RPM rpm = new RPM() ;
// Create a JFrame
javax.swing.JFrame f = new javax.swing.JFrame("Resize this frame!") ;
f.setSize(300,300) ;
f.setLocation(100,100) ;
rpm.frame = f ;
// Get the JFrame's container.
java.awt.Container content = f.getContentPane() ;
// Add a Single Menu Dropdown. Do the JMenuItems first.
// File
// - New
// - Open
// - Quit
javax.swing.JMenuItem mi_New = new javax.swing.JMenuItem("New") ;
javax.swing.JMenuItem mi_Open = new javax.swing.JMenuItem("Open") ;
javax.swing.JMenuItem mi_Quit = new javax.swing.JMenuItem("Quit") ;
// Define all the jmenuitem special behaviors
mi_New.setAccelerator(
javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.Event.META_MASK)) ;
mi_New.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) { new RPM() ; } } ) ;
mi_Open.setAccelerator(
javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.Event.META_MASK)) ;
mi_Open.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) { System.out.println("Open...") ; } } ) ;
mi_Quit.setAccelerator(
javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.Event.META_MASK)) ;
mi_Quit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) { System.exit(0) ; } } ) ;
// Add the File pulldown
javax.swing.JMenu jMenu = new javax.swing.JMenu("File") ;
jMenu.add(mi_New) ;
jMenu.add(mi_Open) ;
jMenu.addSeparator() ;
jMenu.add(mi_Quit) ;
// Get a JMenuBar, add out JMenu to it, and then ad it to the JFrame
javax.swing.JMenuBar jMenuBar = new javax.swing.JMenuBar() ;
jMenuBar.add(jMenu) ;
f.setJMenuBar(jMenuBar) ;
// Set the Layout Manager
content.setLayout(new java.awt.FlowLayout()) ;
// Add some widgets
content.add(new javax.swing.JLabel("Dumb Label")) ;
javax.swing.JButton myButton1 = new javax.swing.JButton("Mango") ;
javax.swing.JButton myButton2 = new javax.swing.JButton(HNS) ;
myButton1.addActionListener(rpm) ;
myButton2.addActionListener(rpm) ;
content.add(myButton1) ;
content.add(myButton2) ;
//* Add a JTextField
javax.swing.JTextField textField = new javax.swing.JTextField(20) ; // 20 columns wide
content.add(textField) ;
textField.addFocusListener(rpm) ;
//f.pack() ; // Collapse the window to the bare minimum size
f.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE) ; // Close app when RED X is clicked.
f.addComponentListener(rpm) ;
f.setVisible(true) ; // make window appear
}
public void actionPerformed(java.awt.event.ActionEvent ae) {
System.out.println("Button pressed...") ;
System.out.println("ae.getActionCommand()=" + ae.getActionCommand() ) ;
System.out.println("Modifiers are=" + ae.getModifiers() ) ;
if (ae.getActionCommand() == HNS) {
this.frame.setVisible(false) ; // Disappear for 1 second
try {
Thread.sleep(1000) ; // sleep for 1 second
}
catch (Exception e) {}
this.frame.setVisible(true) ; // Make the screen visible again.
}
}
public void componentHidden(java.awt.event.ComponentEvent ce) { // Fired when window is not visible
System.out.println("Component Event Fired = Hidden") ; // See "Hide & Seek" (HNS) button.
}
public void componentShown(java.awt.event.ComponentEvent ce) {
System.out.println("Component Event Fired = Shown") ;
}
public void componentMoved(java.awt.event.ComponentEvent ce) {
System.out.println("Component Event Fired = Moved") ;
}
public void componentResized(java.awt.event.ComponentEvent ce) {
System.out.println("Component Event Fired = Resized") ;
this.frame.setTitle("Width=" + this.frame.getWidth() + ", Height=" + this.frame.getHeight() ) ;
}
public void focusGained(java.awt.event.FocusEvent fe) { // Fires when the JTextField is clicked
System.out.println("Keyboard Focus: Gained") ;
}
public void focusLost(java.awt.event.FocusEvent fe) {
System.out.println("Keyboard Focus: Lost") ;
}
}