更改JFileChooser的颜色

时间:2009-10-12 17:03:24

标签: jfilechooser

我试图根据这篇文章中的一些想法改变我的JFileChooser的颜色:

Change color of WindowsPlacesBar in JFileChooser

但是我无法这样做,我找不到丢失的代码。直到现在我才得到这个:

    UIManager.put("Panel.background", Color.BLACK);
    UIManager.put("Button.background", Color.BLACK);
    UIManager.put("ComboBox.background", Color.BLACK);
    UIManager.put("ComboBox.foreground", Color.WHITE);

    UIManager.put("TextField.background", Color.BLACK);
    UIManager.put("TextField.foreground", Color.WHITE);
    UIManager.put("ToolBar.background", Color.BLACK);

    UIManager.put("Viewport.background", Color.BLACK);
    UIManager.put("Viewport.foreground", Color.WHITE);
    SwingUtilities.updateComponentTreeUI(this);

我希望有人可以帮助我。

3 个答案:

答案 0 :(得分:2)

UIManager.put("ScrollPane.background", Color.BLACK);
UIManager.put("List.background", Color.BLACK);
UIManager.put("List.foreground", Color.WHITE);

或者,如果您愿意:

private static void setColors(Component c, Color fg, Color bg) {
    setColors0(c.getAccessibleContext(), fg, bg);
}

private static void setColors0(AccessibleContext ac, Color fg, Color bg) {
    ac.getAccessibleComponent().setForeground(fg);
    ac.getAccessibleComponent().setBackground(bg);
    int n = ac.getAccessibleChildrenCount();
    for (int i=0; i<n; i++) {
        setColors0(ac.getAccessibleChild(i).getAccessibleContext(), fg, bg);
    }
}

答案 1 :(得分:0)

我搞砸了一下这个想法:

   public void setFileChooserColors(JFileChooser jfc, Color f, Color b){
        jFileChooser1.setForeground(f);
        jFileChooser1.setBackground(b);

for (int index1 = 0; index1 < jfc.getAccessibleContext().getAccessibleChildrenCount(); index1++){ jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleComponent().setForeground(f); jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleComponent().setBackground(b); for (int index2 = 0; index2 < jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChildrenCount(); index2++){ jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleComponent().setForeground(f); jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleComponent().setBackground(b); for (int index3 = 0; index3 < jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChildrenCount(); index3++){ jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleComponent().setBackground(b); jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleComponent().setForeground(f); for (int index4 = 0; index4 < jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChildrenCount(); index4++){ jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleComponent().setBackground(b); jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleComponent().setForeground(f); for (int index5 = 0; index5 < jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChildrenCount(); index5++){ jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleComponent().setBackground(b); jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleComponent().setForeground(f); for (int index6 = 0; index6 < jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleChildrenCount(); index6++){ jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleChild(index6).getAccessibleContext().getAccessibleComponent().setBackground(b); jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleChild(index6).getAccessibleContext().getAccessibleComponent().setForeground(f); for (int index7 = 0; index7 < jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleChild(index6).getAccessibleContext().getAccessibleChildrenCount(); index7++){ jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleChild(index6).getAccessibleContext().getAccessibleChild(index7).getAccessibleContext().getAccessibleComponent().setBackground(b); jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleChild(index6).getAccessibleContext().getAccessibleChild(index7).getAccessibleContext().getAccessibleComponent().setForeground(f); for (int index8 = 0; index8 < jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleChild(index6).getAccessibleContext().getAccessibleChild(index7).getAccessibleContext().getAccessibleChildrenCount(); index8++){ jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleChild(index6).getAccessibleContext().getAccessibleChild(index7).getAccessibleContext().getAccessibleChild(index8).getAccessibleContext().getAccessibleComponent().setBackground(b); jfc.getAccessibleContext().getAccessibleChild(index1).getAccessibleContext().getAccessibleChild(index2).getAccessibleContext().getAccessibleChild(index3).getAccessibleContext().getAccessibleChild(index4).getAccessibleContext().getAccessibleChild(index5).getAccessibleContext().getAccessibleChild(index6).getAccessibleContext().getAccessibleChild(index7).getAccessibleContext().getAccessibleChild(index8).getAccessibleContext().getAccessibleComponent().setForeground(f); } } } } } } } } }<code>

答案 2 :(得分:0)

您需要创建一个CustomFileChooser类并覆盖createDialog(Component parent)方法并设置对话框的contentPane背景颜色并在对话框中设置每个组件的背景,

public class CustomFileChooser extends JFileChooser{
    public CustomFileChooser (){
        super();
        initComponent();
    }

    public CustomFileChooser (String currentDirPath){
        super(currentDirPath);
        initComponent();
    }

    public void initComponent(){
        Component c[] = getComponents();
        customizeUI(c);
    }


public void customizeUI(Component[] c){
    for(int i=0;i<c.length;i++){
        if(c[i] instanceof JPanel){
            ((JPanel)c[i]).setBackground(Color.black);
            if(((JPanel)c[i]).getComponentCount() !=0){
                customizeUI(((JPanel)c[i]).getComponents());
            }
        }
        if(c[i] instanceof JTextField){
            System.out.println("JTextField");
            ((JTextField)c[i]).setBackground(Color.RED);
        }
        if(c[i] instanceof JToggleButton){
            System.out.println("JToggleButton");
        }
        if(c[i] instanceof JScrollPane){
            System.out.println("JScrollPane");
        }

    }   
}
    @Override
    protected JDialog createDialog(Component parent) throws HeadlessException {
    FileChooserUI ui = getUI();
    String title = ui.getDialogTitle(this);
    putClientProperty(AccessibleContext.ACCESSIBLE_DESCRIPTION_PROPERTY,
                      title);

    JDialog dialog;

    if (parent instanceof Frame) {
        dialog = new JDialog((Frame)parent, title, true);
    } else {
        dialog = new JDialog((Dialog)parent, title, true);
    }
    dialog.setComponentOrientation(this.getComponentOrientation());

    Container contentPane = dialog.getContentPane();
    contentPane.setLayout(new BorderLayout());
    contentPane.add(this, BorderLayout.CENTER);
    contentPane.setBackground(Color.black);


    dialog.pack();
    dialog.setLocationRelativeTo(parent);

    return dialog;
}}