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 two button search and send 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 and if i am clicking send button i want to send that selected file in to client window. please help me to create this and send me the source code using Java Swing

View Answers

August 22, 2011 at 3:47 PM


I think this will help you, try this
package classes.SearchFile;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextField;

/**
 * @author Ajmal Muhammad P
 *
 */

@SuppressWarnings("serial")
public class SearchFile extends JFrame implements ActionListener {

        private JTextField textField;
        private JButton searchButton, sendButton;
        private JFileChooser fileChooser;

    /**
     * @param args
     */

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new SearchFile();

    }

    public SearchFile() {
        // TODO Auto-generated constructor stub
        super("Search File");
        textField = new JTextField();
        searchButton = new JButton("Search");
        sendButton = new JButton("Send");
        fileChooser = new JFileChooser();

        go();

        this.setVisible(true);

    }

    private void go() {
        // TODO Auto-generated method stub
        Dimension scrn = Toolkit.getDefaultToolkit().getScreenSize();

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setBounds(scrn.width/2 - 250, scrn.height/2 - 35, 500, 70);

        this.getContentPane().add(searchButton, BorderLayout.WEST);
        this.getContentPane().add(sendButton, BorderLayout.EAST);
        this.getContentPane().add(textField);

        searchButton.addActionListener(this);

    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        // TODO Auto-generated method stub
        Object source = ae.getSource();
        if(source == searchButton) {
            int choice = fileChooser.showOpenDialog(this);
            if(choice == JFileChooser.APPROVE_OPTION) textField.setText(fileChooser.getSelectedFile().getAbsolutePath());
        }
        else if(source == sendButton) {
            // Do what u want here

        }

    }

}









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.