Page 1 of 2 12 LastLast
Results 1 to 20 of 22
Like Tree1Likes

Thread: Cancel button ? In java

  1. #1
    smasm is offline Member
    Join Date
    Sep 2011
    Location
    US
    Posts
    16
    Rep Power
    0

    Question Cancel button ? In java

    Hi guys..

    How can I write the set a cancel button ?

    Ive two buttons ..

    One of them the cancel button .. If the user click the cancel button I want to show new message ( you have clicked the cancel button) and exit the program ..

    How can I write this code?

    Thank you

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

  3. #3
    smasm is offline Member
    Join Date
    Sep 2011
    Location
    US
    Posts
    16
    Rep Power
    0

    Default Re: Cancel button ? In java

    Thanks for replying..
    My English not good .. I'm trying to read and understand I can't
    Please do you know the code ?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Cancel button ? In java

    Do a Search on the forum for ActionListener for code samples
    sunde887 likes this.

  5. #5
    smasm is offline Member
    Join Date
    Sep 2011
    Location
    US
    Posts
    16
    Rep Power
    0

    Default Re: Cancel button ? In java

    Yes I did .. I couldn't find ..
    My English not good so I don't understand very fast
    Please I need the code before 11:59 tonight

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Cancel button ? In java

    Yes I did .. I couldn't find ..
    Strange. When I did a Search here it found hundreds of posts.
    Are you sure you spelled it correctly?

  7. #7
    smasm is offline Member
    Join Date
    Sep 2011
    Location
    US
    Posts
    16
    Rep Power
    0

    Default Re: Cancel button ? In java

    since you posted I tried .. I found but i could it run it in my program .. kind of complecated ..
    becuase most of the cases i find is pro and my program basic .. I need only one button
    and there is one hour left to due time to submit my program .. please guys help :(

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

    Default Re: Cancel button ? In java

    We'll be happy to help you, especially with any and all specific questions regarding your code, but your deadline is your problem, not ours, and certainly no one here is going to code this for you. Please show us what you've tried and ask any questions about any errors or problems you are having and we'll try to help.

  9. #9
    smasm is offline Member
    Join Date
    Sep 2011
    Location
    US
    Posts
    16
    Rep Power
    0

    Default Re: Cancel button ? In java

    Ok .. Thanks for the concern .. I'd like to see clue not codes .. I will be happy too .. I tried in youtube didn't fine .. I want lessen in video could not find too ... if you could give me a lessen in you tube .. thanks

  10. #10
    smasm is offline Member
    Join Date
    Sep 2011
    Location
    US
    Posts
    16
    Rep Power
    0

    Default Re: Cancel button ? In java

    Quote Originally Posted by Fubarable View Post
    We'll be happy to help you, especially with any and all specific questions regarding your code, but your deadline is your problem, not ours, and certainly no one here is going to code this for you. Please show us what you've tried and ask any questions about any errors or problems you are having and we'll try to help.
    Ooh sorry guys i feel so bad maybe i am not suppose to write in here like this post
    i just asked sorry again

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Cancel button ? In java

    Like said, we don't mind helping, and I'm sure I have(and others as well) posted code which does something on a button click. The general way in which you get a button to do something is to add an ActionListener. When the button is clicked it will fire an ActionEvent, which will be grabbed by the ActionListener, which will handle to event.

    Generally a button code will look something like this

    Java Code:
    JButton button = new JButton("Hello");
    button.addActionListener(new ActionListener(){
      @Override public void actionPerformed(ActionEvent e){
         //do stuff here
      }
    });
    This is a lot more than I should be giving you, but it should be a sufficient start. More searching will definitely find the correct answer.

  12. #12
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Cancel button ? In java

    Here is a link for JButton:
    How to Use Buttons, Check Boxes, and Radio Buttons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    Here is a link that might be helpful for you. Honestly, I visit this link very often. :)
    The Really Big Index

  13. #13
    smasm is offline Member
    Join Date
    Sep 2011
    Location
    US
    Posts
    16
    Rep Power
    0

    Default Re: Cancel button ? In java

    Thanks alot guys for helping .. this is my program .. I kind understand now .. but feel like there is a problem .. where is it ?

    Java Code:
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    
    
    public class Program3 {
            			
    	public static void main(String[] args) 
    	
    	{
    
    		// declare variables
    		int sum = 0;
    		int max=0, min= 0;
    		//  initialize it one time.
    		boolean firstIteration = true;
    
    
    		// the for loop
    		for ( int i=1; i<=4; i++)
    			
    		{
    			//each inputted number will stores here
    			String userInput = JOptionPane
    			    .showInputDialog("Enter an integer");
                // to converse the string to int
    			int value = Integer.parseInt(userInput);
    			       
    		    if(firstIteration)
    		    {
    				// the variables
    		        sum = value; 
    		    	max = value;
    		    	min = value;
    		    	firstIteration = false;
    		    	
    		    }
    		    else
    		    {
    		    	//add to sum 
    		    	sum += value; 
    		    } 
    
    		     //conditional statement
                //if inputted number is smaller than our min then exchange it.
    		    if(value < min)
    		    {
    		        min = value; 
    		    }
    		    
    		     //conditional statement
                //if inputted number is bigger than our max then exchange it.
    		    if(value > max)
    		    {
    		        max = value; 
    		    }
    	}
    		// calculating the averege by extracted the sum by 4
    		double averge = sum / 4.0;
    
    		   JOptionPane.showMessageDialog(null," Max is " + max + " \nMin is " + min + 
    				                      "\nSum is " + sum + " \nAverage is " + averge
    				               , "Program 3", JOptionPane.PLAIN_MESSAGE);
    
    
    	
    	JButton button = new JButton("cancel");
    	button.addActionListener(new ActionListener(){
    	
    
    
    			JButton button = new JButton();
    			button.setVisible(true);
    			button.setSize(250,250);
    			button.setDefaultCloseOperation(JButton.EXIT_ON_CLOSE);
    			
    			JPanel p = new JPanel();
    			JButton b1 = new JButton ("Action Listener");
    			button.addActionListener(new ActionListener(){
    				
    				public void actionPerformed(ActionEvent e)
    				{
    					JOptionPane.showMessageDialog(null, "cancel button clicked")
    				
    				
    			});
    			
    			p.add(b1);
    			button.add(p);
    		}
    	});
    		   
    			System.exit(0); // program end
    
        }	
    
    }
    Last edited by sunde887; 10-01-2011 at 05:09 AM.

  14. #14
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Cancel button ? In java

    You did not tell us what problem you are encountering.
    And its hard to read, use code tags instead.

    Looking at the code I saw that you declare button two(2) times. I think you do not intend to create new JButton when you click the "cancel" button, right?
    JButton button = new JButton("cancel");
    button.addActionListener(new ActionListener(){



    JButton button = new JButton();
    button.setVisible(true);
    button.setSize(250,250);
    button.setDefaultCloseOperation(JButton.EXIT_ON_CL OSE);

    JPanel p = new JPanel();
    JButton b1 = new JButton ("Action Listener");
    button.addActionListener(new ActionListener(){

    public void actionPerformed(ActionEvent e)
    {
    JOptionPane.showMessageDialog(null, "cancel button clicked")


    });

  15. #15
    smasm is offline Member
    Join Date
    Sep 2011
    Location
    US
    Posts
    16
    Rep Power
    0

    Default Re: Cancel button ? In java

    Cancel button ? In java-captur4e.jpgright .. I was thinking that why do I have to create a new button and I do have it .. the result of the program is attached .. I just need action for it

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

    Default Re: Cancel button ? In java

    You're using a JOptionPane.showInputDialog and this changes everything. In this case you must check what the JOptionPane method returns. If it is null, then the user pressed cancel, and then you show your other JOptionPane that states that the user pressed the cancel button. There's no need to mess with ActionListeners with this. Again, just check the userInput variable to see if it is null (if it == null).

  17. #17
    smasm is offline Member
    Join Date
    Sep 2011
    Location
    US
    Posts
    16
    Rep Power
    0

    Default Re: Cancel button ? In java

    Quote Originally Posted by Fubarable View Post
    You're using a JOptionPane.showInputDialog and this changes everything. In this case you must check what the JOptionPane method returns. If it is null, then the user pressed cancel, and then you show your other JOptionPane that states that the user pressed the cancel button. There's no need to mess with ActionListeners with this. Again, just check the userInput variable to see if it is null (if it == null).
    this is great ..

    you mean I can use something like this


    JOptionPane.CANCEL_OPTION(null,"Cancel was selected. Program will exit");
    should I put it in line 67 ?

  18. #18
    smasm is offline Member
    Join Date
    Sep 2011
    Location
    US
    Posts
    16
    Rep Power
    0

    Default Re: Cancel button ? In java

    Quote Originally Posted by Fubarable View Post
    You're using a JOptionPane.showInputDialog and this changes everything. In this case you must check what the JOptionPane method returns. If it is null, then the user pressed cancel, and then you show your other JOptionPane that states that the user pressed the cancel button. There's no need to mess with ActionListeners with this. Again, just check the userInput variable to see if it is null (if it == null).
    this is great ..

    you mean I can use something like this


    JOptionPane.CANCEL_OPTION(null,"Cancel was selected. Program will exit");
    should I put it in line 67 ?

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

    Default Re: Cancel button ? In java

    No, not quite. For one thing, JOptionPane.CANCEL_OPTION is an int and you can't call it like it's a method. I meant something more like this:

    Java Code:
          String test = JOptionPane.showInputDialog(null, "Enter Something");
          if (test == null) {
             // the user pressed cancel. Show a JOptionPane.
          } else {
             // they pressed OK and the test variable has useful information
          }
    Last edited by Fubarable; 10-01-2011 at 06:04 AM.

  20. #20
    smasm is offline Member
    Join Date
    Sep 2011
    Location
    US
    Posts
    16
    Rep Power
    0

    Default Re: Cancel button ? In java

    I'm sorry my computer is too slow .. I'm replying from my iPad ,, I've to submit the program before 12
    And I'm trying to run it .. It's frizzing now .. as soon as it work I'll test ittttt ...

Page 1 of 2 12 LastLast

Similar Threads

  1. Cancel button for exiting app
    By phearless in forum AWT / Swing
    Replies: 1
    Last Post: 01-13-2011, 07:20 PM
  2. How do I close Cancel Dialog Box by code
    By thedatawarehouse in forum SWT / JFace
    Replies: 2
    Last Post: 12-22-2009, 06:33 AM
  3. Struts 2 cancel button
    By kirtichopra2003 in forum Advanced Java
    Replies: 0
    Last Post: 09-29-2009, 03:40 PM
  4. Cancel Particular scheduled task
    By hjava in forum New To Java
    Replies: 0
    Last Post: 08-11-2009, 11:10 PM
  5. How should I cancel this thread ?
    By playwin2 in forum Threads and Synchronization
    Replies: 7
    Last Post: 08-27-2008, 06:20 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
  •