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: Trick - How to build a button to switch between panel colors

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    34
    My Mood
    Busy
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Trick - How to build a button to switch between panel colors

    This is just a little code snippet containing a little trick I used today to build a simple button that switches between panel colors.

    [NOTE: This might not be the best method. If you have a better way of doing it, please reply.]

     
    //Declarations for the variables used below
    int option = 0; //Variable used to select colors
    final Color[] colors = {Color.ORANGE, Color.BLUE,
    Color.CYAN, Color.RED, Color.GREEN,
    Color.YELLOW, Color.MAGENTA}; //Our Color array.
    public void actionPerformed(ActionEvent e) {
     
        option++; //Increment this variable each time we click the button
        if(option>colors.length-1) option = 0; //Check if it has reached the end of the array.
        //If it has, reset the variable option.
        myPanel.setBackground(colors[option]); //Update colors.
        myPanel2.setBackground(colors[option]);
    }

    So what this should do is everytime you click the button, the option variable increments. Then it sets the background of the 2 panels to one of the values in color[], where the index is the new value of option. If it becomes greater than the amount of elements in option (-1), It resets the option variable back to 0. This method is handy, so all you have to do to add more colors is to add more elements to the array.

    Again, there could be other methods better than this, so feel free to post if you have an easier way.

    EDIT: Just a note to some people wondering why I use colors.length-1 instead of colors.length. If you use colors.length, It will work fine, but it shows errors in the console. Use colors.length-1 to avoid the errors.

  2. The Following User Says Thank You to vividMario52 For This Useful Post:

    craigjlner (October 22nd, 2013)


  3. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trick - How to build a button to switch between panel colors

    I'd move the colors array outside the method next to where the option variable is defined and make it final to show that isn't to be updated.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #3
    Member
    Join Date
    Jan 2013
    Posts
    34
    My Mood
    Busy
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Re: Trick - How to build a button to switch between panel colors

    Quote Originally Posted by Norm View Post
    I'd move the colors array outside the method next to where the option variable is defined and make it final to show that isn't to be updated.
    Ah, good idea. I'll change that right now.

  5. #4
    Junior Member Ayokings2002's Avatar
    Join Date
    Feb 2013
    Location
    Elizabeth , nj
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Well the if statement could be in one bracket
    If(option=0;option>colors.length;option++){


  6. #5
    Member
    Join Date
    Jan 2013
    Posts
    34
    My Mood
    Busy
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Re: Trick - How to build a button to switch between panel colors

    Yea, but I exclude the bracket(s) if the code is only one line.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trick - How to build a button to switch between panel colors

    Leaving off the {}s can be a problem if later you add more code and forget to add them. Best to always use them.
       if(some == all) 
           doThis();
           doThat();          //<<<<<<< This is not part of the if
    This can happen and will not be noticed.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7

    Default Re: Trick - How to build a button to switch between panel colors

    Thanks for sharing the code. I will implement this code to build simple button.

Similar Threads

  1. Java3D - Colors mix up
    By Java3D_Noob in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 26th, 2012, 02:24 PM
  2. [SOLVED] The button wouldn't show on the panel
    By nggdowt in forum AWT / Java Swing
    Replies: 7
    Last Post: November 3rd, 2011, 09:31 PM
  3. Trick question?
    By Kungpaoshizi in forum Java Theory & Questions
    Replies: 4
    Last Post: August 30th, 2011, 07:20 AM
  4. Adding fixed size picture and button to panel
    By Javabeginner in forum AWT / Java Swing
    Replies: 10
    Last Post: August 23rd, 2010, 06:07 PM

Tags for this Thread