Results 1 to 6 of 6
  1. #1
    geckolux is offline Member
    Join Date
    Dec 2007
    Posts
    1
    Rep Power
    0

    Default Call action of JButton from another class

    HY

    i would like to ask if it is possible to perform the actions when you click a JButton(actions described in add action listener) from another class.
    So like interface.jButton.press smth like that.

    thx

    gecko

  2. #2
    Abdul Ajis is offline Member
    Join Date
    Dec 2007
    Posts
    12
    Rep Power
    0

    Default

    Possible

    implements action listener in the new class

    and give that referece in the button addActionListener

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

    Default Re: Call action of JButton from another class

    What about with getsource, canno't figure it out...
    JButton and ActionListener (action listener in different class/.java file).

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Call action of JButton from another class

    If you have a reference to the button you can do:

    Java Code:
    button.doClick();

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

    Default Re: Call action of JButton from another class

    I wanted to use hasmap, but dont know how to do it. I got gui in 1 file, i wanted to have a action listener in 2 file, and in other files imported functions in actionlistener.

    It look like i need references to all Jframe/dialog components.

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

    Default Re: Call action of JButton from another class

    Quote Originally Posted by camickr
    If you have a reference to the button you can do:
    Still i am frozen on my work and i don't know how to do it, i don't understand.
    How do u add a reference to button in a gui class, and how i should write counstuctor??

    Action listener
    PHP Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    
    public class Listener implements ActionListener{
    	JFrame parent;
    	int action;
    	
    	public Listener(JFrame parent, int action)
    	{
    	this.parent = parent;
    	this.action = action;
    	}
    
    	@Override
    	public void actionPerformed(ActionEvent event) {
    		JButton pressed = (JButton)event.getSource();
    		Object actionSource = event.getSource();
    		/*if (event.getActionCommand().equals("Exit"))
    		{
    			parent.dispose();
    			}*/
    		
    		if (pressed.equals(btnExit)){
    				//action==0){
    			
    			//actionSource.equals(btnExit);
    			parent.dispose();
    		}
    				//actionSource == btnExit){			
    		
    		// TODO Auto-generated method stub
    		
    		
    	}
    
    }



    Gui
    PHP Code:
    import java.awt.EventQueue;
    
    
    public class ConfigMain {
    
    	private JFrame frameMain;
    	private JTextField textField;
    	private JTextField textField_1;
    	private JTextField textField_2;
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					ConfigMain window = new ConfigMain();
    					window.frameMain.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the application.
    	 */
    	public ConfigMain() {
    		try {
    			UIManager
    					.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    		}
    
    		catch (Exception e) {
    			System.out.println("Unable to load Windows look and feel");
    		}
    		initialize();
    	}
    
    	/**
    	 * Initialize the contents of the frame.
    	 */
    	private void initialize() {
    		frameMain = new JFrame();
    		frameMain.setBounds(100, 100, 650, 400);
    		frameMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frameMain.getContentPane().setLayout(null);
    		
    		JPanel panel_1 = new JPanel();
    		panel_1.setBounds(0, 136, 634, 226);
    		frameMain.getContentPane().add(panel_1);
    		
    		JScrollPane scrollPane = new JScrollPane();
    		panel_1.add(scrollPane);
    		
    		JPanel panel = new JPanel();
    		panel.setBounds(0, 0, 634, 137);
    		frameMain.getContentPane().add(panel);
    		
    		JButton btnOpenFile = new JButton("Open File");
    		
    		JButton btnSave = new JButton("Save");
    		
    		JButton btnSaveAs = new JButton("Save As");
    		
    		JButton btnDeleteCurrentFile = new JButton("Delete Current File");
    		btnDeleteCurrentFile.setForeground(Color.RED);
    		
    		JButton btnNew = new JButton("New");
    		
    		JButton btnExit = new JButton("Exit");
    		btnExit.addActionListener(new Listener(frameMain,0));
    		
    		JButton btnAddMods = new JButton("Add Mods");
    		
    		JButton btnRemoveMods = new JButton("Remove Mods");
    		
    		JSpinner spinner = new JSpinner();
    		spinner.setModel(new SpinnerNumberModel(new Integer(3), null, null, new Integer(1)));
    		
    		textField_1 = new JTextField();
    		textField_1.setColumns(10);
    		
    		JComboBox comboBox = new JComboBox();
    		comboBox.setEditable(true);
    		
    		JLabel lblSerwer = new JLabel("Serwer:");
    		
    		JLabel lblGameversion = new JLabel("GameVersion:");
    		
    		textField_2 = new JTextField();
    		textField_2.setColumns(10);
    		
    		JLabel lblCurrentFile = new JLabel("Current file:");
    		
    		JButton btnCopy = new JButton("Copy");
    		GroupLayout gl_panel = new GroupLayout(panel);
    		gl_panel.setHorizontalGroup(
    			gl_panel.createParallelGroup(Alignment.LEADING)
    				.addGroup(gl_panel.createSequentialGroup()
    					.addContainerGap()
    					.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
    						.addGroup(gl_panel.createSequentialGroup()
    							.addComponent(btnAddMods, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
    							.addPreferredGap(ComponentPlacement.UNRELATED)
    							.addComponent(btnRemoveMods, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
    							.addGap(18)
    							.addComponent(spinner, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)
    							.addGap(357))
    						.addGroup(gl_panel.createSequentialGroup()
    							.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
    								.addGroup(gl_panel.createSequentialGroup()
    									.addComponent(btnNew, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
    									.addPreferredGap(ComponentPlacement.RELATED)
    									.addComponent(btnOpenFile, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
    									.addPreferredGap(ComponentPlacement.UNRELATED)
    									.addComponent(btnSave, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
    									.addPreferredGap(ComponentPlacement.UNRELATED)
    									.addComponent(btnSaveAs, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
    									.addPreferredGap(ComponentPlacement.UNRELATED))
    								.addGroup(gl_panel.createSequentialGroup()
    									.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
    										.addComponent(lblSerwer)
    										.addComponent(lblCurrentFile))
    									.addPreferredGap(ComponentPlacement.RELATED)
    									.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
    										.addComponent(textField_2, GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE)
    										.addComponent(textField_1, GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE))))
    							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
    								.addGroup(gl_panel.createSequentialGroup()
    									.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
    										.addGroup(gl_panel.createSequentialGroup()
    											.addPreferredGap(ComponentPlacement.RELATED)
    											.addComponent(btnCopy, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)
    											.addPreferredGap(ComponentPlacement.UNRELATED)
    											.addComponent(btnExit, GroupLayout.DEFAULT_SIZE, 90, GroupLayout.PREFERRED_SIZE))
    										.addGroup(gl_panel.createSequentialGroup()
    											.addGap(18)
    											.addComponent(lblGameversion)
    											.addPreferredGap(ComponentPlacement.UNRELATED)
    											.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, 129, GroupLayout.PREFERRED_SIZE)))
    									.addGap(36))
    								.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
    									.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    									.addComponent(btnDeleteCurrentFile)
    									.addGap(66))))))
    		);
    		gl_panel.setVerticalGroup(
    			gl_panel.createParallelGroup(Alignment.LEADING)
    				.addGroup(gl_panel.createSequentialGroup()
    					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
    						.addComponent(btnSaveAs)
    						.addComponent(btnSave)
    						.addComponent(btnOpenFile)
    						.addComponent(btnNew)
    						.addComponent(btnExit)
    						.addComponent(btnCopy))
    					.addPreferredGap(ComponentPlacement.RELATED)
    					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
    						.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
    						.addComponent(lblCurrentFile)
    						.addComponent(btnDeleteCurrentFile))
    					.addPreferredGap(ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
    					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
    						.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
    						.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
    						.addComponent(lblSerwer)
    						.addComponent(lblGameversion))
    					.addGap(18)
    					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
    						.addComponent(btnRemoveMods)
    						.addComponent(btnAddMods)
    						.addComponent(spinner, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE))
    					.addGap(27))
    		);
    		panel.setLayout(gl_panel);
    		
    		
    									
    						
    	}
    }

    I need how do i change or accces fields from another class on gui window...
    I WANA be able to use "btnOpenFile, btnSave, btnSaveAs" and ext. in action listener class.
    HELP.
    Last edited by MrNice; 08-04-2014 at 07:22 PM.

Similar Threads

  1. action class
    By jayagowri in forum Web Frameworks
    Replies: 1
    Last Post: 05-07-2008, 04:23 PM
  2. Action class in Struts
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-27-2007, 11:05 AM
  3. Few action in one Jbutton
    By kubiasty in forum New To Java
    Replies: 0
    Last Post: 07-25-2007, 11:19 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
  •