Dear friend, I have a main application that contains a Menu
and in each of this Menu
there is a JMenuItem
. I want when I click one of the JMenuItem
I should be able to open a new JFrame
that will do specific task. This JFrame
should be in a different class that implements ActionListener
, not in the same class that contains Main Method many example that I have seen in the internet and in http://stackoverflow.com does not give the solution in two different classes. When I try the same methods within the same class or as an inner class that implements ActionListener
it works but not like I said 2 different classes. The reason I need it like that is because there is a lot of JMenuItems
in the Menu
and each JMenuItem
handles a great deal of process. If I am going to place everything in one file then it is not Object Oriented Programming any more and it is going to be a very very long file. An example is shown below. However the bellow example did not work for me. Could some one point out what am I doing wrong. Thank in Advance.
Main class that implements main method.
public class SwendaEye{
public static void main(String[]args){
FrameandComp frame = new FrameandComp();
JFrame win;
win = frame.mainFrame();
JMenuBar bar;
bar = new JMenuBar();
win.setJMenuBar(bar);
JMenu swenda = new JMenu("SWENDAEYE");// adding Swenda menu to the bar.
bar.add(swenda);
JMenuItem open = new JMenuItem("Open");
swenda.add(open);
JMenuItem exit = new JMenuItem("Exit");
swenda.add(exit);
JMenu tools = new JMenu("Tools");// adding Tools menu to the bar.
bar.add(tools);
JMenuItem convertIP = new JMenuItem("Convert IP Address");
tools.add(convertIP);
JMenuItem convertDomain = new JMenuItem("Convert Domain Name");
tools.add(convertDomain);
convertDomain.addActionListener(new Domain());
win.setVisible(true);
}
}
This is the action Listener class separate from the above class
public class Domain implements ActionListener{
public void actionPerformed(ActionEvent e)
{
if("Convert Domain Name".equals(e.getActionCommand())){
JFrame awindow = new JFrame();
awindow.setSize(200,400);
awindow.getContentPane().setBackground(Color.DARK_GRAY);
awindow.setTitle("Convert");
awindow.setDefaultCloseOperation(1);
}
}
}
In this example I am only demonstrating the Convert Domain Name JMenuItem
.
and before you answer please do not tell me JOptionPane
because I basically need to do a lot of thing in this window like table, image and many more. thank again.