Instantly share code, notes, and snippets.
Last active
December 11, 2015 11:48
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save mchirico/4596582 to your computer and use it in GitHub Desktop.
Oracle's menu demo with a few events. The trail can be found here: http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html
Make sure you use frame.validate() to correctly display the frame.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dev.sysadmin.test; | |
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.JButton; | |
import javax.swing.JMenu; | |
import javax.swing.JMenuItem; | |
import javax.swing.JCheckBoxMenuItem; | |
import javax.swing.JRadioButtonMenuItem; | |
import javax.swing.ButtonGroup; | |
import javax.swing.JMenuBar; | |
import javax.swing.KeyStroke; | |
import javax.swing.ImageIcon; | |
import javax.swing.ScrollPaneConstants; | |
import javax.swing.JPanel; | |
import javax.swing.JTextArea; | |
import javax.swing.JScrollPane; | |
import javax.swing.JFrame; | |
/* MenuLookDemo.java requires images/middle.gif. */ | |
/* | |
* This class exists solely to show you what menus look like. | |
* It has no menu-related event handling. | |
*/ | |
public class MenuLookDemo implements ActionListener { | |
private JFrame frame; | |
private JTextArea output = null; | |
private JScrollPane scrollPane = null; | |
private JTextArea output0 = null; | |
private JScrollPane scrollPane0 = null; | |
private JTextArea output1 = null; | |
private JScrollPane scrollPane1 = null; | |
private JPanel p0; | |
private JPanel p1; | |
public MenuLookDemo() { | |
} | |
public void setP0(JPanel jpanel) { | |
this.p0 = jpanel; | |
} | |
public JPanel getP0() { | |
this.output = this.output0; | |
this.scrollPane = this.scrollPane0; | |
return this.p0; | |
} | |
public void setP1(JPanel jpanel) { | |
this.p1 = jpanel; | |
} | |
public JPanel getP1() { | |
this.output = this.output1; | |
this.scrollPane = this.scrollPane1; | |
return this.p1; | |
} | |
public JMenuBar createMenuBar() { | |
JMenuBar menuBar; | |
JMenu menu, submenu, submenu2; | |
JMenuItem menuItem; | |
JRadioButtonMenuItem rbMenuItem; | |
JCheckBoxMenuItem cbMenuItem; | |
// Create the menu bar. | |
menuBar = new JMenuBar(); | |
// Build the first menu. | |
menu = new JMenu("Menu0"); | |
menu.setMnemonic(KeyEvent.VK_A); | |
menu.getAccessibleContext().setAccessibleDescription( | |
"First Menu top left"); | |
menu.setActionCommand("Menu0"); | |
menuBar.add(menu); | |
// a group of JMenuItems | |
menuItem = new JMenuItem("A text-only menu item", KeyEvent.VK_T); | |
// menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead | |
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, | |
ActionEvent.ALT_MASK)); | |
menuItem.getAccessibleContext().setAccessibleDescription( | |
"This doesn't really do anything"); | |
// Added this | |
menuItem.setActionCommand("MenuItem01"); | |
menuItem.addActionListener(this); | |
menu.add(menuItem); | |
ImageIcon icon = createImageIcon("images/middle.gif"); | |
menuItem = new JMenuItem("Both text and icon", icon); | |
menuItem.setActionCommand("MenuItem02"); | |
menuItem.addActionListener(this); | |
menuItem.setMnemonic(KeyEvent.VK_B); | |
menu.add(menuItem); | |
// NEW | |
menu.addSeparator(); | |
menuItem = new JMenuItem("Repaint"); | |
menuItem.setActionCommand("Repaint"); | |
menuItem.addActionListener(this); | |
menu.add(menuItem); | |
menuItem = new JMenuItem("Two"); | |
menuItem.setActionCommand("Two"); | |
menuItem.addActionListener(this); | |
menuItem.setMnemonic(KeyEvent.VK_D); | |
menu.add(menuItem); | |
// a group of radio button menu items | |
menu.addSeparator(); | |
ButtonGroup group = new ButtonGroup(); | |
rbMenuItem = new JRadioButtonMenuItem("A radio button menu item"); | |
rbMenuItem.setSelected(true); | |
rbMenuItem.setMnemonic(KeyEvent.VK_R); | |
// Added these two | |
rbMenuItem.setActionCommand("rbMenuItem"); | |
rbMenuItem.addActionListener(this); | |
group.add(rbMenuItem); | |
menu.add(rbMenuItem); | |
rbMenuItem = new JRadioButtonMenuItem("Another one"); | |
rbMenuItem.setMnemonic(KeyEvent.VK_O); | |
group.add(rbMenuItem); | |
menu.add(rbMenuItem); | |
// a group of check box menu items | |
menu.addSeparator(); | |
cbMenuItem = new JCheckBoxMenuItem("A check box menu item"); | |
cbMenuItem.setMnemonic(KeyEvent.VK_C); | |
// Added these two | |
cbMenuItem.setActionCommand("cbMenuItem"); | |
cbMenuItem.addActionListener(this); | |
menu.add(cbMenuItem); | |
cbMenuItem = new JCheckBoxMenuItem("Another one"); | |
cbMenuItem.setMnemonic(KeyEvent.VK_H); | |
menu.add(cbMenuItem); | |
// a submenu | |
menu.addSeparator(); | |
submenu = new JMenu("A submenu"); | |
submenu.setMnemonic(KeyEvent.VK_S); | |
menuItem = new JMenuItem("An item in the submenu"); | |
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, | |
ActionEvent.ALT_MASK)); | |
// Added these two | |
menuItem.setActionCommand("An item in the subment"); | |
menuItem.addActionListener(this); | |
submenu.add(menuItem); | |
menuItem = new JMenuItem("Another item 2a"); | |
// Added these two | |
menuItem.setActionCommand("Another item 2a"); | |
menuItem.addActionListener(this); | |
submenu.add(menuItem); | |
menu.add(submenu); | |
// Build second menu in the menu bar. | |
menu = new JMenu("Menu1"); | |
menu.setMnemonic(KeyEvent.VK_N); | |
menu.getAccessibleContext().setAccessibleDescription( | |
"This menu does nothing"); | |
menuBar.add(menu); | |
// Menu10 | |
menuItem = new JMenuItem("Menu10"); | |
menuItem.setActionCommand("Menu10"); | |
menuItem.addActionListener(this); | |
menu.add(menuItem); | |
// Menu 11 | |
menuItem = new JMenuItem("Menu11"); | |
menuItem.setActionCommand("Menu11"); | |
menuItem.addActionListener(this); | |
menu.add(menuItem); | |
menu.addSeparator(); | |
// Menu 12 | |
menuItem = new JMenuItem("Menu12"); | |
menuItem.setActionCommand("Menu12"); | |
menuItem.addActionListener(this); | |
menu.add(menuItem); | |
// ********************************************* | |
// submenu2 | |
submenu2 = new JMenu("Submenu 1X"); | |
submenu2.setMnemonic(KeyEvent.VK_S); | |
menuItem = new JMenuItem("**Top:"); | |
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, | |
ActionEvent.ALT_MASK)); | |
// Added these two | |
menuItem.setActionCommand("An item in the subment"); | |
menuItem.addActionListener(this); | |
submenu2.add(menuItem); | |
menuItem = new JMenuItem("1a"); | |
// Added these two | |
menuItem.setActionCommand("1a"); | |
menuItem.addActionListener(this); | |
submenu2.add(menuItem); | |
menu.add(submenu2); | |
// Menu 13 | |
menuItem = new JMenuItem("Menu13"); | |
menuItem.setActionCommand("Menu13"); | |
menuItem.addActionListener(this); | |
menu.add(menuItem); | |
menu = new JMenu("Refresh0"); | |
menu.setMnemonic(KeyEvent.VK_N); | |
menu.setActionCommand("Refresh0"); | |
menu.addActionListener(this); | |
menu.getAccessibleContext().setAccessibleDescription( | |
"This menu does nothing"); | |
menuBar.add(menu); | |
menu = new JMenu("Refresh1"); | |
menu.setActionCommand("Refresh0"); | |
// menu.addActionListener(this); | |
menu.setMnemonic(KeyEvent.VK_N); | |
menu.getAccessibleContext().setAccessibleDescription( | |
"This menu does nothing"); | |
menuBar.add(menu); | |
return menuBar; | |
} | |
public void setFrame(JFrame frame) { | |
this.frame = frame; | |
} | |
public Container createContentPane() { | |
// Create the content-pane-to-be. | |
JPanel contentPane = new JPanel(new BorderLayout()); | |
contentPane.setOpaque(true); | |
// Create a scrolled text area. | |
if (output == null) | |
output = new JTextArea(5, 30); | |
output.setEditable(false); | |
if (scrollPane == null) | |
scrollPane = new JScrollPane(output); | |
// Add the text area to the content pane. | |
contentPane.add(scrollPane, BorderLayout.CENTER); | |
return contentPane; | |
} | |
public JPanel createPanel0() { | |
JPanel p = new JPanel(new GridBagLayout()); | |
p.setOpaque(true); | |
p.setBackground(Color.WHITE); | |
GridBagConstraints c = new GridBagConstraints(); | |
c.insets = new Insets(0, 0, 0, 0); // insets for all components | |
c.gridwidth = 1; | |
c.gridheight = 1; | |
c.gridx = 0; // column 0 | |
c.gridy = 0; // row 0 | |
// c.ipadx = 850; // width x | |
// c.ipady = 400; // width y | |
// New | |
c.ipadx = 1; | |
c.ipady = 1; | |
JButton button0 = new JButton("Button 1"); | |
button0.addActionListener(this); | |
p.add(button0, c); | |
c.gridx = 1; | |
JButton button1 = new JButton("Button 2"); | |
button1.addActionListener(this); | |
p.add(button1, c); | |
c.gridx = 0; | |
c.gridy = 1; | |
JButton button3 = new JButton("Button 3"); | |
button3.addActionListener(this); | |
p.add(button3, c); | |
c.gridx = 1; | |
c.gridy = 1; | |
JButton button4 = new JButton("Button 4"); | |
button4.addActionListener(this); | |
p.add(button4, c); | |
c.gridx = 0; | |
c.gridy = 2; | |
c.gridwidth = 2; | |
/* | |
* if (output == null) output = new JTextArea(5, 30); | |
*/ | |
System.out.println("p0"); | |
// output and scrollPane | |
JTextArea output = new JTextArea(5, 30); | |
this.output0 = output; | |
JScrollPane scrollPane = new JScrollPane(output); | |
this.scrollPane0 = scrollPane; | |
scrollPane | |
.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); | |
output.setEditable(true); | |
output.setBackground(Color.GREEN); | |
p.add(scrollPane, c); | |
return p; | |
} | |
public JPanel createPanel1() { | |
JPanel p = new JPanel(new GridBagLayout()); | |
p.setOpaque(true); | |
p.setBackground(Color.getHSBColor(0.3f, 0.3f, 0.3f)); | |
GridBagConstraints c = new GridBagConstraints(); | |
c.insets = new Insets(0, 0, 0, 0); // insets for all components | |
c.gridwidth = 1; | |
c.gridheight = 1; | |
c.gridx = 0; // column 0 | |
c.gridy = 0; // row 0 | |
// c.ipadx = 850; // width x | |
// c.ipady = 400; // width y | |
// New | |
c.ipadx = 1; | |
c.ipady = 1; | |
JButton button0 = new JButton("Button 1"); | |
button0.addActionListener(this); | |
p.add(button0, c); | |
c.gridx = 1; | |
JButton button1 = new JButton("Button 2"); | |
button1.addActionListener(this); | |
p.add(button1, c); | |
c.gridx = 0; | |
c.gridy = 1; | |
JButton button3 = new JButton("Button 3"); | |
button3.addActionListener(this); | |
p.add(button3, c); | |
c.gridx = 1; | |
c.gridy = 1; | |
JButton button4 = new JButton("Button 4"); | |
button4.addActionListener(this); | |
p.add(button4, c); | |
c.gridx = 2; | |
c.gridy = 0; | |
JButton button5 = new JButton("Button 5"); | |
button5.addActionListener(this); | |
p.add(button5, c); | |
c.gridx = 0; | |
c.gridy = 2; | |
c.gridwidth = 2; | |
// output and scrollPane | |
// output and scrollPane | |
JTextArea output = new JTextArea(5, 30); | |
JScrollPane scrollPane = new JScrollPane(output); | |
scrollPane | |
.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); | |
output.setEditable(true); | |
output.setBackground(Color.GREEN); | |
this.output1 = output; | |
this.scrollPane1 = scrollPane; | |
p.add(scrollPane, c); | |
return p; | |
} | |
/** Returns an ImageIcon, or null if the path was invalid. */ | |
protected static ImageIcon createImageIcon(String path) { | |
java.net.URL imgURL = MenuLookDemo.class.getResource(path); | |
if (imgURL != null) { | |
return new ImageIcon(imgURL); | |
} else { | |
System.err.println("Couldn't find file: " + path); | |
return null; | |
} | |
} | |
/** | |
* Create the GUI and show it. For thread safety, this method should be | |
* invoked from the event-dispatching thread. | |
*/ | |
private static void createAndShowGUI() { | |
// Create and set up the window. | |
JFrame frame = new JFrame("MenuLookDemo With Events"); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
// Create and set up the content pane. | |
MenuLookDemo demo = new MenuLookDemo(); | |
frame.setJMenuBar(demo.createMenuBar()); | |
// frame.setContentPane(demo.createContentPane()); | |
demo.setP0(demo.createPanel0()); | |
demo.setP1(demo.createPanel1()); | |
frame.add(demo.getP1()); | |
demo.setFrame(frame); | |
// Display the window. | |
frame.setSize(550, 360); | |
frame.setVisible(true); | |
} | |
public static void main(String[] args) { | |
// Schedule a job for the event-dispatching thread: | |
// creating and showing this application's GUI. | |
javax.swing.SwingUtilities.invokeLater(new Runnable() { | |
public void run() { | |
createAndShowGUI(); | |
} | |
}); | |
} | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
// TODO Auto-generated method stub | |
System.out.println("Event:" + e.getActionCommand().toString()); | |
if (e.getActionCommand().toString().equalsIgnoreCase("Button 1")) { | |
System.out.println("Removing from frame 1"); | |
this.frame.remove(getP0()); | |
this.frame.add(getP1()); | |
this.frame.validate(); | |
this.frame.repaint(); | |
} | |
if (e.getActionCommand().toString().equalsIgnoreCase("Button 2")) { | |
System.out.println("Removing from frame"); | |
this.frame.remove(getP1()); | |
this.frame.add(getP0()); | |
this.frame.validate(); | |
this.frame.repaint(); | |
} | |
// Extra's | |
if (e.getActionCommand().toString().equalsIgnoreCase("Another item 2")) { | |
System.out.println("Removing from frame"); | |
this.frame.remove(getP1()); | |
// this.frame.repaint(); | |
this.frame.add(getP0()); | |
this.frame.validate(); | |
this.frame.repaint(); | |
} | |
if (e.getActionCommand().toString() | |
.equalsIgnoreCase("An item in the subment")) { | |
System.out.println("Removing from frame"); | |
this.frame.remove(getP0()); | |
this.frame.add(getP1()); | |
this.frame.validate(); | |
this.frame.repaint(); | |
} | |
if (e.getActionCommand().toString().equalsIgnoreCase("Repaint")) { | |
System.out.println("In Repaint: this.frame.validate()"); | |
getP0().repaint(); | |
this.frame.validate(); | |
this.frame.repaint(); | |
} | |
// Repaint | |
this.output.setText(this.output.getText() + "\nEvent:" | |
+ e.getActionCommand().toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment