Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By xaymacan

Thread: JButton and ActionListener (action listener in different class/.java file).

  1. #1
    xaymacan is offline Member
    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0

    Default JButton and ActionListener (action listener in different class/.java file).

    Okay I am having some trouble tying two classes together, my actual class with the GUI and my class with the ActionListener. I also have a main file which actually starts the Gui through instansiating the class in a seperate class which I call main. This third class actually sets the gui as visible and that is it.

    // This is my interface coding
    public class sampGuiInter extends JFrame {
    // Variables
    JButton but1;

    // Constructor
    public sampGuiInter() {
    super("Sample Gui");
    setSize(300,300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout());

    // Adding the other parts of the interface.
    but1 = new JButton("Button 1");
    but1.addActionListener(new butActionListener());
    add(but1);

    }
    } // End of Interface coding.




















    // Here is my actionlistener
    public class butActionListener extends sampGuiInter implements ActionListener {
    sampGuiInter interfac = new sampGuiInter();
    String command;

    // Processing.
    @Override
    public void actionPerformed(ActionEvent e) {Attachment 5480
    if(e.getSource() == but1) JOptionPane.showMessageDialog(null, "Button 1 was pressed!");
    }
    }

    For some reason I keep getting either a compiling error or a run time error which I have attached to this post.
    Last edited by xaymacan; 08-31-2013 at 06:19 AM.

  2. #2
    xaymacan is offline Member
    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0

    Default Re: JButton and ActionListener (action listener in different class/.java file).

    JButton and ActionListener (action listener in different class/.java file).-screenshot-2013-08-31-00-05-46.png

  3. #3
    xaymacan is offline Member
    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0

    Default Re: JButton and ActionListener (action listener in different class/.java file).

    Admins can you lock this thread I figured it out!

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: JButton and ActionListener (action listener in different class/.java file).

    Quote Originally Posted by xaymacan View Post
    I figured it out!
    And this being a forum, you will post your solution for the benefit of the community, won't you?

    Before that, please go through the Guide For New Members and BB Code List - Java Programming Forum - Learn Java Programming

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

  5. #5
    xaymacan is offline Member
    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0

    Default Re: JButton and ActionListener (action listener in different class/.java file).

    Yeah my bad! Here it is:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class calcGui extends JFrame {
      // Class Variables.
      private calcActionListener myListener;
      private JButton but1;
      private JButton but2;
      private JButton but3;
      private JButton but4;
      private JButton but5;
      private JButton but6;
      private JButton but7;
      private JButton but8;
      private JButton but9;
      private JButton but0;
      private JButton butpercent;
      private JButton butaddition;
      private JButton butsubtraction;
      private JButton butdivide;
      private JButton butmultiply;
      private JButton butdecimal;
      private JButton butequal;
      public JTextArea txtresult;
      GridLayout grid = new GridLayout(4,4);
      GridLayout numGrid = new GridLayout(3,1);
      FlowLayout flow = new FlowLayout();
      JPanel butPanel = new JPanel(grid);
      JPanel funButPanel = new JPanel(numGrid);
      JPanel txtPanel = new JPanel();
      double num1,num2,total;
    
      // Constructor.
    public calcGui() {
        super("Jermaine's Calculator");
        setSize(500,400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(new BorderLayout(0,5));
        myListener = new calcActionListener();
    
     // Setting up Buttons and adding them to pane.
      butpercent = new JButton("%");
      but1 = new JButton("1");
      but1.addActionListener(myListener);
      but2 = new JButton("2");
      but2.addActionListener(myListener);
      but3 = new JButton("3");
      but3.addActionListener(myListener);
      but4 = new JButton("4");
      but4.addActionListener(myListener);
      but5 = new JButton("5");
      but5.addActionListener(myListener);
      but6 = new JButton("6");
      but6.addActionListener(myListener);
      but7 = new JButton("7");
      but7.addActionListener(myListener);
      but8 = new JButton("8");
      but8.addActionListener(myListener);
      but9 = new JButton("9");
      but9.addActionListener(myListener);
      but0 = new JButton("0");
      butaddition = new JButton("+");
      butaddition.addActionListener(myListener);
      butsubtraction = new JButton("-");
      butmultiply = new JButton("*");
      butdivide = new JButton("/");
      butdecimal = new JButton(".");
      butequal = new JButton("=");
      butPanel.add(but7);
      butPanel.add(but8);
      butPanel.add(but9);
      butPanel.add(butmultiply);
      butPanel.add(but4);
      butPanel.add(but5);
      butPanel.add(but6);
      butPanel.add(butsubtraction);
      butPanel.add(but1);
      butPanel.add(but2);
      butPanel.add(but3);
      butPanel.add(butaddition);
      butPanel.add(but0);
      butPanel.add(butdecimal);
      butPanel.add(butequal); 
      butPanel.add(butdivide);
    
      // butPanel.setPreferredSize(new Dimension(90,30));
    
     // Adding basic math function buttons to the Gui.
    
    
    
     // Adding Label to Calc.
     txtresult = new JTextArea("Hello World!",6,45);
     txtresult.setFont(new Font("Arial", Font.BOLD, 10));
     txtPanel.setPreferredSize(new Dimension(35,70));
     txtresult.setEditable(false);
     txtPanel.add(txtresult);
    
     // Adding all the panes to main screen.
     add(butPanel, BorderLayout.CENTER);
     add(funButPanel, BorderLayout.EAST);	
     add(txtPanel, BorderLayout.NORTH);
    
      } // End of Construtor
    
    } // End of class
    Last edited by xaymacan; 09-05-2013 at 06:03 AM.

  6. #6
    xaymacan is offline Member
    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0

    Default Re: JButton and ActionListener (action listener in different class/.java file).

    I just created an instance of the class then just initialized it in the constructor. Then once I was ready to set the actionlistener I just put the instance of the class in it.

    Don't know if I explained it correctly.
    DarrylBurke likes this.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: JButton and ActionListener (action listener in different class/.java file).

    Using an array for the 1 .. 0 buttons should make the code considerably shorter and more readable.

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

  8. #8
    MrNice is offline Member
    Join Date
    Jan 2014
    Posts
    8
    Rep Power
    0

    Default Re: JButton and ActionListener (action listener in different class/.java file).

    Hey i got some trouble 2, i am new btw so, got gui in window builder and adding actions lisener in seperate class

    and here the problem:
    Java Code:
    package Options;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.EventObject;
    
    import javax.swing.JFrame;
    
    public  class OptionsListener implements ActionListener {
    	JFrame parent;
    
        public OptionsListener(JFrame parent) {
            this.parent = parent;
        }
    
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		Object btnExit;
    		//if( e.getActionCommand().equals("Exit"))
    		if(e.getSource() == btnExit)
    				{
    			parent.dispose();
    				}
    				
    		// TODO Auto-generated method stub
    		
    	}
    }
    Don't know what to do, it looks like Options listener dosen't see btnExit from option class. Hlp
    GetCommand works, but not get source, and i need it if i am going to use mutliplay buttons.

  9. #9
    MrNice is offline Member
    Join Date
    Jan 2014
    Posts
    8
    Rep Power
    0

    Default Re: JButton and ActionListener (action listener in different class/.java file).

    I manage to modify my code but still java dosen't se btnExit
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.EventObject;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public  class OptionsListener implements ActionListener {
    	JFrame parent;
    	private ArrayList<Object> Actions =new ArrayList<Object>();
    
        public OptionsListener(JFrame parent,ArrayList Actions) {
            this.parent = parent;
            this.Actions=Actions;
        }
    
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		//Object btnExit;
    		//if( e.getActionCommand().equals("Exit"))
    		if((JButton)e.getSource() == Actions.contains(btnExit))
    				{
    			parent.dispose();
    				}
    				
    		
    	}
    }
    Last edited by MrNice; 01-05-2014 at 03:13 PM.

Similar Threads

  1. Call action of JButton from another class
    By geckolux in forum AWT / Swing
    Replies: 5
    Last Post: 08-04-2014, 06:17 PM
  2. Replies: 6
    Last Post: 04-28-2013, 12:31 AM
  3. Action Listener listen to another class/ program
    By kevinn205 in forum New To Java
    Replies: 1
    Last Post: 12-09-2011, 10:59 PM
  4. Replies: 2
    Last Post: 11-09-2011, 06:24 PM
  5. Replies: 2
    Last Post: 09-05-2011, 12:38 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
  •