Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7056359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:51:28+00:00 2026-05-28T03:51:28+00:00

This problem appeared after my last question here . I want to set each

  • 0

This problem appeared after my last question here. I want to set each button focused and lost focus background to background color which is below main menu (ContentPane which is JPanel), so buttons look like tabs. It couuld be different in different environments, so it is dynamic, so I can’t set it manually. Now, If I log ContentPane background it says 238, 238, 238. If I log it inside FocusListener – it also states 238, 238, 238. If I directly set button’s background to ContentPane background outside FocusListener – it works, but if I set inside FocusListener – it looks like value is not read and set, but if I set color manually – it works. How this could happen? Setting FocusListener to buttons is the last thing what I do in initialization of main JPanel.

private void setButtonDefaults(JButton but) {//calls once for each menu button to set defaults
    but.setBorderPainted(false);
    but.setBackground(Color.DARK_GRAY);
    but.setForeground(Color.WHITE);
    but.setName(but.getText().toLowerCase());
    but.setPreferredSize(buttonSize);
    but.addActionListener(this);
    //add focus listener
    but.addFocusListener(new FocusListener() {
        @Override
        public void focusLost(FocusEvent e) {
            Color clr = ContentPane.getBackground();
            log(clr + "");//logs that color is 238, 238, 238
            JButton button = (JButton) e.getSource();
            button.setBackground(clr);//value is not read
            //button.setBackground(new Color(238, 238, 238)); //value is read
        }

        @Override
        public void focusGained(FocusEvent e) {
            //same as focusLost function
        }
    });
}
private void enableOnlyOne(JButton but) { 
/* calls each time when one of menu buttons are pressed. 
All buttons are unpressed and changed colors to black and one
button is set as pressed and changes background color to
ContentPane background color
*/
    //disable all
    setButtonDisabled(MLibrary);
    setButtonDisabled(MScheduler);
    setButtonDisabled(MBudget);
    setButtonDisabled(MReports);
    setButtonDisabled(MManage);
    setButtonDisabled(MSettings);
    //enable one
    but.getModel().setPressed(true);
    but.setBackground(ContentPane.getBackground());//value is read perfect
    but.setForeground(Color.BLACK);
}
private void setButtonDisabled(JButton but) {
    but.getModel().setPressed(false);
    but.setBackground(Color.DARK_GRAY);
    but.setForeground(Color.WHITE);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:51:29+00:00Added an answer on May 28, 2026 at 3:51 am

    It is so good to find out answer by myself:

    private void setButtonDefaults(JButton but) {
        but.setBorderPainted(false);
        but.setBackground(Color.DARK_GRAY);
        but.setForeground(Color.WHITE);
        but.setName(but.getText().toLowerCase());
        but.setPreferredSize(buttonSize);
        but.addActionListener(this);
        //add focus listener
        final Color clr = ContentPane.getBackground();
        final int r = clr.getRed();
        final int g = clr.getGreen();
        final int b = clr.getBlue();
        but.addFocusListener(new FocusListener() {
            @Override
            public void focusLost(FocusEvent e) {
                log("r = " + r + ", g = " + g + ", b = " + b);
                JButton button = (JButton) e.getSource();
                button.setBackground(new Color(r, g, b));
            }
    
            @Override
            public void focusGained(FocusEvent e) {
                JButton button = (JButton) e.getSource();
                button.setBackground(new Color(r, g, b));
            }
        });
    }
    

    Thanks to everyone who tried (and did) to help me

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This problem occurred during daylight saving time change. After the change occurred, we've noticed
While considering another problem one question appeared. I do not know how to write
I have the following problem: This error appeared to me, when I compiled the
This problem crops up every now and then at work. Our build machine can
This problem has been solved thanks to your suggestions. See the bottom for details.
This problem has been kicking my butt for a few days now. I have
This problem involved me not knowing enough of C++. I am trying to access
This problem pertains to Java By using RandomAccessFile I intend to be able to
This problem occurs when I try to hide a file in my directory with
This problem has bugged me so many times and i have now decided to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.