UIManager color at a JFileChooser

扶醉桌前 提交于 2019-11-26 11:34:10

问题


I\'m using Nimbus Look and Feel, with only 3 changes at its colors:

UIManager.put(\"nimbusSelection\", new Color(164,164,164));
UIManager.put(\"nimbusSelectionBackground\", new Color(214,217,223));
UIManager.put(\"nimbusSelectedText\", Color.BLACK);

My FileChooser looks like this:

\"enter

So selected file\'s name appears in white and looks bad, and it also happens for the file type selected at the combobox. I want to change it to Black, but nimbusSelectedText is already black and is not working.

I also had a look at the Nimbus Defaults guide at http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/_nimbusDefaults.html#primary and i see no parameter at FileChooser or Combobox to fix this.

Any help identifying the necessary parameters? Thanks


回答1:


Well, there is one way possible to do it. You can get the JList from your JFileChooser and modify it:

public boolean getJList(Container c)
{
    Component[] cmps = c.getComponents();
    for (Component cmp : cmps)
    {
        if (cmp instanceof JList)
        {
            modifyJList((JList)cmp);
            return true;
        }
        if (cmp instanceof Container)
        {
            if(getJList((Container) cmp)) return true;
        }
    }
    return false;
}
private void modifyJList(JList list)
{
    // Here you can modify your JList
}

and to use it, just call getJList():

JFileChooser chooser = new JFileChooser();
getJList(chooser);



回答2:


JFileChooser is compound JComponent, you can extract JButtons, JToggleButtons and JScrollPane with JViewPort that contains JList, please download Darryl's Swing Utils , read descriptions, then run (Darryl's) code, result is selection for JList or JTable (I voting for that)

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
import javax.swing.plaf.metal.MetalButtonUI;

public class CrazyFileChooser {

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

            @Override
            public void run() {
                new CrazyFileChooser().makeUI();
            }
        });
    }

    public void makeUI() {
        JFileChooser chooser = new JFileChooser();
        for (AbstractButton button : SwingUtils.getDescendantsOfType(AbstractButton.class, chooser)) {
            button.setUI(new XORButtonUI());
        }
        for (JList list : SwingUtils.getDescendantsOfType(JList.class, chooser)) {
            list.setBackground(Color.PINK);
        }
        chooser.showOpenDialog(null);
    }
}

class XORButtonUI extends MetalButtonUI {

    @Override
    public void paint(Graphics g, JComponent c) {
        g.setXORMode(Color.YELLOW);
        super.paint(g, c);
    }
} 



回答3:


I really don't know, but have you tried setting this properties:

List.selectionForceground
List.selectionBackground

A FileChooser looks pretty much like a list...


Second try. Maybe settings these helps:

controlHighlight
controlLHighlight


来源:https://stackoverflow.com/questions/6758719/uimanager-color-at-a-jfilechooser

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