Cor do JComboBox desabilitado

2 respostas
P

Para que um JTextField fique com a cor da fonte preta mesmo desabilitado damos um setDisabledTextColor(Color.black) e pro JComboBox?

2 Respostas

R

Para quem ainda vai precisar dessa resposta. Achei nesse local o que eu precisava:
http://www.developpez.net/forums/archive/index.php/t-354.html

Fiz umas adaptações :

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.*;
import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;

public class Enable { 

    Boolean comboBoxEnabled = false;
    JButton jb; 
    JComboBox jcb; 
    public static void main(String[] args){
        new Enable();
    }

    public Enable(){
    //super("test !!!");

    /*try{
    UIManager.setLookAndFeel ("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    }
    catch(Exception e){
    SwingUtilities.updateComponentTreeUI (getContentPane());
    }*/

        FontUIResource sansserifPlain11 = new FontUIResource("SansSerif",Font.PLAIN, 22);
        UIManager.put("ComboBox.font", sansserifPlain11);
    
        UIManager.put("ComboBox.disabledText", new ColorUIResource(Color.pink));
    
        UIManager.put("Button.foreground", new ColorUIResource(Color.blue));
        UIManager.put("Button.border", new ColorUIResource(Color.ORANGE ));
    
        // combobox habilitado
        UIManager.put("ComboBox.background", new ColorUIResource(Color.TRANSLUCENT));    
        UIManager.put("ComboBox.foreground", new ColorUIResource(Color.RED));
    
        UIManager.put("ComboBox.selectionForeground", new ColorUIResource(Color.BLUE));
    
        // cores do combobox desabilitado
        UIManager.put("ComboBox.disabledForeground", new ColorUIResource(Color.GREEN));    
        UIManager.put("ComboBox.disabledBackground", new ColorUIResource(Color.GRAY));
    
        JFrame jf = new JFrame("bonjour");
    
        jf.getContentPane().setLayout(null);
        jcb = new JComboBox();
        jb = new JButton(assign());
    
        jb.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
                comboBoxEnabled = ! comboBoxEnabled;
                jcb.setEnabled(comboBoxEnabled);
                jb.setText(assign());
            }
        });
    
        jcb.addItem("Opção 1");
        jcb.addItem("Opção 2");
        jcb.addItem("Opção 3");
        jcb.setEnabled(true);
        jf.getContentPane().add(jcb);
        jf.getContentPane().add(jb);
        jcb.setBounds(70,30,120,35);
        jb.setBounds(80,140,80,20);
        jb.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jcb.setEnabled(this.comboBoxEnabled);
        jf.setVisible(true);
        jf.setSize(300,300);
    }
    
    private String assign () {
        if (this.comboBoxEnabled) {
           return "Enabled ON";
        } else {
           return "Enabled OFF";            
        }
    }

}
guigo82

E caso eu precise fazer algo como alterar a cor da TitledBorder de uma combobox como faria?

Fiz algumas tentativas como a abaixo mas sem sucesso.

UIManager.put("ComboBox.disabledTitledBorder", new ColorUIResource(Color.red));
Criado 7 de abril de 2004
Ultima resposta 31 de mar. de 2012
Respostas 2
Participantes 3