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 6 of 6

Thread: need help with JButton and switch statements

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy need help with JButton and switch statements

    Hello Dalisra,
    I hope u can help me.
    I am also kinda new with Java, I still need my Sun exams behind my name so Im not a registered programmer yet.
    Im trying to build a program for myself and trying to put in new statements but have a problem with finishing it
    and Im looking for answers on the Internet but currently havent found anything yet.
    My problem is and I hope u can help me with this one, its also with switch statement. Its about using JButtons
    inside a switch statement. Each button u click on makes a choice in ur switch statement.
    Kinda know one must use an enum but dont totally know what to do all the different methods and stuff that has
    to be used.

    If u can PLEASE help me with this one Dalisra I will be very Pleased.
    Thanks Jean.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Switch statement question

    Quote Originally Posted by jjoubert View Post
    Hello Dalisra,
    I hope u can help me.
    I am also kinda new with Java, I still need my Sun exams behind my name so Im not a registered programmer yet.
    Im trying to build a program for myself and trying to put in new statements but have a problem with finishing it
    and Im looking for answers on the Internet but currently havent found anything yet.
    My problem is and I hope u can help me with this one, its also with switch statement. Its about using JButtons
    inside a switch statement. Each button u click on makes a choice in ur switch statement.
    Kinda know one must use an enum but dont totally know what to do all the different methods and stuff that has
    to be used.

    If u can PLEASE help me with this one Dalisra I will be very Pleased.
    Thanks Jean.
    Hey jjoubert.

    Please create a new thread.. I'm sure someone will be able to solve your problem!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Switch statement question

    Hello JavaPF,
    OK here is my kinda exact problem.
    PLEASE if u can help me with this?


    as u will see following is the enum I created for the 4 buttons:
    enum ChoiceMarker{cvButton, workButton, personalButton, exitButton}
    here following are the creation of the 4 buttons:
        JButton cvButton = new JButton("Qualifications");
        JButton workButton = new JButton("Knowledge of Work");
        JButton personalButton = new JButton("Jean's Preferences");
        JButton exitButton = new JButton("Exit");
    Now this is where the problem is. I cant find a way to declare the JButtons to Integer
    for use with the switch statement.
        Object source = e.getSource();
        ChoiceMarker button = Integer.parseInt(source);
     
     
    switch (button)
    	{
    	case cvButton       : JeansFirstCV_Program jeansCV = new JeansFirstCV_Program();break;
    	case workButton     : JeansLikingOfWork jeanslw = new JeansLikingOfWork();break;
    	case personalButton : JeansePreferences jeansep = new JeansePreferences();break;
    	case exitButton     : ExitFully();break;
    	}
    this is the method I used and it works perfectly but I want to come to know
    the switch statement...
    public void actionPerformed(ActionEvent e)
        {
            Object source = e.getSource();
            if (source == cvButton)
            {
                JeansFirstCV_Program jeansCV = new JeansFirstCV_Program();
     
            }
     
    	else if (source == workButton)
            {    
                JeansLikingOfWork jeanslw = new JeansLikingOfWork();
            }
     
    	else if (source == personalButton)
            {    
                JeansePreferences jeansep = new JeansePreferences();
            }
     
        else if (source == exitButton)
            {
                ExitFully();
            }
    I hope this help u with trying to help me with this problem.
    Just ask if I can send anything more..?!?!
    Thanks till this far
    Last edited by helloworld922; October 23rd, 2009 at 10:36 AM.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: need help with JButton and switch statements

    Java doesn't support "de-facto" casts between integers and enums. I know you can go from Enums to integers by using the ordinal() method, I don't think you can the other way around without writing your own method for the conversion. There is a method inside the Java Enum class that lets you take a String input and convert it to the matching Enum. I'm going to assume that the button's name is the same as the Enum specified.

    Also, fyi it's good practice to have 1 statement per line at most (sometimes, multiple lines per statement).

        ChoiceMarker button = ChoiceMarker.valueOf(e.getActionCommand());
     
     
    switch (button)
    	{
    	case cvButton       :
                        JeansFirstCV_Program jeansCV = new JeansFirstCV_Program();
                        break;
    	case workButton     :
                        JeansLikingOfWork jeanslw = new JeansLikingOfWork();
                        break;
    	case personalButton : 
                        JeansePreferences jeansep = new JeansePreferences();
                        break;
    	case exitButton     : 
                        ExitFully();
                        break;
    	}

  5. The Following User Says Thank You to helloworld922 For This Useful Post:

    jjoubert (October 28th, 2009)

  6. #5
    Junior Member
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: need help with JButton and switch statements

    Quote Originally Posted by helloworld922 View Post
    Java doesn't support "de-facto" casts between integers and enums. I know you can go from Enums to integers by using the ordinal() method, I don't think you can the other way around without writing your own method for the conversion. There is a method inside the Java Enum class that lets you take a String input and convert it to the matching Enum. I'm going to assume that the button's name is the same as the Enum specified.

    Also, fyi it's good practice to have 1 statement per line at most (sometimes, multiple lines per statement).

        ChoiceMarker button = ChoiceMarker.valueOf(e.getActionCommand());
     
     
    switch (button)
    	{
    	case cvButton       :
                        JeansFirstCV_Program jeansCV = new JeansFirstCV_Program();
                        break;
    	case workButton     :
                        JeansLikingOfWork jeanslw = new JeansLikingOfWork();
                        break;
    	case personalButton : 
                        JeansePreferences jeansep = new JeansePreferences();
                        break;
    	case exitButton     : 
                        ExitFully();
                        break;
    	}


    Hello World,
    Thx very much for ur help concerning this. So SORRY coming back to u now but sorry
    been in hospital from monday till today.
    I tried it out and thx the file now compiles correctly thx but wont open up the class concerning the JButton
    clicked upon. Dont really know anything about the new type of code you have given me now if u can just
    PLEASE help here..??

    Thx and hope to hear soon.
    from Jean.

  7. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: need help with JButton and switch statements

    Me thinks you forgot to add your class to be an action listener for all the buttons?

    jeans_CV_Button.addActionListener(this);
    ... etc

Similar Threads

  1. How to Add ActionListener to a JButton in Swing?
    By JavaPF in forum Java Swing Tutorials
    Replies: 17
    Last Post: April 24th, 2013, 05:14 PM
  2. Need help with While and For Statements
    By duckman in forum Loops & Control Statements
    Replies: 2
    Last Post: October 20th, 2009, 08:42 PM
  3. switch with Enums
    By chronoz13 in forum Loops & Control Statements
    Replies: 17
    Last Post: October 8th, 2009, 08:08 PM
  4. [SOLVED] Fixing of bug in number guessing game
    By big_c in forum Loops & Control Statements
    Replies: 6
    Last Post: April 16th, 2009, 02:15 AM
  5. JAVA Image Icon and JButton resizing problem
    By antitru5t in forum AWT / Java Swing
    Replies: 1
    Last Post: March 13th, 2009, 04:39 AM