Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: JButton preforming an action

  1. #1
    Member
    Join Date
    Jan 2011
    Posts
    88
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default JButton preforming an action

    I have the JButtons already made and placed and i made a command for it but when i click still nothing happens.

    heres my command method
    	public void actionPreformed(ActionEvent evt) {
    		String cmd = evt.getActionCommand();
    		if (cmd != null) {
    			if (cmd.equalsIgnoreCase("algButton")) {
    	            getInput.userInput();
    	        }
    		}
    	}

    here's my entire GUI class

    package Algebra;
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    import Algebra.getInput;
     
    public class GUI{
     
    	//Create Frame
    	private JFrame f = new JFrame("Joshs' Algebra Calculator");
     
    	//Create Panels
    	private JPanel funcPnl = new JPanel();
    	private JTextField txtNotes = new JTextField();
     
    	//Buttons
    	private JButton algButton = new JButton("Equations");
    	private JButton areaButton = new JButton("Area");
     
     
    	//Create Menu
    	private JMenuBar mb = new JMenuBar();
    	private JMenu muFile = new JMenu("File");
    	private JMenuItem muItemQuite = new JMenuItem("Quit");
    	private JMenu muHelp = new JMenu("Help");
    	private JMenuItem muItemAbout = new JMenuItem("About");
     
    	public GUI(){		
    		//set menu
    		f.setJMenuBar(mb);
     
    		//build menu
    		muFile.add(muItemQuite);
    		muHelp.add(muItemAbout);
    		mb.add(muFile);
    		mb.add(muHelp);	
     
    		//Add Buttons
    		funcPnl.add(algButton);
    		funcPnl.add(areaButton);
     
    		//Set Up Frame
    		f.setIconImage(Toolkit.getDefaultToolkit()
    				  .getImage("icon_confused.gif"));
    		f.setSize(400,400);
    		f.getContentPane().setLayout(new BorderLayout());
    		f.getContentPane().add(funcPnl, BorderLayout.WEST);
    		f.getContentPane().add(txtNotes, BorderLayout.SOUTH);
     
     
    		// Allows the Swing App to be closed
            f.addWindowListener(new ListenCloseWdw());
     
    		//Add Menu listener
            muItemQuite.addActionListener(new ListenMenuQuit());
     
            //Buttons
        	algButton.setActionCommand("algButton");
    	}
     
        public void actionPreformed(ActionEvent evt) {
    		String cmd = evt.getActionCommand();
    		if (cmd != null) {
    			if (cmd.equalsIgnoreCase("algButton")) {
    	            getInput.userInput();
    	        }
    		}
    	}
     
        public class ListenMenuQuit implements ActionListener{
            public void actionPerformed(ActionEvent e){
                System.exit(0);         
            }
        }
     
        public class ListenCloseWdw extends WindowAdapter{
            public void windowClosing(WindowEvent e){
                System.exit(0);         
            }
        }
     
        public void launchFrame(){
            // Display Frame
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);
        }
     
     
     
        public static void main(String args[]){
            GUI gui = new GUI();
            gui.launchFrame();
            //gui.Action(ActionEvent);
            //getInput.userInput();
        }
    }

    I know its not complete but i just don't know were to go from here.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: JButton preforming an action

    Where do you add an action listener to the button? That is the connection needed for the button to be able to call your code. When it gets an ActionEvent, it calls the action listeners that have been added to it.

  3. #3
    Member
    Join Date
    Jan 2011
    Posts
    88
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: JButton preforming an action

    i just tried that and i got 2 errors
    package Algebra;
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    import Algebra.getInput;
     
    public class GUI{
    	//Create Frame
    	private JFrame f = new JFrame("Joshs' Algebra Calculator");
     
    	//Create Panels
    	private JPanel funcPnl = new JPanel();
    	private JTextField txtNotes = new JTextField();
     
    	//Buttons
    	private JButton algButton = new JButton("Equations");
    	private JButton areaButton = new JButton("Area");
     
     
    	//Create Menu
    	private JMenuBar mb = new JMenuBar();
    	private JMenu muFile = new JMenu("File");
    	private JMenuItem muItemQuite = new JMenuItem("Quit");
    	private JMenu muHelp = new JMenu("Help");
    	private JMenuItem muItemAbout = new JMenuItem("About");
     
    	public GUI(){		
    		//set menu
    		f.setJMenuBar(mb);
     
    		//build menu
    		muFile.add(muItemQuite);
    		muHelp.add(muItemAbout);
    		mb.add(muFile);
    		mb.add(muHelp);	
     
    		//Add Buttons
    		funcPnl.add(algButton);
    		funcPnl.add(areaButton);
     
    		//Set Up Frame
    		f.setIconImage(Toolkit.getDefaultToolkit()
    				  .getImage("icon_confused.gif"));
    		f.setSize(400,400);
    		f.getContentPane().setLayout(new BorderLayout());
    		f.getContentPane().add(funcPnl, BorderLayout.WEST);
    		f.getContentPane().add(txtNotes, BorderLayout.SOUTH);
     
     
    		// Allows the Swing App to be closed
            f.addWindowListener(new ListenCloseWdw());
     
    		//Add Menu listener
            muItemQuite.addActionListener(new ListenMenuQuit());
            algButton.addActionListener(new actionPreformed(evt));
            //Buttons
        	algButton.setActionCommand("Algebra");
    	}
     
    	public void actionPreformed(ActionEvent evt) {
    		String cmd = evt.getActionCommand();
    		if (cmd != null) {
    			if (cmd.equalsIgnoreCase("Algebra")) {
    	            getInput.userInput();
    	        }
    		}
    	}
     
        public class ListenMenuQuit implements ActionListener{
            public void actionPerformed(ActionEvent e){
                System.exit(0);         
            }
        }
     
        public class ListenCloseWdw extends WindowAdapter{
            public void windowClosing(WindowEvent e){
                System.exit(0);         
            }
        }
     
        public void launchFrame(){
            // Display Frame
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);
        }
     
     
     
        public static void main(String args[]){
            GUI gui = new GUI();
            gui.launchFrame();
            //gui.Action(ActionEvent);
            //getInput.userInput();
        }
    }

    error 1
    Severity and Description	Path	Resource	Location	Creation Time	Id
    actionPreformed cannot be resolved to a type	AlgebraCalculator/src/Algebra	GUI.java	line 57	1306982074386	276

    error 2
    Severity and Description	Path	Resource	Location	Creation Time	Id
    evt cannot be resolved	AlgebraCalculator/src/Algebra	GUI.java	line 57	1306982074386	277

  4. #4
    Member
    Join Date
    Jan 2011
    Posts
    88
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: JButton preforming an action

    fixed! thank you!
    Last edited by frozen java; June 1st, 2011 at 10:06 PM.

Similar Threads

  1. JButton with "Enter Key" keyboard action
    By chronoz13 in forum Java Swing Tutorials
    Replies: 2
    Last Post: March 14th, 2012, 06:49 AM
  2. Action button HELP?
    By utoptas in forum Java Theory & Questions
    Replies: 8
    Last Post: August 27th, 2010, 03:32 PM
  3. Action Listener
    By kray66 in forum AWT / Java Swing
    Replies: 2
    Last Post: April 19th, 2010, 03:26 PM
  4. jbutton and action event
    By mt888 in forum AWT / Java Swing
    Replies: 3
    Last Post: March 26th, 2010, 06:24 AM
  5. Need Help Action Listener....
    By vhizent23 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 9th, 2009, 01:46 PM

Tags for this Thread