0

I am creating a little combobox menu for my ap comp sci project. This is my first time working with GUI so I am still familiarizing myself with it. I have a menu class and a few other classes that run mostly void methods to do simple things like calculate GPA, etc. My menu is working fine (at least I think so). However, when I select my first item in the menu (I only have one actionlistener at the moment but I will add more after) the actionlistener runs the void method (run()) and it opens the window. But after that nothing happens. I have user input in that method to type in grade percentages, etc, and it works when i run the method directly from the class. But I can't type or seem to have any input when the actionlistener runs it. Thank you for your time and help!

 package First_Semester_Project;
 import java.awt.Color;
 import javax.swing.*;
  import java.awt.*;
 import java.awt.event.*;

public class Menu extends GradeCalculator
 {
String [] info = {"Grade Calculator", "Spanish Conjugator", "Period Table of    Elements", "Sentence Compiler"};
JLabel l= new JLabel("Welcome to the Resource Library!"); 
JComboBox c = new JComboBox(info);
JButton b = new JButton ("Select Resource");

public Menu () 
{
    frame();
}

public void frame () 
{
    JFrame f = new JFrame ("Resource Library");
   l.setForeground(Color.white);
   l.setFont(new Font("Arial", Font.BOLD, 16));


    f.setSize(600,90);
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel p = new JPanel(); 
    p.setBackground(Color.gray);
    p.add(c);
    p.add(b);
    p.add(l);
    f.add(p);

           b.addActionListener(new ActionListener()
           {
            public void actionPerformed (ActionEvent e ){
                String s = c.getSelectedItem().toString();
                //l.setText(s);
                if (s.equals("Grade Calculator"))
                {
                    run();
                }
                else if (s.equals("travel time"))
                {
                   l.setText(s); 
                }

            }

           });
    }

1 Answers1

0

Im going to assume that the run method can be called from the class as its superclass GradeCalculator has the run() method.

A common thing that i like to do is create my own actionlistener class, so that i can easily follow my code and see who has access to what, this would be my solution.

b.addActionListener(new myCustomActionListener(this)); //replaces b.actionlistener

now i would add myCustomActionListener either to the end of the same file, or in a different class file alltogether - depends on your abstraction level really.

here is the class for reference:

public class  myCustomActionListener implements ActionListener
{

    Menu menu = null;

    public myCustomActionListener(Menu menu)
    {
        this.menu = menu;
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        String s = menu.c.getSelectedItem().toString();
        //l.setText(s);
        if (s.equals("Grade Calculator"))
        {
            menu.run();
        }
        else if (s.equals("travel time"))
        {
           menu.l.setText(s); 
        }
    }

}
Sowry
  • 105
  • 1
  • 10
  • okay so i tried it and the void method is still not accepting any user input. and i am also getting a compiler warning saying that it uses unsafe operations. – user3204894 Jan 17 '14 at 17:50
  • Sorry about the late reply, Could you put up any more code, or a link to your project so I can see what is wrong? – Sowry Jan 20 '14 at 20:14