
Sir,
If i have a frame with one text field and one button search using java swing .If i am clicking a search button then i want to display the file chooser and i want to display the selected file on the text field of frame . please help me to create this and send me the source code using Java Swing

import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.filechooser.*;
public class SelectFile extends JFrame{
public static void main(String[]args){
JFrame frame = new JFrame();
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 100);
Container container = frame.getContentPane();
container.setLayout(new GridBagLayout());
final JTextField text=new JTextField(20);
JButton b=new JButton("Search");
text.setBounds(20,20,120,20);
b.setBounds(150,20,80,20);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
JFileChooser fc = new JFileChooser();
int returnval = fc.showOpenDialog(null);
if (returnval == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
text.setText(file.getPath());
}
}
});
container.add(text);
container.add(b);
frame.setVisible(true);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.