Results 1 to 10 of 10
  1. #1
    Laythe's Avatar
    Laythe is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Question How to make the actionPerformed be executed till the next Button click

    Hello EveryOne :)

    What i need do to is:
    To make the first if loop to be executed first then the switch loop be executed second.

    To keep clicking RNButton till the random number will be 1 at that point i want this line "The First Random Number is 1" to be printed, then the next line wont be printed till i click RNButton again.

    The problem is if the random number is 1 at the first click two line gets printed i just need the line in the first if loop to be printed "The First Random Number is 1".
    if the random number != 1 the lines in the second if loop gets printed and that should'nt happened till i get 1 as random number.

    I can't figure it out on my owne can someone help please.

    My Code is:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.*;
    
    public class NextClickEvent
    {
        private static JLayeredPane PanelX = new JLayeredPane();
        
        private static JButton RNButton    = new JButton("RandomNumber");
        private static JButton ClearButton = new JButton("Clear");
    
        private static JTextField RNTF    = new JTextField();
        private static JTextArea  TA      = new JTextArea();
    
        private static Random Generate = new Random();
        private static int    RandomNumber;
    
        private static boolean Occurred = false;
    
        private static void CreateAndShow()
        {       
            PanelX.setPreferredSize(new Dimension(710, 735));
            
            RNButton.setBounds(300, 80, 150, 30);
            RNButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent AE)
                {
                    RandomButtonActionPerformed(AE);
                }
            });
            PanelX.add(RNButton);
    
            ClearButton.setBounds(300, 112, 150, 30);
            ClearButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent AE)
                {
                    ClearButtonActionPerformed(AE);
                }
            });
            PanelX.add(ClearButton);
    
            RNTF.setBounds(300, 144, 150, 30);
            //RNTF.setEnabled(false);
            PanelX.add(RNTF);
    
            TA.setBounds(80, 80, 200, 200);
            //TA.setEnabled(false);
            PanelX.add(TA);
    
            JFrame FrameX = new JFrame();
            FrameX.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            FrameX.setSize(500, 500);
            FrameX.setVisible(true);
    
            FrameX.setContentPane(PanelX);
        }
    
        private static void RandomButtonActionPerformed(ActionEvent E)
        {
            RandomNumber = Generate.nextInt(4) + 1;
            RNTF.setText(String.valueOf(RandomNumber));
    
            if (RandomNumber == 1 && Occurred == false)
            {
                Occurred = true;
                TA.append("The First Random Number is 1\n");
            }
    
            if(Occurred == true)
            {
                switch(RandomNumber)
                {
                    case 1:
                        TA.append("The second Random Number is 1\n");
                        break;
                    case 2:
                        TA.append("The second Random Number is 2\n");
                        break;
                    case 3:
                        TA.append("The second Random Number is 3\n");
                        break;
                    case 4:
                        TA.append("The second Random Number is 4\n");
                        break;
                }
            }
        }
    
        private static void ClearButtonActionPerformed(ActionEvent E)
        {
            RNTF.setText("");
            TA.setText("");
        }
    
        public static void main(String Laythe[])
        {
            javax.swing.SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    CreateAndShow();
                }
            });
        }
    }
    Last edited by Laythe; 11-01-2009 at 04:36 AM.

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

    Default

    Not sure what you're trying to do here (you may wish to clarify your explanation quite a bit), but are you looking to change the second "if" to an "else if"?

    Also, why the switch and not simply print the number?

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

    Default

    Several other problems with this code:
    1) You use a lot of static variables. If this is being done for the sake of creating a small compilable demonstration program, then fine, but if your program is like this, then it needs to be fixed.
    2) You should observe Java identifier naming rules. Variables should begin with a lower-case letter and classes upper-case. Constants are all-cap.
    3) You should try to avoid null layout. why use a JLayeredPane here rather than a JPanel?
    4) Do you want to place your JTextArea within a JScrollPane?

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

    Default

    Also, and very importantly, you have
    Java Code:
    if (Occurred = true) {
    the single = sets Occurred to true no matter what. Better is

    Java Code:
    else if (Occurred == true) {
    Better still is:
    Java Code:
    else if (Occurred) { // why even have == true or false here?
    Similarly, the first if should read:
    Java Code:
     if (RandomNumber == 1 && !Occurred) {

  5. #5
    Laythe's Avatar
    Laythe is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Default

    Thank you Fubarable for your response.
    1) why is it a problem if i used static variables? i will get error if i dont declare the variables as static.
    2) yes, i know about the convention you are talking but i like them that way.
    3) i used JLayeredPane instead of JPanel cos when i try to setBounds() for the components it dosent work and i dont know why.
    4) nop that's not the problem, compile the code run it and you will understand what i need, if you care enough.

    Thanks in advance.
    Last edited by Laythe; 11-01-2009 at 04:37 AM.

  6. #6
    Laythe's Avatar
    Laythe is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Default

    Also, and very importantly, you have
    Code:
    if (Occurred = true) {

    the single = sets Occurred to true no matter what. Better is

    Code:

    else if (Occurred == true) {

    Better still is:
    Code:

    else if (Occurred) { // why even have == true or false here?
    i am using == i just missed it in my Thread.

    Similarly, the first if should read:
    Code:

    if (RandomNumber == 1 && !Occurred) {
    i want the first if to be executed just once + what i mentioned

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

    Default

    Quote Originally Posted by Laythe View Post
    Thank you Fubarable for your response.
    You're welcome.
    1) why is it a problem if i used static variables? i will get error if i dont declare the variables as static.
    Because you are using an OOPs language in a non-OOPs way. You are getting the errors not because your variables should be static but in that you've designed the program badly. Create true classes here and use true class objects here.

    2) yes, i know about the convention you are talking but i like them that way.
    The conventions are not there for what you like or I like to do, they are there to make things consistent, so that someone helping you, such as the members of this forum, or someone grading you such as a teacher (if you are in school) will be able to tell at a glance that an identifier is a variable or a class. The question you should ask yourself: do I want to make it easier for others to understand my programs, including the volunteers in this forum that help me? Your sticking to conventions will suggest that you want to make it easier for us to help you, and we will appreciate that.

    3) i used JLayeredPane instead of JPanel cos when i try to setBounds() for the components it dosent work and i dont know why.
    Because you need to read up on how to use swing layouts and use them. Go to the Sun tutorials if you wish to learn to do Swing the right way, else you'll be in for a very frustrating experience.

    4) nop i that's not the problem compile the code run it and you will understand what i need, if you care enough.
    I understand (I think) and solution has been posted.

  8. #8
    Laythe's Avatar
    Laythe is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Default

    Thank you very much sir Fubarable it worked perfectly :)

    I didn't saw the answer when i was responding to your reply, cos the page needed to be refreshed.

    Because you are using an OOPs language in a non-OOPs way. You are getting the errors not because your variables should be static but in that you've designed the program badly. Create true classes here and use true class objects here.
    As i learned when dealing with swing is better to use a static methods called CreateAndShow() and then invoke that method as i did so what's wrong with what i did?
    would you direct me to something to read about when to use static and non-static variables and methods.

    By the way i am a self-java-learner.
    Last edited by Laythe; 11-01-2009 at 05:16 AM.

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

    Default

    Quote Originally Posted by Laythe View Post
    Thank you very much sir Fubarable it worked perfectly :)
    You're quite welcome.

    As i learned when dealing with swing is better to use a static methods called CreateAndShow() and then invoke that method as i did so what's wrong with what i did?
    Nothing's wrong about that, but what is key is that inside the static createAndShow method, you create and instantiate objects, and use them.

    would you direct me to something to read about when to use static and non-static variables and methods.
    This is why I think it is key to get a grip on basic Java before trying to do Swing. I suggest that you get a basic Java text book, ANY basic text book and go through it chapter by chapter. This is how you'll learn to create and use classes and basic OOPs technique. Myself, I taught myself Java 3 yrs ago to help my son out with a college project, and I bought a couple of used Java books online, so I was able to start out "on the cheap" so to speak.

    By the way i am a self-java-learner.
    By the way, me too.

  10. #10
    Laythe's Avatar
    Laythe is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

Similar Threads

  1. Generate csv file on a button click
    By violeta in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-27-2009, 02:54 AM
  2. deselecting a button after the click.
    By ramsrocker in forum New To Java
    Replies: 10
    Last Post: 02-15-2009, 06:52 AM
  3. Double click radio button
    By mandragora in forum AWT / Swing
    Replies: 11
    Last Post: 11-10-2008, 11:06 AM
  4. AWT - catching click button event
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-11-2008, 11:02 PM
  5. How to perform some event to button click
    By eva in forum AWT / Swing
    Replies: 2
    Last Post: 01-16-2008, 12:27 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
  •