Jamsiya N P
question
1 Answer(s)      a year and 9 months ago
Posted in : Java Beginners

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

View Answers

August 20, 2011 at 5:43 PM


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);
        }
}









Related Pages:
question
question   sir plz tell me what should i give in title box. just i want java program for the question typed in this area
question
question   sir plz tell me what should i give in title box. just i want java program for the question typed in this area
Question
Question   When there is an exception in my program how java runtime system handles
question
question  dear sir/madam my question is how to compare two text format in java..we are java beginners..so we need the complete source code for above mentioned question...we have to compare each and every word
question
question  Dear sir i had some typing mistake at previous question so its my humble request to let me know the steps to start the tomcat6 under the tomcat directory
question
question  Gud morning sir, I have asked u some question regarding jsp in saturaday for that i didnot find any answere in which u send me the some of the links.U have asked me the specify some details. There is a entity name
Question?
Question?  My question is how to: Add a menu bar to the program with a File menu. In the File menu, add a submenu (JMenuItem) called About. When the user clicks on the About menu item, display a JOptionPane message dialog

Ask Questions?

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.