Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: JButton event handling.

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default JButton event handling.

    Hi, this is my first post on a Java forum, so please go easy ! (I`m sure you won`t bite !)


    OK, I`m learning Java on an Open University Course. I`ve come to a point where I want to "deviate" from the course a little, to test and expand upon what I`ve learned. As usual, I`m running (well, jogging) a little before I can walk properly, but that`s the way I am !

    I`m trying to write a Lottery Syndicate Management program for a friend who runs the syndicate that I am a member of. It`s a GUI standalone application that stores and maintains sysndicate member`s details and monitors payments from members, payouts to members and checks our lottery entries against each week`s lottery draw. So far, so good(ish). I have now stumbled upon a programming issue that I`m stuck on.

    I have a JPanel with several buttons to invoke different functions for Syndicate Members - Add, Delete, Edit, Payment, Payout, OK and Cancel. Next to these buttons, in the same JPanel, I`m displaying the currently selected member (if there is one). If I click on "Add", the "add" method is called to carry out the procedure to enter a new syndicate member`s details, then add this member to the current syndicate. Depending on current "mode", I enable or disable appropriate buttons. In "add" mode, I disable Edit, Payment etc buttons, but enable OK and Cancel. When the member has been added, the add method finishes by creating a new member and enabling the "Add, Delete Edit etc buttons, ready for the next user request.

    Here`s the question !

    The "Cancel" and "Ok" buttons need to execute different code/methods, depending upon the current procedure being carried out. If a member is being added, the OK button should check the entered details and create a new member. If a payment is being input for a current member, the OK button will simply invoke code to enter the payment into the member`s details. I`m using ActionListener to detect mouse clicks on enabled JButtons, then call the appropriate method, as below.

        public MemberPanel() {
            //initComponents();
            super();
            jButton1.addActionListener(this);
            jButton2.addActionListener(this);
            jButton3.addActionListener(this);
            jButton4.addActionListener(this);
            jButton5.addActionListener(this);
            jButton6.addActionListener(this);
            jButton7.addActionListener(this);
            jButton8.addActionListener(this);
            jButton9.addActionListener(this);
        }   
     
        public void actionPerformed(ActionEvent event)
        {
           Object source = event.getSource();
           if (source == jButton1)
           {
              this.add();
           }
           if (source == jButton2)
          {
              this.payment()
           }
           if (source == jButton6)
           {
              this.ok();
           }
           if (source == jButton7)
           {
              this.cancel();
           }
         }
    If the program is executing "add()", and the user clicks the Cancel button, I need invoke specific code to cancel the "add" operation. However, if the user is using "Payment", the same cancel button will need to invoke different code.

    Any suggestion on what is the best way to achieve what I`m trying to do ?

    I`ve thought about creating seperate, specific JPanels for each mode of operation, but that seems like a lot of work, and is probably inefficient.

    Could it be that I need to create several "Cancel" buttons, and utilise a specific button for each mode of operation ? What I`ve learned about Java so far, is that it`s more powerfull, yet complex than I thought it would be, and that there are often "clever" ways of achieving certain programming goals.

    Oh, and studying Java is interesting, but it sometimes makes my brain hurt.
    Last edited by Prof; February 3rd, 2010 at 07:46 AM.


  2. #2
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: JButton event handling.

    the same cancel button will need to invoke different code.
    you mean 1 source for different commands?

    i hope this will help you
    cancelButton.setActionCommand("Your first string command here");
    cancelButton.setActionCommand("Your second string command here");
    cancelButton.setActionCommand("Your third string command here");

    in your interface class(actionlistner)
     
    if(event.getActionCommand.equalas("MyFirstCancelCommand") {
     
        // do the task;
    }
    else if (event.getActionCommand.equals("MySeconCancelCommand") {
     
       // do the taskl
    }
     
    // so on...

    the event will get the action command, it must be equal to the "String" command that you set in your button.
    Last edited by chronoz13; February 3rd, 2010 at 08:19 AM.

  3. The Following User Says Thank You to chronoz13 For This Useful Post:

    Prof (February 3rd, 2010)

  4. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JButton event handling.

    Thank you, that looks like the solution I need. I thought I`d read about something like this, but there`s so much to remember about Java !

    OK, another thought/question, as this is the first time i`ve programmed with Swing/actionEvents.

    How often, and when do these "actionEvents" occur ? If I have a method executing, and the user clicks on an enabled JButton, at what point does the code to handle the event get executed ? If required, is there a way to disable or ignore events while some critical processing is taking place ?

    I`ve got so many questions ! I`d like to know about making JButtons invisible, instead of disabling them. The problem I have is that I am using a simple Java IDE (BlueJay), as Eclipse/NetBeans are too daunting for me at the moment. However, I am using the NetBeans GUI designer to create the layout for my JPanel(s). I`m probably being a bit "naughty", but I`m simply copying and pasting the NetNeans generated source code into my BlueJay project. If I then add my own code to make a JButton invisible, it messes up the layout, as NetBeans has generated code that places the components in positions relative to each other.

    Any suggestions for an easier/better way for me to design GUI layouts graphically and to generate the Java source code for my applications ? I suppose I`m looking for a simple IDE with GUI design features built in, if such a thing exists.

  5. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: JButton event handling.

    How often, and when do these "actionEvents" occur ? If I have a method executing, and the user clicks on an enabled JButton, at what point does the code to handle the event get executed ? If required, is there a way to disable or ignore events while some critical processing is taking place ?
    - Threads and Swing , Event dispatching thread - Wikipedia, the free encyclopedia , Lesson: Writing Event Listeners (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    I`m looking for a simple IDE with GUI design features
    - netbeans is your way sir, as far as I know netbeans is the only IDE that can provide rich features regarding with GUIs, you might also look for Eclipse IDE.

    For layouts have a look at this - Using Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

    theres also another way for setting your components manualy l (i.e setting their locations and dimensions manually
    AbosluteLayout
    - Doing Without a Layout Manager (Absolute Positioning) (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

    but i suggest try to study layouts, because you might need those for your future complex demands in your GUIs


    for example: im currently writing a program , an application that uses JFrame, in that frame I have a scroll pane that divides two panels, the logic for the scroll pane is If i move the scroll Up the components on the north side will "Shrink" then the components on the south side will expand, respectively, but I am not "Able" to do this because I didnt use and studied any layouts... so i would suggest you study basic layouts that sun provides, I've already gave you the link sir.. I hope it helped you ...

    happy coding..
    Last edited by chronoz13; February 3rd, 2010 at 10:16 AM.

  6. The Following User Says Thank You to chronoz13 For This Useful Post:

    Prof (February 3rd, 2010)

  7. #5
    Junior Member
    Join Date
    Feb 2010
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JButton event handling.

    Thanks,

    Looks like there`s no short cut...... back to the books/articles for me.

    I`m finding that to really learn to code, I have to practice coding, and when I start to code I realise that I haven`t learned enough !

  8. #6
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: JButton event handling.

    so do I.. I know how and what you feel sir... I thought I learned "A lot" but its not "Enough" .

    happy coding...

  9. #7
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: JButton event handling.

    I want to share this too sir.. lots of sample codes Swing JFCJava

Similar Threads

  1. Regarding File Handling
    By ravjot28 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 1st, 2010, 07:45 PM
  2. Quick JComboBox Event Question
    By -tr in forum AWT / Java Swing
    Replies: 2
    Last Post: December 12th, 2009, 05:35 PM
  3. Event handling
    By subhvi in forum AWT / Java Swing
    Replies: 3
    Last Post: August 26th, 2009, 11:20 AM
  4. Exception handling for TextFields
    By FretDancer69 in forum AWT / Java Swing
    Replies: 1
    Last Post: June 16th, 2009, 07:48 AM
  5. Exception handling
    By AnithaBabu1 in forum Exceptions
    Replies: 6
    Last Post: August 27th, 2008, 09:37 AM