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.
Re: Need some info on menus
Do you mean a combo box or a menu? Here is an example if a menu is what you want :
Code:
import java.awt.*;
import java.awt.event.*;
public class TestMenu extends Frame {//implements ActionListener{
public static void main(String args[]) {
TestMenu T=new TestMenu("Window title");
T.pack();
T.show();
}
public TestMenu(String title) {
super(title);
setLayout(new BorderLayout());
Button bAdd=new Button("Button");
add(bAdd);
FileMenu fileMenu = new FileMenu(this);
MenuBar mb = new MenuBar();
mb.add(fileMenu);
setMenuBar(mb);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void exit() {
setVisible(false);
dispose();
System.exit(0);
}
}
class FileMenu extends Menu implements ActionListener {
TestMenu TM; // who owns us?
public FileMenu(TestMenu tm) {
super("File");
TM = tm;
MenuItem mi;
add(mi = new MenuItem("Exit"));
mi.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String item = e.getActionCommand();
if (item.equals("Exit")) {
TM.exit();
} else {
System.out.println("Selected FileMenu " + item);
}
}
}
Re: Need some info on menus
Maybe that's what he meant!
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.
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.
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
Quote:
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);
}
}
Re: Need some info on menus
you didn't implement the actionListener?