gpt4 book ai didi

java - 仅更改可编辑 JComboBox 的文本字段背景颜色

转载 作者:太空宇宙 更新时间:2023-11-04 06:44:29 25 4
gpt4 key购买 nike

经过一番研究,我找到了一种方法来仅更改可编辑 JComboBox 的文本字段的背景颜色,但是有更好的方法吗?

我想要什么:

我的结果应该是这样的:

enter image description here

我现在在做什么:

澄清一下,我已经得到了上面的结果,但我认为实现它的代码太老套了,因为在第一步中我用想要的颜色绘制所有内容(文本字段、按钮和列表)。然后,我从按钮中删除颜色,然后通过 BasicComboBoxRenderer 从列表中删除颜色。

我的代码:

public static void changeOnlyEditorBackgroundColorOfEditableJComboBox(final JComboBox cb, final Color color) {
//paint the whole ComboBox
cb.getEditor().getEditorComponent().setBackground(color);
//remove the color from the button
final Component[] comps = cb.getComponents();
for (int i = 0; i < comps.length; i++) { // hack valid only for Metal L&F
if (comps[i] instanceof MetalComboBoxButton) {
final MetalComboBoxButton coloredArrowsButton = (MetalComboBoxButton)comps[i];
coloredArrowsButton.setBackground(null);
break;
}
}
//remove the color from the list
cb.setRenderer(new RemoveColorFromComboBoxListRenderer());
}

private static class RemoveColorFromComboBoxListRenderer extends BasicComboBoxRenderer {

// only slightly changed from the original implementation.
@Override
public Component getListCellRendererComponent(final JList list,
final Object value,
final int index,
final boolean isSelected,
final boolean cellHasFocus) {
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
// these lines were changed to always set the background white and the forground black
setBackground(Color.white);
setForeground(Color.black);
}

setFont(list.getFont());

if (value instanceof Icon) {
setIcon((Icon)value);
} else {
setText((value == null) ? "" : value.toString());
}
return this;
}
}

所以我希望我已经监督了一些事情,并且有更好的方法来做到这一点。

最后这是一个指向 SSCCE 的链接.

最佳答案

  • 我认为应该使用 XxxRanderer

  • 成功使用 JComboBox.getEditor().getEditorComponent().setBackground(Color.YELLOW);

enter image description here

例如(UIManager 的键仅适用于不可编辑的 JComboBox,很奇怪但是......)

import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.Vector;
import javax.swing.*;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.metal.MetalComboBoxButton;

public class MyComboBox {

private Vector<String> listSomeString = new Vector<String>();
private JComboBox someComboBox = new JComboBox(listSomeString);
private JComboBox editableComboBox = new JComboBox(listSomeString);
private JComboBox non_EditableComboBox = new JComboBox(listSomeString);
private JFrame frame;

public MyComboBox() {
listSomeString.add("-");
listSomeString.add("Snowboarding");
listSomeString.add("Rowing");
listSomeString.add("Knitting");
listSomeString.add("Speed reading");
//
someComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
someComboBox.setFont(new Font("Serif", Font.BOLD, 16));
someComboBox.setEditable(true);
someComboBox.getEditor().getEditorComponent().setBackground(Color.YELLOW);
((JTextField) someComboBox.getEditor().getEditorComponent()).setBackground(Color.YELLOW);
someComboBox.getEditor().getEditorComponent().addFocusListener(fcsListener);
//
editableComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
editableComboBox.setFont(new Font("Serif", Font.BOLD, 16));
editableComboBox.setEditable(true);
JTextField text = ((JTextField) editableComboBox.getEditor().getEditorComponent());
text.setBackground(Color.YELLOW);
JComboBox coloredArrowsCombo = editableComboBox;
Component[] comp = coloredArrowsCombo.getComponents();
for (int i = 0; i < comp.length; i++) {
if (comp[i] instanceof MetalComboBoxButton) {
MetalComboBoxButton coloredArrowsButton = (MetalComboBoxButton) comp[i];
coloredArrowsButton.setBackground(null);
break;
}
}
editableComboBox.getEditor().getEditorComponent().addFocusListener(fcsListener);
//
non_EditableComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
non_EditableComboBox.setFont(new Font("Serif", Font.BOLD, 16));
non_EditableComboBox.addFocusListener(fcsListener);
//
frame = new JFrame();
frame.setLayout(new GridLayout(0, 1, 10, 10));
frame.add(someComboBox);
frame.add(editableComboBox);
frame.add(non_EditableComboBox);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(100, 100);
frame.pack();
frame.setVisible(true);
}

//
private FocusListener fcsListener = new FocusListener() {

@Override
public void focusGained(FocusEvent e) {
dumpInfo(e);
}

@Override
public void focusLost(FocusEvent e) {
dumpInfo(e);
}

private void dumpInfo(FocusEvent e) {
final Component c = e.getComponent();
System.out.println("Source : " + name(e.getComponent()));
System.out.println("Opposite : " + name(e.getOppositeComponent()));
System.out.println("Temporary: " + e.isTemporary());
if (c instanceof JFormattedTextField) {//works for editable JComboBox too
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
((JFormattedTextField) c).selectAll();
}
});
} else if (c instanceof JTextField) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
((JTextField) c).selectAll();
}
});
} else if (c instanceof JTextField) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
((JComboBox) c).getEditor().selectAll();
}
});
}
}

private String name(Component c) {
return (c == null) ? null : c.getName();
}
};

public static void main(String[] args) {
UIManager.put("ComboBox.background", new ColorUIResource(Color.yellow));
UIManager.put("JTextField.background", new ColorUIResource(Color.yellow));
UIManager.put("ComboBox.selectionBackground", new ColorUIResource(Color.magenta));
UIManager.put("ComboBox.selectionForeground", new ColorUIResource(Color.blue));
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
MyComboBox aCTF = new MyComboBox();
}
});
}
}

关于java - 仅更改可编辑 JComboBox 的文本字段背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24204544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com