Results 1 to 20 of 22
Thread: Cancel button ? In java
- 10-01-2011, 01:02 AM #1
Member
- Join Date
- Sep 2011
- Location
- US
- Posts
- 16
- Rep Power
- 0
- 10-01-2011, 01:07 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: Cancel button ? In java
- 10-01-2011, 01:45 AM #3
Member
- Join Date
- Sep 2011
- Location
- US
- Posts
- 16
- Rep Power
- 0
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 ?
- 10-01-2011, 02:09 AM #4
Re: Cancel button ? In java
Do a Search on the forum for ActionListener for code samples
- 10-01-2011, 02:56 AM #5
Member
- Join Date
- Sep 2011
- Location
- US
- Posts
- 16
- Rep Power
- 0
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
- 10-01-2011, 03:01 AM #6
Re: Cancel button ? In java
Strange. When I did a Search here it found hundreds of posts.Yes I did .. I couldn't find ..
Are you sure you spelled it correctly?
- 10-01-2011, 04:04 AM #7
Member
- Join Date
- Sep 2011
- Location
- US
- Posts
- 16
- Rep Power
- 0
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 :(
-
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.
- 10-01-2011, 04:14 AM #9
Member
- Join Date
- Sep 2011
- Location
- US
- Posts
- 16
- Rep Power
- 0
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-01-2011, 04:25 AM #10
Member
- Join Date
- Sep 2011
- Location
- US
- Posts
- 16
- Rep Power
- 0
- 10-01-2011, 04:29 AM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
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.Java Code:JButton button = new JButton("Hello"); button.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e){ //do stuff here } });
- 10-01-2011, 04:30 AM #12
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
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
- 10-01-2011, 05:03 AM #13
Member
- Join Date
- Sep 2011
- Location
- US
- Posts
- 16
- Rep Power
- 0
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.
- 10-01-2011, 05:16 AM #14
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
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")
});
- 10-01-2011, 05:25 AM #15
Member
- Join Date
- Sep 2011
- Location
- US
- Posts
- 16
- Rep Power
- 0
-
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).
- 10-01-2011, 05:52 AM #17
Member
- Join Date
- Sep 2011
- Location
- US
- Posts
- 16
- Rep Power
- 0
- 10-01-2011, 05:53 AM #18
Member
- Join Date
- Sep 2011
- Location
- US
- Posts
- 16
- Rep Power
- 0
-
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.
- 10-01-2011, 06:14 AM #20
Member
- Join Date
- Sep 2011
- Location
- US
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
Cancel button for exiting app
By phearless in forum AWT / SwingReplies: 1Last Post: 01-13-2011, 07:20 PM -
How do I close Cancel Dialog Box by code
By thedatawarehouse in forum SWT / JFaceReplies: 2Last Post: 12-22-2009, 06:33 AM -
Struts 2 cancel button
By kirtichopra2003 in forum Advanced JavaReplies: 0Last Post: 09-29-2009, 03:40 PM -
Cancel Particular scheduled task
By hjava in forum New To JavaReplies: 0Last Post: 08-11-2009, 11:10 PM -
How should I cancel this thread ?
By playwin2 in forum Threads and SynchronizationReplies: 7Last Post: 08-27-2008, 06:20 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks