thebighoncho
thebighoncho

Reputation: 405

Using JFrame and JFileChooser to select a File

I am attempting to integrate JFileChooser into my program. Essentially, I would like to have a interface to select a CSV file to be read into my program. I'm trying to do this using JFileChooser. Examples I've seen elsewhere show this being done, but the JFileChooser opens up right away without the JFrame. Is there a way to have JFileChooser be a child element of my JFrame element?

My code is below:

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

public class QuitButtonExample extends JFrame {
    JPanel panel = new JPanel();

    public QuitButtonExample() {
        initUI();
        quitButton();
        menu();
        fileChooser();
    }

    private void initUI() {
        JLabel label1 = new JLabel(
            "Selct the .csv file contaning the addresses to be geocoded...");
        label1.setBounds(0, 0, 500, 50);
        panel.add(label1); 
        getContentPane().add(panel);
        setSize(1000, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private void quitButton() {

        // Quit Button

        panel.setLayout(null);
        JButton quitButton = new JButton("Quit");
        quitButton.setBounds(0, 50, 80, 30);

        quitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });
        panel.add(quitButton);
        setTitle("Quit Button");
    }

    private void menu() {
        // Menu Bar

        // "File"
        JMenuBar menuBar = new JMenuBar();
        JMenu file = new JMenu("File");
        JMenuItem menuItem = new JMenuItem("Exit"); // eMenuItem
        menuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });

        file.add(menuItem);
        menuBar.add(file);

        // "Credits"
        JMenu credits = new JMenu("Credits");
        JMenuItem about = new JMenuItem("About...");
        about.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.exit(0); // TODO - New window, showing credits for
                        // project
            }
        });

        credits.add(about);
        menuBar.add(credits);
        setJMenuBar(menuBar);
    }

    private void fileChooser() {
        // FileChooser

        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter(
            "CSV Files", "csv");
        chooser.setFileFilter(filter);
        chooser.setBounds(0, 75, 500, 300);
        panel.add(chooser);
    }

    public static void main(String[] args) {
        System.out.println("Hello World");
        QuitButtonExample ex = new QuitButtonExample();
        ex.setVisible(true);
    }
}

Upvotes: 0

Views: 15148

Answers (3)

Rakesh KR
Rakesh KR

Reputation: 6527

You are trying to call fileChooser() in side the constructor. change that one and call fileChooser() inside a ActionListener ie whether a button is clicked or Menuitem is pressed. So after corresponding action JFilechooser will come in the action.

Edit:

JFileChooser fileChooser = new JFileChooser();
int returnVal = fileChooserAddDoc.showOpenDialog(fileChooser);
if (returnVal == JFileChooser.APPROVE_OPTION) {
     file = fileChooser.getSelectedFile();
     .
     .
}

Upvotes: 3

amahfouz
amahfouz

Reputation: 2398

This code works as it should.

You are adding a JFileChooser (which is a JComponent) to a JFrame's panel. Like any other JComponent, the file chooser is nested within the frame. What you are seeing is the frame (evident from the menus) with the file chooser being one of its components.

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347204

Start by not calling QuitButtonExample within the constructor.

Instead, create a menu open called Open (for example) and within it's ActionListener call the method instead

Take a look at How to use File Choosers and Code Conventions for the Java Programming Language

Upvotes: 0

Related Questions