wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes changing background color of JFileChooser.-urgent Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "changing background color of JFileChooser.-urgent" Watch "changing background color of JFileChooser.-urgent" New topic
Author

changing background color of JFileChooser.-urgent

meera sood
Ranch Hand

Joined: Dec 18, 2001
Posts: 50
i am facing a curious problem. When i try to change the background color of the JFileChooser, I simply cannot do it. See, the JFileChooser control has 3 distinct parts-the drive chooser, the area where files are shown and the text box and Button where he opens the file and selects the file type. When I try to change the background color(For instance set it to white), I can do it for the area beteeen the 3 parts, but not the area between the sub-parts of the area.I do not know if i have made myself clear, please try the following code, to see what i mean.

Please help me out....it is very very urgent!!!
thanks in advance....
Meera
Mahesh swami
Greenhorn

Joined: Aug 13, 2002
Posts: 26
I think you need to extend FileChooserUI class and override installComponent() method. Then you set this Custom File Chooser Ui by using FileChooser.setUI(Your Custom UI class) .In this method you can copy the same code from MetalFileChooserUI class and just set the background of all the components created in the method to the color that you want .I think this will solve your problem.
meera sood
Ranch Hand

Joined: Dec 18, 2001
Posts: 50
hi mahesh,
thanx for your reply. I am new to swing ,and could not exactly get what you are trying toexplain. Could you please explain it in a bit more detail, and if possible, post a snippet of code. Moreover, i am adding more contols also to this JFrame, which i had not posted in my code.
Thanx in advance again, but please do help me out....
regards,
Meera
meera sood
Ranch Hand

Joined: Dec 18, 2001
Posts: 50
p.s...
figured out what you are trying to tell me(well-kind of ).But where do i get the code for MetalFileChooserUI. Can you please help me out--its my deadline today!!! ...
thanx
Meera
Mahesh swami
Greenhorn

Joined: Aug 13, 2002
Posts: 26
/* MetalFileChooserUI code deleted by moderator...

This was taking up a lot of space in the thread, and if anyone needed to see it, they could get it from their source.jar file...
[ August 13, 2002: Message edited by: Nathan Pruett ]
Mahesh swami
Greenhorn

Joined: Aug 13, 2002
Posts: 26
Hello Meera,
Following code will give u a hint regarding how to go about it
import javax.swing.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.*;
import javax.swing.event.*;
import javax.swing.plaf.*;
import javax.swing.plaf.basic.*;
import javax.swing.table.*;
import javax.swing.text.Position;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DateFormat;
import java.util.*;
import javax.swing.plaf.metal.*;
import java.awt.*;
public class testFileChooser extends JFrame{
Container cont = null;
JFileChooser files;
public testFileChooser(){
cont = getContentPane();
cont.setBackground(Color.white);
files = new CustomChooser();
files.setBounds(0,0,400,375);
files.setBackground(Color.white);
files.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
cont.add(files);
}
public static void main(String args[]) {
testFileChooser tfc = new testFileChooser();
tfc.setSize(500,500);
tfc.setVisible(true);
}
}
class CustomChooser extends JFileChooser
{
public CustomChooser(){
super();
setUI( new CustomChooserUI(this));
}
}
class CustomChooserUI extends MetalFileChooserUI{
public CustomChooserUI(JFileChooser filechooser) {
super(filechooser);
}
public void installComponents(JFileChooser fc) {
super.installComponents(fc);
getBottomPanel().setBackground(Color.white);
getButtonPanel().setBackground(Color.white);
}
}
meera sood
Ranch Hand

Joined: Dec 18, 2001
Posts: 50
thanx...
sorry for all this trouble..i managed to get the source code out of the src.jar file. Now what if i want to change the background and foreground colors of the directoryComboBox etc also...how do i go about that...
actually, am working on a project where the client wants real jazzy colors for the interface, so have to set the background and foreground colors of all the components....
thanx again for all the trouble...
Meera
Mahesh swami
Greenhorn

Joined: Aug 13, 2002
Posts: 26
Hi meera,
see there are some variables like directoryComboBox and many other which are private in super class ie. MetalFileCHooserUI.
Now you can do one thing .
Override installComponents() in your custom chooser class
And copy the code form MetalFileChooserUI.installComponents()
paste it in your installComponnet().
Now check which variables are private in super class those are used in installCompnent
and you can creat local instances
public void installComponents(JFileChooser fc){
//Since directoryComboBox is private in super class so comment the following
//directoryComboBox = new JComboBox();
JComboBox directoryComboBox = new JComboBox();
}
I know it will be very tedious task, but I dont know any other way to do it.
Hope u will complete it before dead line
meera sood
Ranch Hand

Joined: Dec 18, 2001
Posts: 50
hi mahesh,
thanx...well does this mean that if i create local instances of these private variables, i will have to set their actionListeners and all also again.... that'll be good as writing my very own JFileChooser ...right???
please help!!!
meera
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
why don't you get one of the lookandfeels out there and use it for your ui. you don't have to do work, that has already been done and is available free.
see:
http://www.javootoo.com/
cheers
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: changing background color of JFileChooser.-urgent