Seems like JPanel background isn't read in FocusListener

流过昼夜 提交于 2019-12-25 03:32:45

问题


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:


please don't do that this way, since I read your previous thread ..., now I can't resist

better and easiest way is add ChangeListener to expected JButtons, then inside stateChanged(ChangeEvent changeEvent) you can determine which of JButtons fired this event, for type of event you have to extract this event's type from ButtonModel

(rest is up to you, please put these JButtons to the Vector, Array or Enumerations), for example

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

public class MyButtonGroup {

    private JButton button1 = new JButton("Test Enabled / Disabled");
    private JButton button2 = new JButton("Test Enabled / Disabled");
    private JButton button3 = new JButton("Test Enabled / Disabled");

    public MyButtonGroup() {
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(3, 0, 10, 10));
        button1.addChangeListener(changeListener);
        panel.add(button1);
        button2.addChangeListener(changeListener);
        panel.add(button2);
        button3.addChangeListener(changeListener);
        panel.add(button3);
        JFrame frame = new JFrame("Grouping Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }
    private ChangeListener changeListener = new ChangeListener() {

        public void stateChanged(ChangeEvent changeEvent) {
            JButton abstractButton = (JButton) changeEvent.getSource();
            if (abstractButton == button1) {
                ButtonModel buttonModel = abstractButton.getModel();
                boolean armed = buttonModel.isArmed();
                boolean pressed = buttonModel.isPressed();
                boolean selected = buttonModel.isSelected();
                boolean rolover = buttonModel.isRollover();
                System.out.println("Changed: " + armed + "/" + pressed + "/" + selected + "/" + rolover);
            } else if (abstractButton == button2) {
                ButtonModel buttonModel = abstractButton.getModel();
                boolean armed = buttonModel.isArmed();
                boolean pressed = buttonModel.isPressed();
                boolean selected = buttonModel.isSelected();
                boolean rolover = buttonModel.isRollover();
                System.out.println("Changed: " + armed + "/" + pressed + "/" + selected + "/" + rolover);
            }
        }
    };

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                MyButtonGroup xxx = new MyButtonGroup();
            }
        });
    }
}



回答2:


Because some L&Fs don't display changes to the background color is a useful way, you may want to consider another form of highlighting: a different border, an altered foreground color, a custom icon, or a suitable background panel. More here.




回答3:


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



来源:https://stackoverflow.com/questions/8788696/seems-like-jpanel-background-isnt-read-in-focuslistener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!