JApplet menus are not visible - java

I'm a newbie for java and learning for past two months from a book. I've tried a JApplet Menu program from the book.
MenuDemo.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MenuDemo implements ActionListener {
JLabel jlab;
MenuDemo() {
JFrame jfrm = new JFrame("Menu Demo");
jfrm.setLayout(new FlowLayout());
jfrm.setSize(220, 200);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jlab = new JLabel();
JMenuBar jmb = new JMenuBar();
JMenu jmFile = new JMenu("File");
JMenuItem jmiOpen = new JMenuItem("Open");
JMenuItem jmiClose = new JMenuItem("Close");
JMenuItem jmiSave = new JMenuItem("Save");
JMenuItem jmiExit = new JMenuItem("Exit");
jmFile.add(jmiOpen);
jmFile.add(jmiClose);
jmFile.add(jmiSave);
jmFile.addSeparator();
jmFile.add(jmiExit);
jmb.add(jmFile);
JMenu jmOptions = new JMenu("Options");
JMenu jmColors = new JMenu("Colors");
JMenuItem jmiRed = new JMenuItem("Red");
JMenuItem jmiGreen = new JMenuItem("Green");
JMenuItem jmiBlue = new JMenuItem("Blue");
jmColors.add(jmiRed);
jmColors.add(jmiGreen);
jmColors.add(jmiBlue);
jmOptions.add(jmColors);
JMenu jmPriority = new JMenu("Priority");
JMenuItem jmiHigh = new JMenuItem("High");
JMenuItem jmiLow = new JMenuItem("Low");
jmPriority.add(jmiHigh);
jmPriority.add(jmiLow);
jmOptions.add(jmPriority);
JMenuItem jmiReset = new JMenuItem("Reset");
jmOptions.addSeparator();
jmOptions.add(jmiReset);
jmb.add(jmOptions);
JMenu jmHelp = new JMenu("Help");
JMenuItem jmiAbout = new JMenuItem("About");
jmHelp.add(jmiAbout);
jmb.add(jmHelp);
jmiOpen.addActionListener(this);
jmiClose.addActionListener(this);
jmiSave.addActionListener(this);
jmiExit.addActionListener(this);
jmiRed.addActionListener(this);
jmiGreen.addActionListener(this);
jmiBlue.addActionListener(this);
jmiHigh.addActionListener(this);
jmiLow.addActionListener(this);
jmiReset.addActionListener(this);
jmiAbout.addActionListener(this);
jfrm.add(jlab);
jfrm.setJMenuBar(jmb);
jfrm.setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
String comStr = ae.getActionCommand();
if (comStr.equals("Exit"))
System.exit(0);
jlab.setText(comStr + " Selected");
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MenuDemo();
}
});
}
}
But it gives the below exception.
load: MenuDemo is not public or has no public constructor.
java.lang.IllegalAccessException: Class sun.applet.AppletPanel can not access a
member of class MenuDemo with modifiers ""
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:102)
at java.lang.Class.newInstance(Class.java:436)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:799)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:728)
at sun.applet.AppletPanel.run(AppletPanel.java:378)
at java.lang.Thread.run(Thread.java:745)
So I changed first three lines from class as below:
public class MenuDemo extends JApplet implements ActionListener {
JLabel jlab;
public void MenuDemo() {
Now the applet window is visible but without menus. As a newbie how can I resolve it.
Thank you.

A constructor does not have a return type. Remove void from the following line.
EDIT
Class definition:
public class MenuDemo extends JApplet implements ActionListener {
Change public MenuDemo() to public void init()
Create two functions in your class as follows.
public void start(){}
public void stop(){}
change
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
to
jfrm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

Related

How to add Action Listener from another class java?

I want to develop a very basic application, well here is the code, I have 4 classes one is the main class, the second one is JMenuBar, the third one is Panel and the fourth one is for Action Listener. at the moment my problem is here how to add instance of Action Listener for class of JMenuBar, till i could do some action.
public class My_Action implements ActionListener {
public My_Action() {
}
#Override
public void actionPerformed(ActionEvent actionEvent) {
}
}
public class My_Menu extends JMenuBar {
private My_Action my_action = new My_Action();
JMenu file;
JMenu Edit;
JMenu help;
public My_Menu() {
file = new JMenu("File");
Edit = new JMenu("Edit");
help = new JMenu("help");
JMenuItem item1 = new JMenuItem("file");
JMenuItem item2 = new JMenuItem("New");
JMenuItem item3 = new JMenuItem("Setting");
JMenuItem item4 = new JMenuItem("Color");
JMenuItem item5 = new JMenuItem("Print");
JMenuItem item6 = new JMenuItem("Exit");
file.add(item1);
file.add(item2);
file.add(item3);
file.add(item4);
file.add(item5);
file.add(item6);
file.addSeparator();
this.add(file);
this.add(Edit);
this.add(help);
item1.addActionListener(my_action);
}
public void actionPerformed(ActionEvent actionEvent){
System.exit(0);
}
}
This is my test program for you.
Check the console output when executing the menu program.
package just.test;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class My_Menu extends JMenuBar implements ActionListener {
private static final long serialVersionUID = 1L;
private My_Action my_action = new My_Action();
JMenu file;
JMenu Edit;
JMenu help;
public My_Menu() {
file = new JMenu("File");
Edit = new JMenu("Edit");
help = new JMenu("help");
this.add(file);
this.add(Edit);
this.add(help);
JMenuItem item1 = new JMenuItem("file");
item1.setActionCommand("file");
JMenuItem item2 = new JMenuItem("New");
item2.setActionCommand("New");
JMenuItem item3 = new JMenuItem("Setting");
item3.setActionCommand("Setting");
JMenuItem item4 = new JMenuItem("Color");
item4.setActionCommand("Color");
JMenuItem item5 = new JMenuItem("Print");
item5.setActionCommand("Print");
JMenuItem item6 = new JMenuItem("Exit");
item6.setActionCommand("Exit");
JMenuItem item7 = new JMenuItem("edit1");
item7.setActionCommand("edit1");
JMenuItem item8 = new JMenuItem("edit2");
item8.setActionCommand("edit2");
JMenuItem item9 = new JMenuItem("about..");
item9.setActionCommand("about");
file.add(item1);
file.add(item2);
file.addSeparator();
file.add(item3);
file.add(item4);
file.add(item5);
file.add(item6);
Edit.add(item7);
Edit.add(item8);
help.add(item9);
item1.addActionListener(my_action);
item2.addActionListener(my_action);
item3.addActionListener(my_action);
item4.addActionListener(my_action);
item5.addActionListener(my_action);
item7.addActionListener(this);
item8.addActionListener(this);
item9.addActionListener(this);
file.addActionListener(this);
Edit.addActionListener(this);
help.addActionListener(my_action);
}
#Override
public void actionPerformed(ActionEvent actionEvent) {
String command = actionEvent.getActionCommand();
System.out.println("----1----\n" + command);
//System.exit(0);
}
class My_Action implements ActionListener {
public My_Action() {
}
#Override
public void actionPerformed(ActionEvent actionEvent) {
String command = actionEvent.getActionCommand();
System.out.println("----2----\n" + command);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame f = new JFrame();
JPanel jp = new JPanel();
jp.setLayout(new BorderLayout());
f.add(new My_Menu(), BorderLayout.NORTH);
f.add(jp, BorderLayout.CENTER);
jp.setSize(500,400);
f.setSize(800, 600);
f.setVisible(true);
//f.pack();
}
});
}
}
You can just add a addActionListener to the component in which you have to send a message.
item1.addActionListener(my_action);
or
item7.addActionListener(this);
Don't forget to implement a ActionListener on your My_Menu class.
What you have is right.
It should call action performed when you click on file menu item. Just put a print ln in that method to see it being called.

Java ActionListener not listening to event

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 menu item enabling within event listener

Hello im trying to enable my JMenuItem from within an event listener but it seems to be out of scope. im new to java so how would i properly go about this. the said event listener will change to a new view and enable the disabled menu items.
//Create and add MenuItems
JMenuItem fileItem0 = new JMenuItem("Load");
collMenu.add(fileItem0);
JMenuItem fileItem1 = new JMenuItem("Add");
fileItem1.setEnabled(false);
collMenu.add(fileItem1);
JMenuItem fileItem2 = new JMenuItem("Delete");
fileItem2.setEnabled(false);
collMenu.add(fileItem2);
//Add Menu bar to frame
menubar.add(collMenu);
//Menu Item Functions
fileItem0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
JOptionPane.showMessageDialog(null,"You selected: Load.");
//Enable fileitem1 & 2 here ??
}
});
I hope this small example will help you clear your doubts...
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MenuExample extends JFrame {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
JMenu menu1 = new JMenu("Edit");
JMenuItem item1 = new JMenuItem("New");
JMenuItem item2 = new JMenuItem("Open");
public MenuExample() {
setJMenuBar(menuBar);
setVisible(true);
setSize(400, 200);
menuBar.add(menu);
menuBar.add(menu1);
menu.add(item1);
menu.add(item2);
item1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
}
});
item2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
}
});
}
public static void main(String[] args) {
MenuExample ex = new MenuExample();
}
}
Don't declare these menu items (fileitem1 and fileitem2) in the same method. Just Declare your fileitem1 and fileitem2 outside of your method.
This solves your problem...
Delcare the JMenuItems you are trying to access as final to be accessed from within an inner class (addActionListener(new ActionListener() {...}).
Inner class needs JMenuItems accessed within ActionListener
to be final. To avoid strange side-effects with closures in java
variables referenced by an anonymous delegates.
Here is an example:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Test {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Test().createAndShowUI();
}
});
}
private void createAndShowUI() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initComponents(frame);
frame.pack();
frame.setVisible(true);
}
private void initComponents(JFrame frame) {
JMenuBar menuBar = new JMenuBar();
JMenu menu1 = new JMenu("File");
JMenuItem itemLoad = new JMenuItem("Load");
//delcare them as final to be accessed from within an inner class
final JMenuItem itemAdd = new JMenuItem("Add");
final JMenuItem itemDel = new JMenuItem("Del");
itemAdd.setEnabled(false);
itemDel.setEnabled(false);
itemLoad.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
itemAdd.setEnabled(true);
itemDel.setEnabled(true);
}
});
menu1.add(itemLoad);
menu1.add(itemAdd);
menu1.add(itemDel);
menuBar.add(menu1);
frame.setJMenuBar(menuBar);
}
}

How to enable copy/cut/paste jMenuItem

I am making text editor in netbeans and have added jMenuItems called Copy,Cut & Paste in the Edit menu.
How do I enable these buttons to perform these functions after actionPerformed()
Here is my attempt:
private void CopyActionPerformed(java.awt.event.ActionEvent evt) {
JMenuItem Copy = new JMenuItem(new DefaultEditorKit.CopyAction());
}
private void PasteActionPerformed(java.awt.event.ActionEvent evt) {
JMenuItem Paste = new JMenuItem(new DefaultEditorKit.PasteAction());
}
private void CutActionPerformed(java.awt.event.ActionEvent evt) {
JMenuItem Cut = new JMenuItem(new DefaultEditorKit.CutAction());
}
simple editor example with cut ,copy ,paste:
public class SimpleEditor extends JFrame {
public static void main(String[] args) {
JFrame window = new SimpleEditor();
window.setVisible(true);
}
private JEditorPane editPane;
public SimpleEditor() {
editPane = new JEditorPane("text/rtf","");
JScrollPane scroller = new JScrollPane(editPane);
setContentPane(scroller);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
setSize(600,500);
JMenu editMenu = new JMenu("Edit");
Action cutAction = new DefaultEditorKit.CutAction();
cutAction.putValue(Action.NAME, "Cut");
editMenu.add(cutAction);
Action copyAction = new DefaultEditorKit.CopyAction();
copyAction.putValue(Action.NAME, "Copy");
editMenu.add(copyAction);
Action pasteAction = new DefaultEditorKit.PasteAction();
pasteAction.putValue(Action.NAME, "Paste");
editMenu.add(pasteAction);
bar.add(editMenu);
}
}
Hope this helps!
JEditorPane edit=... your instance;
Then use one of the
edit.cut();
edit.copy();
edit.paste();

Problem with submenu in java

My code works correctly, except for the submenu. The "Import" button is supposed to expand into newsfeed, bookmarks, and mail. However, the program doesn't even display "Import". It displays the first submenu entry ("newsfeed") which cannot be hovered. What am I doing wrong?
import javax.swing.*;
import java.awt.event.*;
public class test extends JFrame{
private static final long serialVersionUID = 1L;
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
test ex = new test();
ex.setVisible(true);
}
});
}
public test()
{
initGUI();
}
public final void initGUI()
{
JMenuBar menubar = new JMenuBar();
ImageIcon exitIcon = new ImageIcon("icons/exit.png");
ImageIcon openIcon = new ImageIcon("icons/open.png");
ImageIcon newIcon = new ImageIcon("icons/new.png");
ImageIcon saveIcon = new ImageIcon("icons/save.png");
JMenu file = new JMenu("File");
JMenuItem importMenu = new JMenuItem("Import");
importMenu.setMnemonic(KeyEvent.VK_M);
JMenuItem newsfeedMenu = new JMenuItem("Import newsfeed list...");
JMenuItem bookmarksMenu = new JMenuItem("Import bookmarks...");
JMenuItem mailMenu = new JMenuItem("Import mail...");
importMenu.add(newsfeedMenu);
importMenu.add(bookmarksMenu);
importMenu.add(mailMenu);
JMenuItem newMenu = new JMenuItem("New", newIcon);
newMenu.setMnemonic(KeyEvent.VK_N);
newMenu.setToolTipText("Start new document");
JMenuItem openMenu = new JMenuItem("Open", openIcon);
openMenu.setMnemonic(KeyEvent.VK_O);
openMenu.setToolTipText("Open document");
JMenuItem saveMenu = new JMenuItem("Save", saveIcon);
saveMenu.setMnemonic(KeyEvent.VK_S);
saveMenu.setToolTipText("Save document");
JMenuItem exitMenu = new JMenuItem("Exit", exitIcon);
exitMenu.setMnemonic(KeyEvent.VK_X);
exitMenu.setToolTipText("Exit application");
exitMenu.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
System.exit(0);
}
});
exitMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,
KeyEvent.CTRL_MASK));
file.add(newMenu);
file.add(openMenu);
file.add(saveMenu);
file.addSeparator();
file.add(importMenu);
file.addSeparator();
file.add(exitMenu);
menubar.add(file);
setJMenuBar(menubar);
JPanel panel = new JPanel();
getContentPane().add(panel);
panel.setLayout(null);
JButton button = new JButton("Quit");
button.setBounds(100,60,80,40);
button.setToolTipText("Press");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event){
System.exit(0);
}
});
panel.add(button);
setSize(300, 200);
setTitle("testGUI");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Change it to a JMenu
JMenu importMenu = new JMenu("Import");

Categories

Resources