I'm doing a program where I'm working with getting the menu's to appear, but for some reason I can't seem to get the names for my menu drop downs to appear.I would greartly appreciante some help with this matter.
public class Application extends JPanel implements ActionListener{
JPanel p;
JFrame f;
JMenu m;
JMenu m2;
JMenuBar menu;
JMenuItem item;
JMenuItem item2;
Application()
{
super();
//creating panel
f=new JFrame("CMPSC 221 Exam 2 ");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(200,200);
f.setContentPane(new JDesktopPane());
m2 = new JMenu();
menu = new JMenuBar();
m = new JMenu();
//adding the the drop down menu
m.setName("File");
m.add("New");
m.addSeparator();
m.add("Open");
menu.add(m);
m2.setName("Edit");
m2.add("Copy");
m2.addSeparator();
m2.add("Paste");
menu.add(m2);
menu.setVisible(true);
f.setJMenuBar(menu);
f.setVisible(true);
}
Don't use m.setName - pass the name into JMenu() constructor
Related
I have a JMenu A within a JMenuBar aligned to the right of the screen. Within this menu are several JMenuItems, along with another JMenu B. Since menu A is right aligned, I need menu B to open to the left. In order to do this I found the code
setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);.
The issue however, is that this messes with the text alignment of menu 2, which I would like to stay exactly like the other menu items.
I have also tried manually aligning menu 2 using SwngConstants.leftAlign, however this is too severe:
How can I make the menu expand to the left, while retaining the default text alignment? Thanks in advance! The code I used to produce the above images is seen below:
import java.awt.*;
import javax.swing.*;
public class Test{
public Test(){
JFrame frame = new JFrame();
JMenuBar menuBar = new JMenuBar();
JMenu menu1 = new JMenu("Menu 1");
JMenu menu2 = new JMenu("Menu 2");
JMenuItem menuitem1 = new JMenuItem("Menu Item 1");
JMenuItem menuitem2 = new JMenuItem("Menu Item 2");
JMenuItem menuitem3 = new JMenuItem("Menu Item 3");
JMenuItem menuitem4 = new JMenuItem("Menu Item 4");
menuBar.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
menu1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
menu2.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
menu2.setHorizontalAlignment(SwingConstants.LEFT);
menuBar.add(menu1);
menu1.add(menuitem1);
menu1.add(menuitem2);
menu1.add(menu2);
menu2.add(menuitem3);
menu2.add(menuitem4);
frame.setJMenuBar(menuBar);
frame.getContentPane().setBackground(Color.WHITE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(270,170));
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args){
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
UIManager.getLookAndFeelDefaults().put("Label.font", new Font("Arial", Font.PLAIN, 12));
UIManager.getLookAndFeelDefaults().put("Button.font", new Font("Arial", Font.PLAIN, 12));
UIManager.getLookAndFeelDefaults().put("ComboBox.font", new Font("Arial", Font.PLAIN, 12));
UIManager.getLookAndFeelDefaults().put("JTextField.font", new Font("Arial", Font.PLAIN, 12));
new Test();
} catch(Exception e){
JOptionPane.showConfirmDialog(null, e.getMessage());
}
}
}
UPDATE: If I remove the lines of code aligning the menus right to left and move the frame to the edge of the display screen, then the menus act as desired (i.e. the menu will not expand off the monitor). Perhaps there is a way to tell the menu not to expand off the JFrame rather than the monitor?
UPDATE: Thank you #StanislavL for the idea. Overriding menu 2 with the following code does the trick, it also gets rid of that unsightly overlap between the two menus. Note that menu 2 no longer needs ComponentOrientation.RIGHT_TO_LEFT.
JMenu menu2 = new JMenu("Menu 2"){
protected Point getPopupMenuOrigin(){
return new Point(-this.getPopupMenu().getPreferredSize().width, 0);
}
};
You can try to override
public ComponentOrientation getComponentOrientation()
method of JMenu to return your alignmnet
or JMenu has method you can override
protected Point getPopupMenuOrigin()
I'm just upping my knowledge on GUI and implementation of GUI's and buttons, and I've run into a little issue. I've searched for this, and even compared button codes but nothing seems different from what I'm seeing in comparison to others' code. When I add html to any button, take testOne for instance if I use:
JButton testOne = new JButton("<html><b><u>T</u>est<br>1</b></html>");
I have a button, of course, but the button is so large with tiny font, while my other buttons are normal size. Anyone have an idea why? I posted the code without a homework tag, because it's not homework, just fun practice.
public FunProject(){
super("Fun Project"); //child
//create layout and get the content pane for content
contents = getContentPane();
contents.setLayout(new FlowLayout(FlowLayout.CENTER));
leftBox = getContentPane();
leftBox.setLayout(new BoxLayout(leftBox, BoxLayout.X_AXIS));
//menuBar = new
//Create the buttons to use
JButton testOne = new JButton("Test 1");
JButton testTwo = new JButton("Test 2");
JButton testThree = new JButton("Test 3");
Also, for anyone feeling like spreading a little knowledge, how would I go about stacking my buttons? Right now they're just in a horizontal plane unstacked, but at the location I want (Center to the left). Thank you in advance.
import javax.swing.*;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import java.awt.*;
import java.awt.event.*;
public class FunProject extends JFrame implements ActionListener, KeyListener, MenuListener{
//Content only visible to me
private Container contents;
private Container leftBox;
private JButton testOne, testTwo, testThree;
private JMenuBar menuBar;
private JMenu men, file, edit, exit;
private JMenuItem fileOpen, fileSave, fileType;
private JMenuItem editOpen, editSave, editType;
//Constructor
public FunProject(){
super("Fun Project");
//create layout and get the content pane for content
contents = getContentPane();
contents.setLayout(new FlowLayout(FlowLayout.CENTER));
leftBox = getContentPane();
leftBox.setLayout(new BoxLayout(leftBox, BoxLayout.X_AXIS));
//Key Listener
this.addKeyListener(this);
//Creating Menu bar
menuBar = new JMenuBar();
//Adding header to menuBar
men = new JMenu("Test Menu");
men.addMenuListener(this);
menuBar.add(men);
//Adding exit to menuBar
exit = new JMenu("Exit Menu");
exit.setMnemonic(KeyEvent.VK_X);
exit.addMenuListener(this);
menuBar.add(exit);
//Add submenu
file = new JMenu("FILE");
file.addMenuListener(this);
men.add(file);
edit = new JMenu("EDIT");
edit.addMenuListener(this);
men.add(edit);
//Add item to submenu FILE
fileOpen = new JMenuItem("Open a file");
fileOpen.addActionListener(this);
file.add(fileOpen);
fileSave = new JMenuItem("Save a file");
fileSave.addActionListener(this);
file.add(fileSave);
fileType = new JMenuItem("File type");
fileType.addActionListener(this);
file.add(fileType);
//Add item to submenu EDIT
editOpen = new JMenuItem("Open a file");
editOpen.addActionListener(this);
edit.add(editOpen);
editSave = new JMenuItem("Save a file");
editSave.addActionListener(this);
edit.add(editSave);
edit = new JMenuItem("File type");
editType.addActionListener(this);
edit.add(editType);
//Add menu bar
this.setJMenuBar(menuBar);
//Create the buttons to use
JButton testOne = new JButton("Test 1");
JButton testTwo = new JButton("Test 2");
JButton testThree = new JButton("Test 3");
//Button modifications
testOne.setPreferredSize(new Dimension(100, 100));
//Add buttons and such to the leftBox (frame > pane)
leftBox.add(testOne);
leftBox.add(testTwo);
leftBox.add(testThree);
//Tell the program what Listener will handle events for the button or whatever you
//are implementing
testOne.addActionListener(this);
testTwo.addActionListener(this);
testThree.addActionListener(this);
//Set size and visibility to display content and color
setSize(400,300);
setVisible(true);
}
public static void main(String[] args){
FunProject fp = new FunProject();
fp.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
I was messing around with menu's (So ignore the menu code) and what they're all about. That's for later time when I learn it better.
1)try to use
h1 Tag -> JButton testOne = new JButton("<html><b><u><h1>T</u>est<br>1</b></html>");
for increase the size or font use <font> tag for changing the color or size of the font.
2)some time it depend upon the layout you used check it out
3) try ButtonObject.setBounds();
Image of the problem:
http://gyazo.com/56c4f7f5fc10805695fc80de567b92c5.png
private JButton settingsButton = new JButton("Settings");
private JPopupMenu settings = new JPopupMenu();
private JMenuItem accounts = new JMenuItem("Accounts");
settings.add(accounts);
settingsButton.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
settings.show(e.getComponent(), 0, 0);
}
});
So as you have seen on the image, I don't think this is any accurate. Is there any (better) method to get it on a the right spot?
I have clicked on the JButton: "Settings" and it should popup just a tad down it.
You are better off using a JMenuBar and then adding JMenus with JMenuItems, there is no point on reinventing the wheel:
JMenuBar menubar = new JMenuBar();
JMenu settings = new JMenu("Settings");
JMenuItem accounts = new JMenuItem("Accounts");
settings.add(accounts);
menubar.add(settings);
myFrame.setJMenuBar(menubar);
My main class displays a JMenuBar. This JMenuBar is managed from "calculator.ui.MenuBar."
public JMenuBar createMenuBar()
{
JMenuBar menuBar = new JMenuBar();
new calculator.ui.MenuBar(menuBar);
return menuBar;
}
MenuBar creates my "File" JMenu and "Insert" JMenu.
public MenuBar(JMenuBar menuBar)
{
new FileMenu(menuBar);
new InsertMenu(menuBar);
}
FileMenu contains all the options for "File." In the File class, there is a JMenuItem called "New Calculator." Now, when you click "New Calculator," I want the JPanel in my main class to create an instance of Calculator in my main class.
newFileSubMenu = new JMenu("New...");
calculatorFileSubMenu = new JMenuItem("New Calculator");
calculatorFileSubMenu.getAccessibleContext().setAccessibleDescription(
"New Calculator");
newFileSubMenu.add(calculatorFileSubMenu);
ActionListener newCalculatorListener = new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
newCalculator();
}
};
calculatorFileSubMenu.addActionListener(newCalculatorListener);
This is the code for my main class JPanel:
public Container createContentPane() {
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setOpaque(true);
JTabbedPane tabbedPane = new JTabbedPane();
return contentPane;
}
My questions are related to the design of my program. For each instance of Calculator, I want to:
Create a JPanel within the main JPanel that contains my Calculator (what stumps me here is how do I - from my FileMenu class - create a JPanel that's in my main class?).
Make sure that the Calculator object refreshes.
Note: I also want my JPanels to be in TabbedPanes (if that changes anything; if it doesn't, then I can figure that part out once I know the answer for the first question.)
Thanks for your help, I hope I've been clear enough in what I want to do.
In your menu item's Action, you can use setSelectedIndex() on your JTabbedPane to select the pane holding an existing calculator instance. You can use setComponentAt() to replace the content of any tab with an instance of your calculator.
There's a related example here.
I am making a simple java program that represents a Microsoft Word menu bar, and in the file menu I add an exit button... it does nothing.
I need your help to tell me what to do to make the exit button actually exit and close the program.
Here is my code:
public class MicrosoftWordMenu {
public static void main(String [] args)
{
JPanel panel = new JPanel();
JFrame frame = new JFrame("Microsoft Word");
JMenuBar bar = new JMenuBar();
JMenu menu = new JMenu("File");
JMenu menu1 = new JMenu("Edit");
JMenu menu2 = new JMenu("View");
JMenu menu3 = new JMenu("Insert");
JMenu menu4 = new JMenu("Format");
JMenu menu5 = new JMenu("Tools");
JMenu menu6 = new JMenu("Table");
JMenu menu7 = new JMenu("Window");
JMenu menu8 = new JMenu("Help");
frame.add(bar, BorderLayout.NORTH);
frame.add(panel);
bar.add(menu);
bar.add(menu1);
bar.add(menu2);
bar.add(menu3);
bar.add(menu4);
bar.add(menu5);
bar.add(menu6);
bar.add(menu7);
bar.add(menu8);
JMenuItem menuitem = new JMenuItem("New...");
JMenuItem menuitem1 = new JMenuItem("Open...");
JMenuItem menuitem2 = new JMenuItem("Close");
JMenuItem menuitem3 = new JMenuItem("Save");
JMenuItem menuitem4 = new JMenuItem("Save as...");
JMenuItem menuitem5 = new JMenuItem("Save as web page...");
JMenuItem menuitem6 = new JMenuItem("Web page preview ");
JMenuItem menuitem7 = new JMenuItem("Print ");
JMenuItem menuitem8 = new JMenuItem("Exit");
menu.add(menuitem);
menu.add(menuitem1);
menu.add(menuitem2);
menu.add(menuitem3);
menu.add(menuitem4);
menu.add(menuitem5);
menu.add(menuitem6);
menu.add(menuitem7);
menu.add(menuitem8);
frame.setSize(600,100);
frame.setVisible(true);
}
}
Also consider using Action to let components share functionality, as shown here.
Calling System.exit(0) on menu selection would do just fine.
At the moment you're just creating the GUI element, but they're basically empty shells. In particular, you've created items that appear on the menu, but you haven't added any behaviour to those items. (Since you haven't told your program anywhere what to do when an item is clicked, what did you think would happen)?
In order to provide this behaviour, you'll need to register an ActionListener, which will be called when something happens on an element, and will let you take an appropriate action at that point (such as calling System.exit(0). See the tutorial on Writing [Swing] Event listeners for details.
I always extend the AbstractAction class (which implements the ActionListener interface) which allows you to reuse your actions. By extending the AbstractAction, the actionPerformed(ActionEvent e) method will be invoked very time your button is clicked.
public class CloseAction extends AbstractAction {
public CloseAction() {
super("Close");
}
public actionPerformed(ActionEvent e) {
System.exit(1);
}
}
Now, to apply the above action to one of your buttons, you simply need to follow the below piece of code.
CloseButton.setAction(new CloseAction());
The close button will now shut down your application each time it gets pressed.
Use the ExitAction defined in Closing an Application. Then clicking on the Exit menu item will be just like clicking on the Close icon at the top right of the window. This allows for consistency when closing an application in case you ever need to do close processing.
Edit:
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
...
// JMenuItem menuitem8 = new JMenuItem("Exit");
JMenuItem menuitem8 = new JMenuItem( new ExitAction() );