Results 1 to 2 of 2
  1. #1
    BariMutation is offline Member
    Join Date
    Oct 2010
    Posts
    22
    Rep Power
    0

    Default ActionListener w/ Dialog

    I've tried looking for this on Google, to no avail. Essentially, when someone presses the "Resize" button, I need a dialog to pop up saying "Increase by what factor?" with an input line. Obviously, the program needs to store this variable and use it later, so... how would I do this? So far, all I have is the very basic written for it.

    This is in my CreateButtons method;
    Java Code:
    JButton resizeButton = new JButton("Resize");
    resizeButton.setBounds(1024, 584, BUTTON_WIDTH, BUTTON_HEIGHT);
    resizeButton.addActionListener(this);
    add(resizeButton);
    And this is in my actionPerformed method that catches the event;
    Java Code:
    String buttonText = e.getActionCommand();
    if(buttonText.equals("Resize"))
    	{    
    	     //SOMETHING NEEDS TO GO HERE
    	}
    It's an if-statement because I have about 10 other buttons that aren't relevant to this question., in case you were wondering. Thanks for any help! :)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by BariMutation View Post
    I've tried looking for this on Google, to no avail. Essentially, when someone presses the "Resize" button, I need a dialog to pop up saying "Increase by what factor?" with an input line. Obviously, the program needs to store this variable and use it later, so... how would I do this? So far, all I have is the very basic written for it.

    This is in my CreateButtons method;
    Java Code:
    JButton resizeButton = new JButton("Resize");
    resizeButton.setBounds(1024, 584, BUTTON_WIDTH, BUTTON_HEIGHT);
    resizeButton.addActionListener(this);
    add(resizeButton);
    And this is in my actionPerformed method that catches the event;
    Java Code:
    String buttonText = e.getActionCommand();
    if(buttonText.equals("Resize"))
    	{    
    	     //SOMETHING NEEDS TO GO HERE
    	}
    It's an if-statement because I have about 10 other buttons that aren't relevant to this question., in case you were wondering. Thanks for any help! :)
    You really don't want to have a "switch-board" action listener, but instead give each JButton or like group of buttons their own ActionListener.

    As to your question, I'd use one of the JOptionPane dialogs. The Swing tutorial and API will show you what you need to do to accomplish this.

Similar Threads

  1. ActionListener+KeyListener
    By mandelbrot in forum AWT / Swing
    Replies: 5
    Last Post: 09-10-2010, 01:25 AM
  2. Determining ActionListener
    By siamino in forum New To Java
    Replies: 12
    Last Post: 05-26-2009, 12:04 AM
  3. ActionListener Error
    By blackstormattack in forum New To Java
    Replies: 1
    Last Post: 03-05-2009, 09:36 AM
  4. Java actionlistener help
    By justsomeguy in forum AWT / Swing
    Replies: 1
    Last Post: 05-27-2008, 06:42 AM
  5. ActionListener interface
    By tsantana in forum New To Java
    Replies: 2
    Last Post: 03-30-2008, 11:24 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
  •