|
-
Mar 2nd, 2005, 11:22 AM
#1
Thread Starter
Addicted Member
Need some info on menus
I am interested in learning how to do drop down menus in my Java GUI's. I just need some information before i get started
What classes are needing for drop down menus? How does Java respond to a menu selection? Does it use a ListSelectionListener or an ActionEventListener?
Can I see a very simple example of a drop down menu.
Last edited by ddmeightball; Mar 4th, 2005 at 12:23 PM.
Otaku no Kamisama – God of the Geeks
-
Mar 2nd, 2005, 04:12 PM
#2
Frenzied Member
Re: Need some info on menus
If you use Swing, which is what I would recommend, use JComboBox:
Code:
JComboBox jcb = new JComboBox();
jcb.addActionListener(this);
then to add stuff to it:
jcb.addItem("One");
jcb.addItem("Two");
in the actionPeformed method:
I would do this:
String strWhich = (String)jcb.getSelectedItem();
if (strWhich == "One")
{
//do something
}
I do have an example from a while back that converts currency, let me know if your interested in seeing it.
-
Mar 2nd, 2005, 07:29 PM
#3
-
Mar 2nd, 2005, 07:43 PM
#4
Frenzied Member
Re: Need some info on menus
Maybe that's what he meant!
-
Mar 2nd, 2005, 09:02 PM
#5
Fanatic Member
Re: Need some info on menus
Just for addition to Phillip's post. You can do it like this on swing
VB Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test extends JFrame{
JMenuBar mbar=new JMenuBar();
JMenu mnufile=new JMenu("File");
JMenuItem mnuexit=new JMenuItem("Exit");
public test(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300,300);
setJMenuBar(mbar);
mbar.add(mnufile);
mnufile.add(mnuexit);
mnuexit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
mnuexit_actionPerformed(e);
}
});
}
void mnuexit_actionPerformed(ActionEvent e){
System.exit(0);
}
public static void main(String[] args){
new test().setVisible(true);
}
}
Btw, show is deprecated, so refrain from using it. Also I created the mnuexit_actionPerformed on the class' scope. This is just for some use. You can access it from within the class.
-
Mar 4th, 2005, 12:09 PM
#6
Thread Starter
Addicted Member
Re: Need some info on menus
I wanted a menu, not a combo box. Kinda like the menu that we all have in our internet browsers. you know, FILE, EDIT, VIEW, etc.
Otaku no Kamisama – God of the Geeks
-
Mar 4th, 2005, 12:22 PM
#7
Thread Starter
Addicted Member
Re: Need some info on menus
Ok, i was tinkering around with this and I wanted to just make an ActionPerformed method, not an abstract method. The problem comes up when i try to add the actionlistener to my exit JMenuItem.
mnuexit.addActionListener(this);
I get this error
addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (TestMenu2)
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestMenu2 extends JFrame{
JMenuBar mbar=new JMenuBar();//creates the menu bar
JMenu mnufile=new JMenu("File");//creates the file selection of the menu bar
JMenuItem mnuexit=new JMenuItem("Exit");//creates the options for the file menu
JMenu mnuEdit = new JMenu("Edit");//create the edit selection of the menu bar
JMenuItem mnuCopy=new JMenuItem("Copy");//creates the options for the file menu
public TestMenu2(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300,300);
setJMenuBar(mbar);
mbar.add(mnufile);//adds "File"
mnufile.add(mnuexit);//adds "Exit" option for the file selection
mbar.add(mnuEdit);//adds "Edit"
mnuEdit.add(mnuCopy);//adds copy to Edit
mnuexit.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()=="Exit"){
System.exit(0);
}
}
public static void main(String[] args){
new TestMenu2().setVisible(true);
}
}
Otaku no Kamisama – God of the Geeks
-
Mar 4th, 2005, 03:27 PM
#8
Frenzied Member
Re: Need some info on menus
you didn't implement the actionListener?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|