姬長信(Redy)

java – 如何更改焦点JComboBox的突出显示颜…


让我先解释一下我想要实现的目标.我正在Swing中创建一个数据输入表单,由许多JComboBox和&组成. JTextField的.验证例程迭代这些组件并确定为每个控件指定的值是否“有效”(验证的详细信息与本示例的目的无关).

当例程识别出组件包含无效值时,我想要更改
该字段的背景颜色,以及该字段的前景/文本颜色 – 使用户清楚该字段存在问题.

如果字段被视为“有效”,我想将控件的背景设置为白色 – 前景/文本为黑色.

到目前为止,所有这些都很简单,并且可以在下面附带的演示代码中实现.

当一个组合框包含一个有效值并且被聚焦时 – 组合中编辑器的背景设置为蓝色,我非常满意.

但是,我想要实现的是当该组合框包含无效值时,更改用于突出显示焦点组合框的颜色.尽管已将组合框的背景颜色更改为粉红色,但如果控件被聚焦,它仍然使用蓝色来表示它是聚焦的.

聚焦的无效字段示例:
http://postimg.org/image/ne9xgjch3/

虽然我很欣赏这是完全正常的行为,但我想要做的是将用于突出显示“无效”字段之一的颜色更改为颜色较深的非聚焦字段.无效控制将具有 – 这样用户仍然可以看到哪个控件是聚焦的,并且它仍然是粉红色的.我很欣赏这可能看起来很小,但我的最终用户坚持认为整个领域在聚焦时仍然是粉红色(或者更确切地说,是不同的粉红色).这就是我的eutopia,一个专注和’无效’
字段,看起来像:

http://postimg.org/image/9793bqcfj/

我试过扩展DefaultListCellRenderer& BasicComboBoxEditor类,并将组合框分别设置为渲染器和编辑器.我的印象是编辑器将成为我需要集中注意力的地方,因此在类的getEditorComponent方法中,我将返回一个具有适当背景和前景的标签 – 但是从该方法中,我无法知道控件是否具有焦点,因此无法确定如何格式化返回的标签.另外,一旦我开始针对组合框设置编辑器,我似乎完全失去了控制焦点的能力 – 尽管这可能是我对如何实现编辑器缺乏了解.

我也一直在阅读有关BasicComboBoxUI的内容,但我遇到的任何内容都没有作为解决方案.

请有人可以指出我正确的方向,我花了几天时间来修补它,它真的开始让我烦恼.请原谅netbeans生成的演示代码,这只是为了让我快速拼凑一个演示.

package com.test;

import java.awt.*;

public class TestForm extends javax.swing.JFrame {

    public TestForm()
    {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // //GEN-BEGIN:initComponents
    private void initComponents() {

        cboOne = new javax.swing.JComboBox();
        txtOne = new javax.swing.JTextField();
        txtTwo = new javax.swing.JTextField();
        btnValidate = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        cboOne.setBackground(new java.awt.Color(255, 255, 255));
        cboOne.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Valid Value", "Invalid Value", "Another Invalid Value" }));

        txtOne.setText("123");

        txtTwo.setText("123");

        btnValidate.setText("Validate");
        btnValidate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnValidateActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cboOne, 0, 376, Short.MAX_VALUE)
                    .addComponent(txtOne)
                    .addComponent(txtTwo)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnValidate)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cboOne, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtOne, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtTwo, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnValidate)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// //GEN-END:initComponents

    private void btnValidateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnValidateActionPerformed

        //Check if the selection in the ComboBox is valid...
        if (((String)cboOne.getSelectedItem()).equals("Valid Value"))
        {
            //Selected Value is Valid.
            //We want the combo box to appear with a white background
            //and black text.
            cboOne.setBackground(Color.white);
            cboOne.setForeground(Color.black);
        }
        else
        {
            //The value specified is invalid.
            //We want to highlight the field in pink to identify an issue,
            //and change the color of the text to red too:
            cboOne.setBackground(Color.pink);
            cboOne.setForeground(Color.red);
        }

        //Check if the value entered into the first Text Field is valid...
        if (txtOne.getText().equals("123"))
        {
            //Selected Value is Valid.
            //We want the text box to appear with a white background
            //and black text.            
            txtOne.setBackground(Color.white);
            txtOne.setForeground(Color.black);
        }
        else
        {
            //Selected Value is invalid.
            //We want the text box to appear with a pink background
            //and red text. 
            txtOne.setBackground(Color.pink);
            txtOne.setForeground(Color.red);
        }

        //Check if the value entered into the second Text Field is valid...
        if (txtTwo.getText().equals("123"))
        {
            //Selected Value is Valid.
            //We want the text box to appear with a white background
            //and black text. 
            txtTwo.setBackground(Color.white);
            txtTwo.setForeground(Color.black);
        }
        else
        {
            //Selected Value is invalid.
            //We want the text box to appear with a pink background
            //and red text. 
            txtTwo.setBackground(Color.pink);
            txtTwo.setForeground(Color.red);
        }

    }//GEN-LAST:event_btnValidateActionPerformed

    public static void main(String args[]) {


        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TestForm().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnValidate;
    private javax.swing.JComboBox cboOne;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JTextField txtOne;
    private javax.swing.JTextField txtTwo;
    // End of variables declaration//GEN-END:variables
}