Results 1 to 6 of 6
  1. #1
    Outsider418 is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Question Trouble with actionListener

    Good evening.

    I'm working on a payroll calculator. On the code I have my departments listed on a menu, and once I click on an department, a new window should come up. I have the second JFrame (the one that does the actual calculations) on a second file.

    My problem is that I am having trouble with the ActionListener, when I click on a department, I want the other method called. I just can't get it to work. I managed to get the action listener for my exit button to work, but I can't repeat my success with the other action listener. Any help will be appreciated.

    Here is the code from my main program with the menus.

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class PrescottWk2 extends JFrame
    {
    	private JPanel panel;
    	private JTextField nameTextField;
    	private JTextField deptTextField;
    	private JTextField salaryTextField;
    	private JButton calcButton;
    	private JLabel messageLabel;
    	private final int WINDOW_WIDTH = 300;
    	private final int WINDOW_HEIGHT = 300;
    	private JMenuBar menuBar;
    	private JMenu fileMenu;
    	private JMenu deptMenu;
    	private JMenuItem exitItem;
    	private JRadioButtonMenuItem autoprocessingItem;
    	private JRadioButtonMenuItem propertyprocessingItem;
    	private JRadioButtonMenuItem underwritingItem;
    	private JRadioButtonMenuItem claimsItem;
    	private JRadioButtonMenuItem retItem;
    
    public PrescottWk2()
    {
    	super("Payroll Calculator");
    
    	setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    	buildPanel();
    
    	add(panel);
    
    	setVisible(true);
    
    	buildMenuBar();
    
    
    }
    
    private void buildPanel()
    {
    	messageLabel = new JLabel("Choose Your Department From the Menu");
    
    	panel = new JPanel();
    
    	panel.add(messageLabel);
    
    }
    
    
    private void buildMenuBar()
    {
    	menuBar = new JMenuBar();
    
    	buildFileMenu();
    	builddeptMenu();
    
    
    	menuBar.add(fileMenu);
    	menuBar.add(deptMenu);
    
    
    	setJMenuBar(menuBar);
    }
    
    private void buildFileMenu()
    {
    	exitItem = new JMenuItem("Exit");
    	exitItem.setMnemonic(KeyEvent.VK_X);
    	exitItem.addActionListener(new ExitListener());
    
    	fileMenu = new JMenu("File");
    	fileMenu.setMnemonic(KeyEvent.VK_F);
    
    	fileMenu.add(exitItem);
    }
    
    private void builddeptMenu()
    {
    	autoprocessingItem = new JRadioButtonMenuItem("Auto Processing");
    	propertyprocessingItem = new JRadioButtonMenuItem("Property Processing");
    	underwritingItem = new JRadioButtonMenuItem("Underwriting");
    	claimsItem = new JRadioButtonMenuItem("Claims");
    	retItem = new JRadioButtonMenuItem("Ret");
    
    	ButtonGroup group = new ButtonGroup();
    	group.add(autoprocessingItem);
    	group.add(propertyprocessingItem);
    	group.add(underwritingItem);
    	group.add(claimsItem);
    	group.add(retItem);
    
    	deptMenu = new JMenu("Departments");
    	deptMenu.setMnemonic(KeyEvent.VK_D);
    
    	deptMenu.add(autoprocessingItem);
    	deptMenu.add(propertyprocessingItem);
    	deptMenu.add(underwritingItem);
    	deptMenu.add(claimsItem);
    	deptMenu.add(retItem);
    }
    
    
    
    
    private class ExitListener implements ActionListener
    {
    	public void actionPerformed(ActionEvent e)
    	{
    		System.exit(0);
    	}
    }
    
    
    
    
    
    
    public static void main(String[] args)
    {
    	new PrescottWk2();
    }
    }

    And here is the code for one of the actual payroll calculators...

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class AutoProcessing extends JFrame
    {
    	private JLabel nameL, deptL, hourlyrateL, hoursworkedL;
    	private JTextField nameTF, deptTF, hourlyrateTF, hoursworkedTF;
    
    	private JButton calcButton;
    
    	private static final int WIDTH = 400;
    	private static final int HEIGHT = 300;
    
    	double hours;
    	double rate;
    	double pay;
    	double overtimePay;
    	double payCheck;
    
    public AutoProcessing()
    {
    	nameL = new JLabel("Employee Name: ",SwingConstants.CENTER);
    	deptL = new JLabel("Department: ",SwingConstants.CENTER);
    	hourlyrateL = new JLabel("Hourly Rate: ",SwingConstants.CENTER);
    	hoursworkedL = new JLabel("Hours Worked For The Week: ", SwingConstants.CENTER);
    
    	nameTF = new JTextField(20);
    	deptTF = new JTextField(10);
    	hourlyrateTF = new JTextField(10);
    	hoursworkedTF = new JTextField(10);
    
    	calcButton = new JButton("Calculate");
    
    	calcButton.addActionListener(new CalcButtonListener());
    
    
    	setTitle("Payroll Calculator");
    
    	Container pane = getContentPane();
    
    	pane.setLayout(new GridLayout(9, 2));
    
    	pane.add(nameL);
    	pane.add(nameTF);
    	pane.add(deptL);
    	pane.add(deptTF);
    	pane.add(hourlyrateL);
    	pane.add(hourlyrateTF);
    	pane.add(hoursworkedL);
    	pane.add(hoursworkedTF);
    	pane.add(calcButton);
    
    	setSize(WIDTH, HEIGHT);
    	setVisible(true);
    	setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    
    
    private class CalcButtonListener implements ActionListener
    {
    	public void actionPerformed(ActionEvent e)
    	{
    		String str;
    
    		rate = Double.parseDouble(hourlyrateTF.getText());
    		hours = Double.parseDouble(hoursworkedTF.getText());
    
    		if (hours < 0|hours> 60)
    		{
    			JOptionPane.showMessageDialog(null, "Invalid Number");
    		}
    
    		if (rate < 6|rate > 150)
    		{
    			JOptionPane.showMessageDialog(null, "Invalid Number");
    		}
    
    		if (hours > 40)
    		{
    			pay = hours * rate;
    			overtimePay = (hours - 40) * (rate * 1.5);
    			payCheck = pay + overtimePay;
    			JOptionPane.showMessageDialog(null, "Your Pay With Overtime Is " + payCheck);
    		}
    		else
    		{
    			pay = hours * rate;
    			JOptionPane.showMessageDialog(null, "Your Pay Is " + pay);
    		}
    
    	}
    }
    
    
    
    public static void main(String[]args )
    {
    	new AutoProcessing();
    }
    }
    I can get them to work separately, just not together.

    Thanks for the help.

    -MP-
    Last edited by Outsider418; 05-07-2012 at 02:33 AM.

  2. #2
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

    Default Re: Trouble with actionListener

    Please edit your post to use CODE tags so we can read your code.

  3. #3
    Outsider418 is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Default Re: Trouble with actionListener

    Sorry...I'm new to the forums. How do I do that?

  4. #4
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

    Default Re: Trouble with actionListener

    Click "Edit Post" under your first post, click "Go advanced", highlight your source code and click the code button at the top (looks like "#").

  5. #5
    Outsider418 is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Default Re: Trouble with actionListener

    Okay...done :)

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Trouble with actionListener

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. ActionListener Help
    By rakosky in forum AWT / Swing
    Replies: 4
    Last Post: 04-06-2012, 04:59 PM
  2. trouble with actionlistener and null pointer exception.
    By flamewolf393 in forum New To Java
    Replies: 1
    Last Post: 04-04-2012, 02:13 AM
  3. Please help with actionlistener
    By ADustedEwok in forum New To Java
    Replies: 5
    Last Post: 12-08-2011, 11:04 PM
  4. ActionListener
    By jaylimix in forum Java Applets
    Replies: 5
    Last Post: 11-06-2011, 07:05 PM
  5. Please Help With ActionListener
    By Daman12 in forum New To Java
    Replies: 29
    Last Post: 10-26-2011, 08:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •