I am just started learning Java and I've been reading through this documentation. I don't like to copy a bunch of code and paste it. So I have been trying to work my way through the documentation one thing at at time.
I already have a working JFrame and decided I would start by adding a menu.
HERE IS MY CODE:
package mainframe;
import javax.swing.*;
public class menuBar extends JMenuBar {
JMenuBar mainMenu = JMenuBar("Menu");
}
MY ERROR:
error: cannot find symbol
JMenuBar mainMenu = JMenuBar("Menu");
symbol: method JMenuBar(String)
location: class menuBar
1 error
So anyways. I am not really sure what the "cannot find symbol error" means. Maybe I am searching wrong. But every time I Google it it takes me to more complex questions with no clear answer. Any advice as to what I am doing wrong and or to what the cannot find symbol error means would be very much appreciated. Thanks in advance.
In response to your particular code here, I suggest that you do not extend the JMenuBar class. You may have seen it in many tutorials or examples where the JFrame class is extended, although that is considered bad practice. To add a JMenuBar to your window, I would suggest doing the following:
public class MyProgram {
JFrame frame;
public MyProgram() {
...
frame = new JFrame();
JMenuBar mainMenu = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.add(new JMenuItem("Open..."));
mainMenu.add(fileMenu); // adds a single JMenu to the menubar
frame.setJMenuBar(mainMenu); // adds the entire menubar to the window
...
frame.setVisible();
...
}
The only reason you would extend the JMenuBar class would be if you wanted to make a class that had additional functionality in terms of methods defined in your subclass, but that seems unlikely especially given the fact that you're just learning Swing.
The constructor for JMenuBar never takes any arguments. Also remember to use the new keyword when you instantiate (create an instance of) a new object. Consider using the following code:
JMenuBar mainMenu = new JMenuBar();
JMenu fileMenu = new JMenu("File");
mainMenu.add(fileMenu);
JMenuBar mainMenu = JMenuBar("Menu");
should be
JMenuBar mainMenu = new JMenuBar("Menu");
You forgot the new keyword. You must always use new when creating a new object with a constructor. Otherwise, Java will think that it is a method, which it is not.
Furthermore, if you look at the documentation here. you will find that JMenuBar's constructor does not take any arguments. Therefore, don't pass anything:
JMenuBar mainMenu = new JMenuBar();
Related
My menuBar isn't showing. Do I need the JPanel for it to show in my GUI?
private void buildCtrlPanel() {
ctrlPanel = new JPanel();
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
optionsMenu = new JMenu("Options");
JFrame frame = new JFrame();
frame.setJMenuBar(menuBar);
frame.setSize(350, 250);
frame.setVisible(true);
ctrlPanel.setLayout(new FlowLayout());
ctrlPanel.add(menuBar);
ctrlPanel.add(frame);
menuBar.add(fileMenu);
menuBar.add(optionsMenu);
}
You can only add a component to one container. You've added the JMenuBar appropriately to the JFrame -- fine, but then you also add it incorrectly to a JPanel (why?) one that uses a FlowLayout, layouts that don't work well with JMenuBars (again why?). Solution: don't do that. Add it to the JFrame as you're already doing, and leave it be.
You also seem to be adding a JFrame to a JPanel -- something that you shouldn't be doing, and again which suggests that you will want to go through the Swing tutorials before proceding further.
You can find links to the Swing tutorials and to other Swing resources here: Swing Info
The Swing menu tutorial can be found here: How to use Menus
So I have made a Jframe with a lot of elements and buttons and things in it, but I am new to using NetBeans. Upon creating the java application a main class.java was created and upon adding the jframe another jframe.java was created. How do I get the main class to open, read, and run my jframe.java? I can upload the specific code if need be.
Thanks in advance
To call a certain method from another class, you must first create a new object for that class, like this:
Jframe frame = new Jframe();
frame.setVisible(true); //or whatever the method is in jframe.class
Maybe rename the actual class name from jframe to something like frameone. I've heard that naming classes the same as classes in the Java API will cause trouble.
Or, you could put it all in one class, with either two separate methods or put it all in the main method. If this doesn't help, then please paste the exact code on pastebin.org and give a link.
Look at this sample example and learn how to set frame visible
import java.awt.*;
import javax.swing.*;
public class exp{
public static void main(String args[]){
JFrame jf=new JFrame("This is JFrame");
JPanel h=new JPanel();
h.setSize(100,100);
h.add(new JButton("Button"));
h.add(new JLabel("this is JLabel"));
h.setBackground(Color.RED);
jf.add(h);
jf.pack();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
Useful Links
Designing a Swing GUI in NetBeans IDE
Creating a GUI With Swing (As #MadProgrammer Commented)
Learning Swing with the NetBeans IDE
I'm new to this, but I got a form up. Woo hoo!
1) The project created my main function in japp1.java
2) I created a JFrame, file jfMain.java
3) While there was probably a way to reference it as it was, I didn't see how right away, so I moved it to a peer level with the japp1 file, both in a folder called japp1 which will cause them to get built together, having the same parent reference available.
src\
japp1\
japp1.java
jfMain.java
4) Then instead of creating a generic JFrame with a title, I created an instance of my class...
5) I gave it a size...
7) Then showed it...
public static void main(String[] args) {
// TODO code application logic here
JFrame frame = new japp1.jfMain();
frame.setPreferredSize(new Dimension(700, 500));
frame.pack();
frame.setVisible(true);
}
I had already put some code in my jframe... to show a messagedialog with JOptionPane from a mouseclick event on a button and set some text for some textfields.
Hope that helps.
Currently I have a very basic file viewer working as follows :
- in JOptionPane I browse for files, and set some variables to display (colors, line connecting etc)
- previous windows loads a frame with drawn points
alt text http://img190.imageshack.us/img190/4443/104bu.jpg
Code :
http://paste.pocoo.org/show/220066/
Now I'd like to throw it into one window, with JMenu for selecting files and changing display parameters. How to get started ? Should I rewrite everything to JDialog ?
alt text http://img684.imageshack.us/img684/5264/lab10db.jpg
If you want the JOPtionPane as a child of the main JFrame, then add it as a child. Of course it will then cover your dots. Hence you will have to not draw your dots directly in the content pane of the main JFrame, but rather in a new JPanel that you have also added to the JFRame's content pane. Let me know if I've understood the question whatsoever.
Here's some code for how I see the setup (I'm leaving the layout problem out of this, partly because it depends on what you want to see):
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(new Dimension(400,400));
frame.getContentPane().add(new JOptionPane());
JPanel canvasForDots = new JPanel();
frame.getContentPane().add(canvasForDots);
You might also like to look at How to Use Tool Bars and How to Use Menus. ImageApp is a typical implementation that associates menu items with the corresponding Action instances.
private class ClearAction extends AbstractAction {…}
private class ImageOpenAction extends AbstractAction {}
private Action openAction = new ImageOpenAction("Open");
private Action clearAction = new ClearAction("Clear");
…
JMenu menu = new JMenu("File");
menu.add(new JMenuItem(openAction));
menu.add(new JMenuItem(clearAction));
This related example adds the file chooser directly to the main frame. Here's a more elaborate example of connecting lines and shapes using the same principles.
So i have made a simple program with a basic menu at the top of the frame, Now i just need to put actions behind each JMenuItem. Im struggling to work the code out though, Here is what i thought would work:
JMenu file_Menu = new JMenu("File");
JMenuItem fileExit = new JMenuItem("Exit Program");
file_Menu.add(fileExit);
fileExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFrame hello = new JFrame("POPUP");
hello.setSize(100,75);
hello.setDefaultCloseOperation(hello.EXIT_ON_CLOSE);
hello.setVisible(true);
}
});
main_Menu.add(file_Menu);
This doesn't seem to work though, I thought that this code would create a small popup window when the menu item is clicked.
Can any spot the bug because i cant seem to.
Suggestion: Instead of adding a separate ActionListener, just use AbstractAction:
JMenuItem fileExit = new JMenuItem(new AbstractAction("Exit Program") {
public void actionPerformed(ActionEvent ae) {
JFrame hello = new JFrame("POPUP");
hello.setSize(100,75);
hello.setDefaultCloseOperation(hello.EXIT_ON_CLOSE);
hello.setVisible(true);
}
});
I'd also suggest, instead of setting EXIT_ON_CLOSE on the popup menu, you set it on the main frame of your application, and have the action simply call theMainFrame.dispose().
You got it working, but you have another problem.
Don't do this:
hello.setDefaultCloseOperation(hello.EXIT_ON_CLOSE);
When you close the pop-up frame, your entire JVM terminates. Consult JFrame.setDefaultCloseOperation javadocs for a more appropriate value.
Give an instance of Action (extend from AbstractAction) to JMenuItem
Based on the code you posted it looks like it should work, but we can't see the entire context of how the menu item is being used.
Did you debug your code (with a System.out.println) to see if the ActionListener is being invoked?
If you need more help post your SSCCE that demonstrates the problem.
Fixed it.
Forgot to add the actionPerformed method.
I would like to assign a mnemonic to a JMenu using resource bundles (or the ResourceMap). So for example, the code without resource file would be...
JMenu fileMenu = new JMenu();
fileMenu.setText("File"); // this would be read from a resource file
fileMenu.setMnemonic('F'); // but the docs say this is obsolete
fileMenu.setMnemonic(KeyEvent.VK_F);
So how do I put the KeyEvent.VK_F in a resource file?
For a JMenuItem I can do it with actions, but this is JMenu.
Java's javax.swing.KeyStroke class bridges the gap:
JMenu fileMenu = new JMenu();
String mnemonic = // string from localization
fileMenu.setMnemonic(KeyStroke.getKeyStroke(mnemonic).getKeyCode());
Accelerators are not supported for JMenus, only for JMenuItems (which makes sense, since these invoke an action without using the menu at all).
Inside the resource file use the accelerator
add.Action.accelerator = control A
You could do it in a similar way, and treat "FileMenu" as a (fake) action?