
Hello everyone. This page is an English translation of a Japanese page. (The original Japanese has been slightly rewritten to make it easier to translate into English.)
This time, we used JColorChooser and JFileChooser of Swing
JColorChooser is used by the user to select a color, and JFileChooser is used by the user to select a filen.
We will use the following program as a template.
Source
import javax.swing.JFrame;
public class Test extends JFrame {
public static void main(String[] args) {
new Test();
}
public Test() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(350, 250);
init();
setVisible(true);
}
private void init() {
// installation process
}
}
This time we will adjust the value of the argument written in “setSize(350, 250);” accordingly.
Table of Contents
- JColorChooser
- Add to frame
- Dialog display
- JFileChooser
- Add to frame
- Dialog display
JColorChooser
Add to frame
Normally, JColorChooser is output as a dialog, but it can be added to a JFrame.
The following is an example of adding a JColorChooser to a JFrame.
Source
import java.awt.FlowLayout;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
public class Test2 extends JFrame {
public static void main(String[] args) {
new Test2();
}
public Test2() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(650, 400);
init();
setVisible(true);
}
private void init() {
setLayout(new FlowLayout());
JColorChooser colorChooser = new JColorChooser();
add(colorChooser);
}
}
For the frame size, we have tried many times and “setSize(650, 400);” is just the right setting. The following is the initial display. The “Sample” tab is selected. The capture is in Japanese (サンプル).
If you select the “RGB” tab, a screen for selecting colors with sliders will appear as shown below.
Dialog display
There is a static method for the case of displaying a dialog. It is the showDialog method, and Color is its return value. The arguments are as follows, in order
- Component
- Component of the dialog’s parent.
- String
- Title of the dialog.
- Color
- Initial color when the chooser is displayed.
For example, it would be common to display a dialog when a button is pressed, as shown below.
Source
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
public class Test3 extends JFrame {
public static void main(String[] args) {
new Test3();
}
public Test3() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(150, 100);
init();
setVisible(true);
}
private void init() {
setLayout(new FlowLayout());
JButton button = new JButton("button");
add(button);
button.addActionListener(event -> {
Color color = JColorChooser.showDialog(this, "color selection", null);
System.out.println(color);
});
}
}

At the bottom of the dialog that appears, three buttons will be added. If the cancel button is pressed, the showDialog() method will return null (Japanese “取消”). In the above sample, the returned Color object is displayed on the standard output. For example, the returned Color object can be used to set the text color or background color of a certain component.
The third argument of the showDialog() method specifies the selected color when the dialog is displayed. If it is null, white (R, G, B values of 255) is selected.
For example, to set the initial value of each RGB to 20, 30, or 40, specify as follows
Source
Color color = JColorChooser.showDialog(this, "color selection", new Color(20, 30, 40));
Sponsored Links
JFileChooser
Add to frame
JFileChooser can also be added to JFrame.
The following sample adds a JFileChooser to the displayed frame.
Source
import javax.swing.JFileChooser;
import javax.swing.JFrame;
public class Test5 extends JFrame {
public static void main(String[] args) {
new Test5();
}
public Test5() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(600, 300);
init();
setVisible(true);
}
private void init() {
JFileChooser jFileChooser = new JFileChooser();
add(jFileChooser);
}
}
The timing for saving this capture was after the frame was displayed and the “C:\test” folder was manually selected. The initial value of the displayed folder is the document folder of the logged-in user in windows os.
Dialog display
There is a method for selecting a file in the dialog. It is the showOpenDialog(Component parent) method. The argument parent specifies the parent component. The return value of this method is a value of type int, and the following constants defined in JFileChooser.
- JFileChooser.CANCEL_OPTION
- cancel
- JFileChooser.APPROVE_OPTION
- A file was selected.
- JFileCHooser.ERROR_OPTION
- error
We did not know the pattern of the constant ERROR_OPTION being returned. The constant APPROVE_OPTION was returned when entering a non-existent file name. The actual selected file can be obtained by the getSelectedFile() method.
For example, the following button event will display a dialog.
Source
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
public class Test6 extends JFrame {
public static void main(String[] args) {
new Test6();
}
public Test6() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(150, 100);
init();
setVisible(true);
}
private void init() {
setLayout(new FlowLayout());
JButton button = new JButton("button");
add(button);
button.addActionListener(event -> {
JFileChooser fileChooser = new JFileChooser();
int value = fileChooser.showOpenDialog(this);
if (value == JFileChooser.APPROVE_OPTION) {
System.out.println(fileChooser.getSelectedFile());
}
});
}
}
In the following capture, the “C:\test” folder is displayed and “abc” is manually entered as the file name.
In the above screen, if the “Open (開く)” button is pressed, “C:\test\abc” will be displayed in the console. From the description of the if statement, we can see that the constant APPROVE_OPTION is returned even if the file does not actually exist. Therefore, if you want to check for the existence of a file, you need to describe the process.
That’s all. I hope this is helpful to you.