Results 1 to 3 of 3
  1. #1
    Droid_X is offline Member
    Join Date
    Jan 2013
    Location
    New Zealand
    Posts
    10
    Rep Power
    0

    Default Swing and EventListener

    So, I am making a user interface window and everything works fine but when I press my buttons nothing happens...

    (The actionPerformed method is at the bottom of the class.)

    Java Code:
    package app;
    
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    
    public class UserInterface extends JFrame implements ActionListener {
    	
    	private JPanel panel1;
    	private JTextArea textArea1;
    	private JScrollPane scroll1;
    	private JTextArea textArea2;
    	private JScrollPane scroll2;
    	private JPanel panel2;
    	private JButton but1;
    	private JButton but2;
    	
    	UserInterface(){
    		setTitle("Macro Thing");
    		setResizable(false);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(300,350);
    		setLocationRelativeTo(null);
    		
    		panel1 = new JPanel();
    		panel1.setSize(300,350);
    		textArea1 = new JTextArea();
    		textArea1.setWrapStyleWord(true);
    		scroll1 = new JScrollPane(textArea1);
    		scroll1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    		scroll1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    		scroll1.setPreferredSize(new Dimension(285,125));
    		panel1.add(scroll1,BorderLayout.CENTER);
    		add(panel1);
    		
    
    		textArea2 = new JTextArea();
    		textArea2.setWrapStyleWord(true);
    		scroll2 = new JScrollPane(textArea2);
    		scroll2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    		scroll2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    		scroll2.setPreferredSize(new Dimension(285,125));
    		panel1.add(scroll2,BorderLayout.CENTER);
    		
    		panel2 = new JPanel(new FlowLayout());
    		panel1.add(panel2);
    	
    		but1 = new JButton("Open File");
    		but1.setEnabled(true);
    		panel2.add(but1);
    		
    		but2 = new JButton("Run Bot");
    		but2.setEnabled(true);
    		panel2.add(but2);
    		
    		setVisible(true);
    	}
    	
    	public void actionPerformed(ActionEvent e) {
    		if (e.getSource() == but1 || e.getSource() == but2) {
    			but1.setText("HELLO");
    		}
    	}
    }

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

    Default Re: Swing and EventListener

    Moved from New to Java

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

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

    Default Re: Swing and EventListener

    Read the API for JButton and follow the link to the Oracle tutorial on How to Use Buttons ... where you can find how to add a listener to a button.

    Aside, learn to use meaningful, descriptive variable names. That may not matter much to you with a program that fits in one screen, but you're not going to be at that level forever. Also, setting the size of your panel1 is meaningless as the JFrame's BorderLayout will resize it anyways; and setting a FlowLayout to panel2 is redundant as that's the default layout for a JPanel.

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

Similar Threads

  1. EventListener for Vectors?
    By Tomtefaen in forum New To Java
    Replies: 4
    Last Post: 01-13-2011, 12:02 AM
  2. Netbeans, Editable JCombo and EventListener
    By mine0926 in forum NetBeans
    Replies: 0
    Last Post: 07-10-2010, 03:55 AM
  3. Replies: 7
    Last Post: 09-12-2009, 07:06 PM
  4. have an eventlistener change values
    By klmdb in forum New To Java
    Replies: 1
    Last Post: 03-20-2009, 12:42 AM
  5. map javax.swing.text.Element to javax.swing.text.View
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 07:02 PM

Posting Permissions

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