I have a Problem with some icons and check-boxes in a JPopupMenu. The Check-boxes and Icons are not aligned
The Items are created like:
JMenuItem deleteAttributeMenuItem = new JMenuItem(locale.getString("TREE_AttrDelete"), iconDelete);
JMenuItem primaryKeyAttributeMenuItem = new JCheckBoxMenuItem(locale.getString("TREE_AttrChkBoxPK"));
Please take a look at the picture:
Any tips?
Have a look at this, in order to achieve what you wanted, I did this,
JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem("Check Me", null, true);
cbmi.setMargin(new java.awt.Insets(5, 25, 5, 5));
cbmi.setIconTextGap(15);
cbmi.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
helpMenu.add(cbmi);
And here is the OUTPUT of the said thingy :
right now I can't found the right way how to re_layout JCheckBoxMenuItem,
but do you agree with this standars output from Swing by using (default) Metal Look and Feel???, just to avoiding any missunderstand by using another Look and Feel(s), because there are some differencies in the API betweens Swing's Standard Look and Feels too
from tutorials code (modified and removed balasts and noises)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MenuLookDemo {
private Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");
private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");
private Icon warnIcon = UIManager.getIcon("OptionPane.warningIcon");
private JTextArea output;
private JScrollPane scrollPane;
private JMenuBar menuBar;
private JMenu menu;
private JMenuItem menuItem;
private JRadioButtonMenuItem rbMenuItem;
private JCheckBoxMenuItem cbMenuItem;
public JMenuBar createMenuBar() {
menuBar = new JMenuBar();
menu = new JMenu("A Menu");
menu.getAccessibleContext().setAccessibleDescription("The only menu in this program that has menu items");
menuBar.add(menu);
menuItem = new JMenuItem("A text-only menu item", KeyEvent.VK_T);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.getAccessibleContext().setAccessibleDescription("This doesn't really do anything");
menu.add(menuItem);
menuItem = new JMenuItem("Both text and icon", errorIcon);
menu.add(menuItem);
menu.addSeparator();
ButtonGroup group = new ButtonGroup();
rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
rbMenuItem.setSelected(true);
group.add(rbMenuItem);
menu.add(rbMenuItem);
rbMenuItem = new JRadioButtonMenuItem("Another one", infoIcon);
group.add(rbMenuItem);
menu.add(rbMenuItem);
menu.addSeparator();
cbMenuItem = new JCheckBoxMenuItem("A check box menu item", warnIcon);
menu.add(cbMenuItem);
cbMenuItem = new JCheckBoxMenuItem("Another one");
menu.add(cbMenuItem);
menu.addSeparator();
return menuBar;
}
public Container createContentPane() {
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setOpaque(true);
output = new JTextArea(5, 30);
output.setEditable(false);
scrollPane = new JScrollPane(output);
contentPane.add(scrollPane, BorderLayout.CENTER);
return contentPane;
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("MenuLookDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MenuLookDemo demo = new MenuLookDemo();
frame.setJMenuBar(demo.createMenuBar());
frame.setContentPane(demo.createContentPane());
frame.setSize(450, 260);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
these methods talking about possitions in pixels
Related
im making this one game and i have menu created for it that opens first when starting the program but the menu buttons wont do anything. How do i link this menu window to the game so that when i press "new game" the game starts?
P.S
Yes i did try to find solution from several youtube videos and javas own help site but i couldnt find how to link button click action to start up action in game so that it basicly opens up game screen and starts the game.
All kind of help would be appreciated!
Code:
public class Menu {
JTextArea output;
JScrollPane scrollPane;
public JMenuBar createMenuBar() {
JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
JRadioButtonMenuItem rbMenuItem;
JCheckBoxMenuItem cbMenuItem;
//Create the menu bar.
menuBar = new JMenuBar();
//Build the first menu.
menu = new JMenu("Menu");
menu.setMnemonic(KeyEvent.VK_A);
menu.getAccessibleContext().setAccessibleDescription(
"The only menu in this program that has menu items");
menuBar.add(menu);
//a group of JMenuItems
menuItem = new JMenuItem("New Game",
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");
menu.add(menuItem);
ImageIcon icon = createImageIcon("");
menuItem = new JMenuItem("Score history", icon);
menuItem.setMnemonic(KeyEvent.VK_B);
menu.add(menuItem);
menuItem = new JMenuItem(icon);
menuItem.setMnemonic(KeyEvent.VK_D);
menu.add(menuItem);
//a submenu
menu.addSeparator();
submenu = new JMenu("Options");
submenu.setMnemonic(KeyEvent.VK_S);
menuItem = new JMenuItem("Sounds");
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_2, ActionEvent.ALT_MASK));
submenu.add(menuItem);
menuItem = new JMenuItem("Exit Game");
submenu.add(menuItem);
menu.add(submenu);
//Build second menu in the menu bar.
return menuBar;
}
public Container createContentPane() {
//Create the content-pane-to-be.
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setOpaque(true);
//Create a scrolled text area.
output = new JTextArea(5, 30);
output.setEditable(false);
scrollPane = new JScrollPane(output);
//Add the text area to the content pane.
contentPane.add(scrollPane, BorderLayout.CENTER);
return contentPane;
}
/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = Menu.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("BlockBreaker");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
Menu demo = new Menu();
frame.setJMenuBar(demo.createMenuBar());
frame.setContentPane(demo.createContentPane());
//Display the window.
frame.setSize(450, 260);
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();
}
});
}
}
Here is how i imagined it in my mind:
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.getAccessibleContext().setAccessibleDescription(
"This doesn't really do anything");
menu.add(menuItem);
menuItem = new MouseAction(MouseEvent.PRESS_LMB)
if (PRESS_LMB.menuItem("New Game")) {
start Gameplay.java;
}
Use AbstractAction class with your JMenuItem as below.
Replace your line of code:
menuItem = new JMenuItem("New Game", KeyEvent.VK_T);
By:
menuItem = new JMenuItem((new AbstractAction("New Game") {
public void actionPerformed(ActionEvent e) {
System.out.print("clicked");
}
}));
Use separate name for different game menu otherwise you can't add diffrent action to them.
Action performed using anonymous class or ActionListener. I'm showing for anonymous class.
See This for more Information: How to use JMenu
Using anonymous class:
final JMenuItem newGame;
newGame = new JMenuItem("New Game",
KeyEvent.VK_T);
menu.add(newGame);
newGame.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JFrame frame = new JFrame();
frame.setSize(500, 100);
frame.setTitle("Game on Fire");
frame.show();
}
});
Using ActionListener:
public YourClass implements ActionListener, ItemListener {
#Override
public void actionPerformed(ActionEvent e) {
//...Get information from the action event...
//...Display it in the text area...
}
}
If you want to run game on same window, use JPanel inside actionPerformed. Here is an Example:
public class GameClass extends JPanel{
///Game Logic Here
}
then add this too your actionPerformed.
public void actionPerformed(ActionEvent e) {
GameClass game = new GameClass();
menuPanel.setVisible(false); /// menuPanel is a selection window.
mainPanel.add(game); // Now add your Game Window to same Frame.
}
Main thread calls GUI() function that adds frame, menus, menuitems, and a scrollpane with jtable (DefaultTableModel).
If i place the running application to a second display/screen it hangs as soon i switch to a different application or when i try to resize it.
public static void main(String[] args) {
GUI();
}
public static void GUI(){
JButton button;
JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
window = new JFrame();
window.setResizable(false);
//menu
menuBar = new JMenuBar();
menu = new JMenu("Start");
submenu = new JMenu("New");
menu.add(submenu);
menuItem = new JMenuItem("menu1");
menuItem.addActionListener(new buttonActionListener("m1"));
submenu.add(menuItem);
menuItem = new JMenuItem("menu2");
menuItem.addActionListener(new buttonActionListener("m2"));
submenu.add(menuItem);
menuItem = new JMenuItem("menu3");
menuItem.addActionListener(new buttonActionListener("m3"));
submenu.add(menuItem);
menuItem = new JMenuItem("menu4");
menuItem.addActionListener(new buttonActionListener("m4"));
submenu.add(menuItem);
menuBar.add(menu); //start
menu = new JMenu("Options");
menuBar.add(menu); //options
//top panel
JPanel top = new JPanel();
button = new JButton("Paste");
button.addActionListener(new buttonActionListener("paste"));
top.add(button);
button = new JButton("Copy");
button.addActionListener(new buttonActionListener("copy"));
top.add(button);
JButton start = new JButton("Start");
start.addActionListener(new buttonActionListener("start"));
top.add(start);
//main panel with table
main = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
DefaultTableModel model = new DefaultTableModel();
table = new JTable(model);
//bottom panel
JPanel bottom = new JPanel();
Creq = new JCheckBox();
Creq.setSelected(true);
item= new JTextField("item");
comment = new JTextField("comment");
target= new JTextField("target");
req = new JTextField("requirement");
bottom.add(Creq);
bottom.add(req);
bottom.add(item);
depToUpdate.setVisible(true);
bottom.add(target);
targetDep.setVisible(true);
bottom.add(comment);
comment.setVisible(true);
window.setJMenuBar(menuBar);
window.add(top, BorderLayout.NORTH);
window.add(main, BorderLayout.CENTER);
window.add(bottom, BorderLayout.SOUTH);
window.pack();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocationRelativeTo(null);
window.setVisible(true);
}
After spending way too much time fixing the code so that it would actually run in Java, I tested the GUI on my second monitor in Windows 8. The GUI didn't do anything, but it didn't freeze either.
Here are the problems I fixed:
I moved the Swing GUI code into a regular class method (not static).
I put the Swing components on the Event Dispatch thread. You must always create and execute your Swing components on the Event Dispatch thread.
Here's the version of the code I ran.
package snippet;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
public class Snippet implements Runnable{
private JCheckBox creq;
private JFrame window;
private JScrollPane main;
private JTable table;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Snippet());
}
#Override
public void run() {
JButton button;
JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
window = new JFrame();
window.setResizable(false);
// menu
menuBar = new JMenuBar();
menu = new JMenu("Start");
submenu = new JMenu("New");
menu.add(submenu);
menuItem = new JMenuItem("menu1");
// menuItem.addActionListener(new buttonActionListener("m1"));
submenu.add(menuItem);
menuItem = new JMenuItem("menu2");
// menuItem.addActionListener(new buttonActionListener("m2"));
submenu.add(menuItem);
menuItem = new JMenuItem("menu3");
// menuItem.addActionListener(new buttonActionListener("m3"));
submenu.add(menuItem);
menuItem = new JMenuItem("menu4");
// menuItem.addActionListener(new buttonActionListener("m4"));
submenu.add(menuItem);
menuBar.add(menu); // start
menu = new JMenu("Options");
menuBar.add(menu); // options
// top panel
JPanel top = new JPanel();
button = new JButton("Paste");
// button.addActionListener(new buttonActionListener("paste"));
top.add(button);
button = new JButton("Copy");
// button.addActionListener(new buttonActionListener("copy"));
top.add(button);
JButton start = new JButton("Start");
// start.addActionListener(new buttonActionListener("start"));
top.add(start);
// main panel with table
// main = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
// ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
DefaultTableModel model = new DefaultTableModel();
table = new JTable(model);
main = new JScrollPane(table);
// bottom panel
JPanel bottom = new JPanel();
creq = new JCheckBox();
creq.setSelected(true);
JTextField item = new JTextField("item");
JTextField comment = new JTextField("comment");
JTextField target = new JTextField("target");
JTextField req = new JTextField("requirement");
bottom.add(creq);
bottom.add(req);
bottom.add(item);
// depToUpdate.setVisible(true);
bottom.add(target);
// targetDep.setVisible(true);
bottom.add(comment);
comment.setVisible(true);
window.setJMenuBar(menuBar);
window.add(top, BorderLayout.NORTH);
window.add(main, BorderLayout.CENTER);
window.add(bottom, BorderLayout.SOUTH);
window.pack();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocationRelativeTo(null);
window.setVisible(true);
}
}
anyone can tell why the class OpenMenuListener doesnt send a feedback when i click the open button in my Gui ? The erase button works though. It sends me a feedback. I'm exausted.
import java.awt.*;
import javax.swing.*;
public class DrawingApplication extends JFrame {
JComponent drawingArea;
class EraseButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("Clicked erase");
}
}
class OpenMenuListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.out.println("Clicked open");
}
}
public DrawingApplication() {
JPanel frame = new JPanel();
add(frame);
// panel1.add( new JButton(Figuur),BorderLayout.CENTER);
drawingArea = new JLabel();
// label1.add(drawingArea);
frame.add(drawingArea);
// Creates a menubar for a JFrame
JMenuBar menuBar = new JMenuBar();
// Add the menubar to the frame
setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);
JMenu open = new JMenu("Open");
fileMenu.add(open);
fileMenu.addSeparator();
JMenu save = new JMenu("Save");
fileMenu.add(save);
fileMenu.addSeparator();
JMenu close =new JMenu("Close");
fileMenu.add(close);
JMenu helpMenu = new JMenu("Help");
menuBar.add(helpMenu);
helpMenu.add(new JMenu("Info"));
JPanel panel2 = new JPanel();
add(BorderLayout.SOUTH, frame);
frame.add(new JLabel("figuurkeuze"));
frame.add(panel2);
setVisible(true);
JRadioButton rectButton = new JRadioButton("Rectangle");
JRadioButton triangleButton = new JRadioButton("Triangle");
JRadioButton circleButton = new JRadioButton("Circle");
frame.add(rectButton);
frame.add(triangleButton);
frame.add(circleButton);
JButton erase = new JButton("Erase");
frame.add(erase);
EraseButtonListener eraselistener = new EraseButtonListener();
erase.addActionListener(eraselistener);
OpenMenuListener openMenuListener = new OpenMenuListener();
open.addActionListener(openMenuListener);
}
public static void main(String[] args) {
DrawingApplication frame = new DrawingApplication();
frame.setTitle("My prgram");
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
It seems a problem of name aliasing, you add the listener to the open variable which is declared as a JMenuItem, so you are adding the ActionListener to the menu item instead that to a button (that you never declare since there is no JButton open = new JButton("open") anywhere).
Java Swing seems to place the 'Menu Text' after the icon (if present) on MenuItems. See example below.
It does NOT look very nice.
Is there a way around this?
On OSX the icon fits in the left margin and the text aligns with all other MenuItems.
Do you mean something like this :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JTextPaneExample
{
private Icon info = UIManager.getIcon("OptionPane.informationIcon");
private Icon error = UIManager.getIcon("OptionPane.errorIcon");
private void createAndDisplayGUI()
{
JFrame frame = new JFrame("JTextPane Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextPane tpane = new JTextPane();
tpane.setContentType("text/html");
JScrollPane scroller = new JScrollPane();
scroller.setViewportView(tpane);
try
{
java.net.URL url = new java.net.URL("http://maps.google.es/");
//tpane.setPage(url);
}
catch (Exception e)
{
e.printStackTrace();
}
frame.setJMenuBar(createMenuBar());
frame.getContentPane().add(scroller);
frame.setSize(300, 300);
frame.setVisible(true);
}
private JMenuBar createMenuBar()
{
JMenuBar menuBar = new JMenuBar();
JMenu windowMenu = new JMenu("Window");
JMenuItem minimizeItem = new JMenuItem("Minimize");
minimizeItem.setMargin(new java.awt.Insets(0, 10, 0, 0));
minimizeItem.setIcon(info);
minimizeItem.setIconTextGap(1);
minimizeItem.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
JMenuItem zoomItem = new JMenuItem("Zoom");
zoomItem.setMargin(new java.awt.Insets(0, 10, 0, 0));
zoomItem.setIconTextGap(1);
zoomItem.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem("Check Me", null, true);
cbmi.setMargin(new java.awt.Insets(5, 25, 5, 5));
cbmi.setIconTextGap(17);
cbmi.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
windowMenu.add(minimizeItem);
windowMenu.add(zoomItem);
windowMenu.add(cbmi);
menuBar.add(windowMenu);
return menuBar;
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new JTextPaneExample().createAndDisplayGUI();
}
});
}
}
Here is the Output :
You could try either of these approaches:
Unicode characters are appealing, but they offer poor alignment in a variable pitch font:
JMenuBar menuBar = new JMenuBar();
JMenu windowMenu = new JMenu("Window");
windowMenu.add(new JMenuItem("♦ Item"));
windowMenu.add(new JMenuItem("✓ Item"));
windowMenu.add(new JMenuItem("• Item"));
menuBar.add(windowMenu);
frame.setJMenuBar(menuBar);
Better, implement the Icon interface, illustrated here and here, using a fixed-size implementation to control geometry. CellTest shows one approach to rendering an arbitrary unicode glyph.
I had a simple drawing application. I need to add a menu and a toolbar on the left side.
So now, instead of using a simple JFrame, I'm creating a simple class that extends JFrame. I was able to add the menu following some examples online, but can't figure out how to add a JToolBar. I've tried a few different ways, but nothing works. I don't get an error, everything complies just fine, but I don't see any JToolBar.
Here's the code for my JFrame, I hope you can help.
class Menu extends JFrame {
private JMenuItem openItem;
private JMenuItem saveItem;
private JMenuItem saveAsItem;
public Menu(String title) {
openItem = new JMenuItem("Open...");
openItem.setMnemonic('O');
openItem.setAccelerator(KeyStroke.getKeyStroke("control O"));
saveItem = new JMenuItem("Save");
saveItem.setMnemonic('S');
saveItem.setAccelerator(KeyStroke.getKeyStroke("control S"));
saveAsItem = new JMenuItem("Save As...");
saveAsItem.setMnemonic('S');
saveAsItem.setAccelerator(KeyStroke.getKeyStroke("control S"));
// (2) Build menubar, menus, and add menuitems.
JMenuBar menubar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic('F');
menubar.add(fileMenu);
fileMenu.add(openItem);
fileMenu.addSeparator();
fileMenu.add(saveItem);
// (3) Add listeners to menu items
openItem.addActionListener(new OpenAction()); // TODO change
setJMenuBar(menubar);
JToolBar toolbar = new JToolBar("Toolbar", JToolBar.VERTICAL);//);
// JPanel panel = new JPanel();
// panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JButton newb = new JButton("new");
toolbar.add(newb);
// toolbar.setOpaque(true);
toolbar.setLocation(100, 100);
toolbar.setVisible(true);
// toolbar.setMinimumSize(new Dimension(100, 100));
// toolbar.setAlignmentX(0);
// panel.add(toolbar);
add(toolbar, BorderLayout.NORTH);
getContentPane().add(toolbar, BorderLayout.NORTH);
// getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
setTitle(title);
pack();
setLocationRelativeTo(null); // Center window.
}
// OpenAction
class OpenAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(Menu.this, "Can't Open.");
}
}
}
its work fine, and you don't need to setVisible tool bar because its showing by default, also don't add the tool bar two time in the same place (NORTH)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MenuDemo {
public static void main(String... args) {
EventQueue.invokeLater(
new Runnable() {
#Override
public void run() {
JFrame menu = new Menu("Testing");
menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menu.setVisible(true);
}
}
);
}
}
class Menu extends JFrame {
private JMenuItem openItem;
private JMenuItem saveItem;
private JMenuItem saveAsItem;
public Menu(String title) {
openItem = new JMenuItem("Open...");
openItem.setMnemonic('O');
openItem.setAccelerator(KeyStroke.getKeyStroke("control O"));
saveItem = new JMenuItem("Save");
saveItem.setMnemonic('S');
saveItem.setAccelerator(KeyStroke.getKeyStroke("control S"));
saveAsItem = new JMenuItem("Save As...");
saveAsItem.setMnemonic('S');
saveAsItem.setAccelerator(KeyStroke.getKeyStroke("control S"));
// (2) Build menubar, menus, and add menuitems.
JMenuBar menubar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic('F');
menubar.add(fileMenu);
fileMenu.add(openItem);
fileMenu.addSeparator();
fileMenu.add(saveItem);
// (3) Add listeners to menu items
openItem.addActionListener(new OpenAction()); // TODO change
setJMenuBar(menubar);
JToolBar toolbar = new JToolBar("Toolbar", JToolBar.VERTICAL);//);
JButton newb = new JButton("new");
toolbar.add(newb);
add(toolbar, BorderLayout.NORTH);
setTitle(title);
setLocationRelativeTo(null);
pack();
}
// OpenAction
private class OpenAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(Menu.this, "Can't Open.");
}
}
}
My problem was that I was the way I was instantiating my JFrame. I was using a helper function like this one:
public static JFrame openInJFrame(Container content, int width, int height,
String title, Color bgColor) {
// ...
frame.setContentPane(content);
frame.setVisible(true);
return (frame);
}
So my JToolBar was getting replaced by the Container object...
Thanks guys! Your answers helped me figure out my problem.