I need to insert a JFileChooser dialog in my Swing application, and I would like to customize its look so that it matches that of the other windows. Specifically, I am having troubles:
- changing the background color of the JFileChooser
- changing the shape of the JTextField showing the folder path
I am including three classes below which exemplify the problem I am facing.
This is the custom JDialog class (the idea to embed the JFileChooser in a JDialog came from here Possible to remove Title bar from JFileChooser?)
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FileChooserDialog extends JDialog {
private ModifiedFileChooser modifiedFileChooser;
public FileChooserDialog(ModifiedFileChooser modifiedFileChooser) {
super();
this.modifiedFileChooser = modifiedFileChooser;
setUndecorated(true);
setLayout(new BorderLayout());
add(modifiedFileChooser, BorderLayout.CENTER);
setBackground(new Color(180, 14, 14));
pack();
setVisible(true);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
JButton openB = new JButton("Open");
openB.setPreferredSize(new Dimension(50,20));
frame.setLayout(new BorderLayout());
frame.add(openB, BorderLayout.CENTER);
openB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new FileChooserDialog(new ModifiedFileChooser());
}
});
frame.setPreferredSize(new Dimension(300, 200));
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
}
This is the custom JFileChooser class (I referred to this post on how to customize a JFileChooser Creating custom JFileChooser)
import javax.swing.*;
import java.awt.*;
public class ModifiedFileChooser extends JFileChooser {
public ModifiedFileChooser() {
super();
customizeFileChooser(this.getComponents());
setBackground(new Color(180, 14, 14));
repaint();
}
private void customizeFileChooser(Component[] comp) {
for (int x = 0; x < comp.length; x++) {
if (comp[x] instanceof JScrollPane) {
((JScrollPane) comp[x]).setBackground(new Color(180, 14, 14));
}
if (comp[x] instanceof JPanel) {
((JPanel) comp[x]).setBackground(new Color(180, 14, 14));
customizeFileChooser(((JPanel) comp[x]).getComponents());
Component[] c = ((JPanel) comp[x]).getComponents();
for (int i = 0; i < c.length; i++) {
if (c[i] instanceof JTextField) {
int previousSize = ((JTextField) c[i]).getColumns();
c[i] = new RoundedTextField(previousSize);
}
if (c[i] instanceof JPanel) {
((JPanel) c[i]).setBackground(new Color(180, 14, 14));
}
if (c[i] instanceof JScrollPane) {
((JScrollPane) c[i]).setBackground(new Color(180, 14, 14));
}
}
}
}
}
}
This is the custom JTextField class (the code is taken from this post: Java Swing rounded border for Jtextfield)
import javax.swing.*;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
public class RoundedTextField extends JTextField {
private Shape shape;
public RoundedTextField(int size) {
super(size);
setOpaque(false);
}
protected void paintComponent(Graphics g) {
g.setColor(getBackground());
g.fillRoundRect(0, 0, getWidth()-1, getHeight()-1, 15, 15);
super.paintComponent(g);
}
protected void paintBorder(Graphics g) {
g.setColor(getForeground());
g.drawRoundRect(0, 0, getWidth()-1, getHeight()-1, 15, 15);
}
public boolean contains(int x, int y) {
if (shape == null || !shape.getBounds().equals(getBounds())) {
shape = new RoundRectangle2D.Float(0, 0, getWidth()-1, getHeight()-1, 15, 15);
}
return shape.contains(x, y);
}
}
The dialog that is diaplyed after pressing the button in the frame looks like this:
Ideally, I would want the textfield close to the "File Name:" label to have rounded corners, and the background of the JFileChooser to be red instead of grey.
Many thanks in advance for any help on this!