Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need way to have a different background color for focused text components #335

Closed
Chrriis opened this issue Jun 4, 2021 · 1 comment
Closed
Milestone

Comments

@Chrriis
Copy link
Contributor

Chrriis commented Jun 4, 2021

Hi all!

GitHub and Firefox, among others, have text components with a gray background that becomes white when focused.
Example:
TextFieldFocusedBackground

I am trying to achieve a similar effect with FlatLaf but I don't see any properties to tweak the behavior on focus. Am I missing something? I tried these:

  • TextField.focusedBackground
  • PasswordField.focusedBackground
  • TextArea.focusedBackground
  • ComboBox.editableFocusedBackground

Here is the test code:

public static void main(String[] args) throws Exception {
	UIManager.setLookAndFeel(new FlatLightLaf());
	UIManager.getDefaults().put("Panel.background", Color.WHITE);
	UIManager.getDefaults().put("TextField.background", new Color(0xf0f0f0));
	UIManager.getDefaults().put("TextField.focusedBackground", Color.WHITE);
	UIManager.getDefaults().put("PasswordField.background", new Color(0xf0f0f0));
	UIManager.getDefaults().put("PasswordField.focusedBackground", Color.WHITE);
	UIManager.getDefaults().put("TextArea.background", new Color(0xf0f0f0));
	UIManager.getDefaults().put("TextArea.focusedBackground", Color.WHITE);
	UIManager.getDefaults().put("ComboBox.editableBackground", new Color(0xf0f0f0));
	UIManager.getDefaults().put("ComboBox.editableFocusedBackground", Color.WHITE);
	JFrame frame = new JFrame();
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	JPanel centerPane = new JPanel(new GridBagLayout());
	centerPane.setBorder(BorderFactory.createEmptyBorder(20, 40, 20, 40));
	int y = 0;
	centerPane.add(new JTextField("Sample text field", 14), new GridBagConstraints(0, y++, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
	centerPane.add(new JPasswordField("Password field", 14), new GridBagConstraints(0, y++, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 0, 0), 0, 0));
	centerPane.add(new JScrollPane(new JTextArea("Sample text area", 5, 14)), new GridBagConstraints(0, y++, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 0, 0), 0, 0));
	JComboBox<String> combo = new JComboBox<>(new String[] {"Item 1", "Item 2", "Item 3"});
	combo.setEditable(true);
	centerPane.add(combo, new GridBagConstraints(0, y++, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 0, 0), 0, 0));
	frame.getContentPane().add(centerPane);
	frame.pack();
	frame.setLocationByPlatform(true);
	frame.setVisible(true);
}

... and here is the output:
TextComponentsFocusedBackground

Thanks in advance!

@Chrriis
Copy link
Contributor Author

Chrriis commented Jun 4, 2021

Another component that is not a text component that would be interesting too: a combo box editable=false. When the user clicks, it would get the white background and the popup that is shown too.
All components could benefit from having these "focusXX" variants.

Chrriis added a commit to Chrriis/FlatLaf that referenced this issue Jun 8, 2021
DevCharly added a commit that referenced this issue Jun 15, 2021
- use focusedBackground only if editable (and enabled)
- prefer explicit set background color over focusedBackground
- added FlatTextFieldUI.getBackground() used by all text components
- support EditorPane.focusedBackground
- support TextPane.focusedBackground

(issue #335)
DevCharly added a commit that referenced this issue Jun 15, 2021
ComboBox:
- prefer explicit set background color over focusedBackground
- if ComboBox.buttonFocusedBackground is not specified use ComboBox.focusedBackground
- added ComboBox.popupFocusedBackground

(issue #335)
DevCharly added a commit that referenced this issue Jun 15, 2021
Issue #335: allow a different background on focus
@DevCharly DevCharly added this to the 1.3 milestone Jun 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants