How to Create a Jpanel That Reads That Uploads to a File in Java
JFileChooser is a function of java Swing bundle. The java Swing package is part of JavaTM Foundation Classes(JFC) . JFC contains many features that aid in building graphical user interface in java . Coffee Swing provides components such every bit buttons, panels, dialogs, etc . JFileChooser is a piece of cake and an effective way to prompt the user to cull a file or a directory .
In this article we will run across how to use JFileChooser in java swing .
Constructors of JFileChooser are :
1. JFileChooser() – empty constructor that points to user's default directory
Java
JFileChooser j = new JFileChooser();
j.showSaveDialog( null );
Output of the code snippet:
two. JFileChooser(String) – uses the given path
Java
JFileChooser j = new JFileChooser( "d:" );
j.showSaveDialog( nil );
Output of the code snippet:
3. JFileChooser(File) – uses the given File as the path
Java
JFileChooser j = new JFileChooser( new File( "C:\\Users\\pc\\Documents\\New binder\\" ));
j.showSaveDialog( zippo );
Output of the lawmaking snippet:
iv. JFileChooser(FileSystemView) – uses the given FileSystemView
Java
JFileChooser j = new JFileChooser(FileSystemView.getFileSystemView());
j.showSaveDialog( null );
Output of the code snippet:
5. JFileChooser(String, FileSystemView) – uses the given path and the FileSystemView
Java
JFileChooser j = new JFileChooser( "d:" , FileSystemView.getFileSystemView());
j.showSaveDialog( zero );
Output of the code snippet:
6. JFileChooser(File, FileSystemView) – uses the given current directory and the FileSystemView
Java
File f = new File( "C:\\Users\\pc\\Documents\\New folder\\" );
JFileChooser j = new JFileChooser(f, FileSystemView.getFileSystemView());
j.showSaveDialog( cipher );
Output of the code snippet:
Note : The code given to a higher place are code snippets not the full lawmaking, the code snippets given above should be used to invoke the constructor as per the need and discretion of the programmer, the paths mentioned above are arbitrary. User should set the path according to their demand.
Practical Applications of JFileChooser
The following codes will not execute in an online compiler. Please use an offline IDE
1. Creating an open or save dialog using JFileChooser
Java
import java.io.*;
import javax.swing.*;
import java.awt.consequence.*;
import javax.swing.filechooser.*;
class filechooser extends JFrame implements ActionListener {
static JLabel l;
filechooser()
{
}
public static void principal(String args[])
{
JFrame f = new JFrame( "file chooser" );
f.setSize( 400 , 400 );
f.setVisible( true );
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button1 = new JButton( "salve" );
JButton button2 = new JButton( "open" );
filechooser f1 = new filechooser();
button1.addActionListener(f1);
button2.addActionListener(f1);
JPanel p = new JPanel();
p.add(button1);
p.add(button2);
l = new JLabel( "no file selected" );
p.add(50);
f.add together(p);
f.show();
}
public void actionPerformed(ActionEvent evt)
{
Cord com = evt.getActionCommand();
if (com.equals( "salvage" )) {
JFileChooser j = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
int r = j.showSaveDialog( null );
if (r == JFileChooser.APPROVE_OPTION)
{
l.setText(j.getSelectedFile().getAbsolutePath());
}
else
50.setText( "the user cancelled the operation" );
}
else {
JFileChooser j = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
int r = j.showOpenDialog( nix );
if (r == JFileChooser.APPROVE_OPTION)
{
l.setText(j.getSelectedFile().getAbsolutePath());
}
else
l.setText( "the user cancelled the operation" );
}
}
}
2. Utilize JFileChooser to select directory only
Java
import java.io.*;
import javax.swing.*;
import java.awt.result.*;
import javax.swing.filechooser.*;
form filechooser extends JFrame implements ActionListener {
static JLabel l;
filechooser()
{
}
public static void main(Cord args[])
{
JFrame f = new JFrame( "file chooser to select directories" );
f.setSize( 400 , 400 );
f.setVisible( truthful );
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button1 = new JButton( "relieve" );
JButton button2 = new JButton( "open up" );
filechooser f1 = new filechooser();
button1.addActionListener(f1);
button2.addActionListener(f1);
JPanel p = new JPanel();
p.add(button1);
p.add(button2);
l = new JLabel( "no file selected" );
p.add(l);
f.add(p);
f.bear witness();
}
public void actionPerformed(ActionEvent evt)
{
String com = evt.getActionCommand();
if (com.equals( "save" )) {
JFileChooser j = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
j.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int r = j.showSaveDialog( null );
if (r == JFileChooser.APPROVE_OPTION) {
l.setText(j.getSelectedFile().getAbsolutePath());
}
else
fifty.setText( "the user cancelled the operation" );
}
else {
JFileChooser j = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
j.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int r = j.showOpenDialog( null );
if (r == JFileChooser.APPROVE_OPTION) {
fifty.setText(j.getSelectedFile().getAbsolutePath());
}
else
l.setText( "the user cancelled the operation" );
}
}
}
3. Utilize JFileChooser to allow multiple selection of files
Java
import java.io.*;
import javax.swing.*;
import java.awt.upshot.*;
import javax.swing.filechooser.*;
course filechooser extends JFrame implements ActionListener {
static JLabel l;
filechooser()
{
}
public static void main(String args[])
{
JFrame f = new JFrame( "file chooser to select multiple files at a time" );
f.setSize( 400 , 400 );
f.setVisible( true );
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button1 = new JButton( "relieve" );
JButton button2 = new JButton( "open up" );
filechooser f1 = new filechooser();
button1.addActionListener(f1);
button2.addActionListener(f1);
JPanel p = new JPanel();
p.add(button1);
p.add(button2);
l = new JLabel( "no file selected" );
p.add(fifty);
f.add(p);
f.testify();
}
public void actionPerformed(ActionEvent evt)
{
String com = evt.getActionCommand();
if (com.equals( "save" )) {
JFileChooser j = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
j.setMultiSelectionEnabled( true );
int r = j.showSaveDialog( aught );
if (r == JFileChooser.APPROVE_OPTION) {
File files[] = j.getSelectedFiles();
int t = 0 ;
l.setText( "" );
while (t++ < files.length)
50.setText(fifty.getText() + " " + files[t - 1 ].getName());
}
else
l.setText( "the user cancelled the operation" );
}
else {
JFileChooser j = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
j.setMultiSelectionEnabled( true );
int r = j.showOpenDialog( zip );
if (r == JFileChooser.APPROVE_OPTION) {
File files[] = j.getSelectedFiles();
l.setText( "" );
int t = 0 ;
while (t++ < files.length)
l.setText(l.getText() + " " + files[t - 1 ].getName());
}
else
l.setText( "the user cancelled the functioning" );
}
}
}
4. Utilize JFileChooser to restrict the type of files shown to the user
Java
import java.io.*;
import javax.swing.*;
import coffee.awt.event.*;
import javax.swing.filechooser.*;
class filechooser extends JFrame implements ActionListener {
static JLabel l;
filechooser()
{
}
public static void main(String args[])
{
JFrame f = new JFrame( "file chooser" );
f.setSize( 400 , 400 );
f.setVisible( true );
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button1 = new JButton( "save" );
JButton button2 = new JButton( "open" );
filechooser f1 = new filechooser();
button1.addActionListener(f1);
button2.addActionListener(f1);
JPanel p = new JPanel();
p.add(button1);
p.add(button2);
50 = new JLabel( "no file selected" );
p.add together(l);
f.add together(p);
f.show();
}
public void actionPerformed(ActionEvent evt)
{
Cord com = evt.getActionCommand();
if (com.equals( "save" )) {
JFileChooser j = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
j.setAcceptAllFileFilterUsed( false );
j.setDialogTitle( "Select a .txt file" );
FileNameExtensionFilter restrict = new FileNameExtensionFilter( "Simply .txt files" , "txt" );
j.addChoosableFileFilter(restrict);
int r = j.showSaveDialog( goose egg );
if (r == JFileChooser.APPROVE_OPTION)
{
l.setText(j.getSelectedFile().getAbsolutePath());
}
else
l.setText( "the user cancelled the operation" );
}
else {
JFileChooser j = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
j.setAcceptAllFileFilterUsed( false );
j.setDialogTitle( "Select a .txt file" );
FileNameExtensionFilter restrict = new FileNameExtensionFilter( "Only .txt files" , "txt" );
j.addChoosableFileFilter(restrict);
int r = j.showOpenDialog( null );
if (r == JFileChooser.APPROVE_OPTION) {
l.setText(j.getSelectedFile().getAbsolutePath());
}
else
l.setText( "the user cancelled the functioning" );
}
}
}
Annotation :
y'all can also customize the approve button by using the function setApproveButtonText(String) . This will fix the text of the approved push button
to the desired text.
Source: https://www.geeksforgeeks.org/java-swing-jfilechooser/
0 Response to "How to Create a Jpanel That Reads That Uploads to a File in Java"
Post a Comment