Solved

Need JFilechooser Functionality on a Parent Panel

Posted on 2008-10-16
7
1,102 Views
Last Modified: 2013-12-14
Hi,
I need the functionality similar to a GUI FTP application...

I need to create a JDialog or JPanel that contains two sets of FileChoosers - one being the "Source",
and the other being the "Destination". Each Chooser sub-panel needs to have basic directory and file navigation, single and multiple file selection capabilities, and have custom buttons including "Select All",
De-Select All", and "Delete.

Besides those functions, there is a button common to both sub-paels "Copy" that will copy the
selected file(s) from the source to the destination directory.

I've seen how you can add accessories to a JFileChooser, but have not seen the functionality
described above. Specifically how to glue the FileChooser widgets to a parent panel (JPanel or
JDialog or ???).

So - is there an "easy" way to embed the file choosers on a parent panel (that I can then add
some custom controls to), or will I have to "roll my own" - the hard way???

Thanks
0
Comment
Question by:dbCnc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 22738832
>> So - is there an "easy" way to embed the file choosers on a parent panel (that I can then add
>> some custom controls to), or will I have to "roll my own" - the hard way???
I would say just add the JFileChooser to the panel and then pass all the settings the panel "receives" through setters to the JFileChooser member
0
 

Author Comment

by:dbCnc
ID: 22741044
Thanks for the reply...

I've tried to add the JFileChooser to my JDialog (and a JFrame), but it is not visible. I've only seen
a JFileChooser pop up as an stand alone dialog. Can they be embedded in a JDialog, JPanel or JFrame?

I've attached a standalone code snippet of how I tried to implement your suggestion.


import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.WindowConstants;
 
public class TestFileChooserDialog extends JDialog {
 
	private class okButtonListener implements ActionListener {
 
		@SuppressWarnings("unchecked")
		// generics...bleh
		public void actionPerformed(ActionEvent e) {
			// The operator selected OK
			System.exit(0);
		}
	}
 
	private static final long serialVersionUID = 1L;
 
	/**
	 * Create the dialog
	 */
	public TestFileChooserDialog() {
		super();
 
		setAlwaysOnTop(true);
 
		setResizable(false);
		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		setTitle("File Select");
		getContentPane().setLayout(null);
		setName("FileSelectDlg");
		getContentPane().setName("fileSelectPanel");
		setBounds(948, 80, 329, 318);
 
		final JButton okButton = new JButton();
		okButton.addActionListener(new okButtonListener());
		okButton.setText("OK");
		okButton.setBounds(44, 230, 75, 26);
		getContentPane().add(okButton);
 
		
		
		JFileChooser fileChooser = new JFileChooser();
		
		// let the user traverse directories
		fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 
		
		getContentPane().add(fileChooser); // <<===============
	}
 
	
	public static void main(String args[]) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TestFileChooserDialog dialog = new TestFileChooserDialog();
					dialog.addWindowListener(new WindowAdapter() {
						@Override
						public void windowClosing(WindowEvent e) {
							System.exit(0);
						}
					});
					dialog.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
}

Open in new window

0
 
LVL 37

Expert Comment

by:zzynx
ID: 22741447
I'm afraid I was wrong.

//Create a file chooser
final JFileChooser fc = new JFileChooser();
...
//In response to a button click:
int returnVal = fc.showOpenDialog(aComponent);

You want to embed the result of what you see due to the last call in another panel, isn't it?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dbCnc
ID: 22744149

zzynx - No, I wanted the FileChooser "widgets" on the same panel as other buttons.

Please take a look at the attached FileSelect.bmp file (or just run the code I've attached)
After re-thinking this, I don't really need to allow the operator to traverse to different directories - I may
actually have to constrain him to a hardcoded data file directory (security/system integrity issues).

As I said, I've attached both the source, and the resulting bitmap image of my current JDialog, and will
probably not use a JFileChooser at this point (although I'd still like to know if you can put 2 JFileChoosers
on the same parent panel...)

Now I'll start looking for code to list the files in a given directory - and to populate the JLists.

Thanks - I look forward to any other thoughts on this...
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
 
public class TestFileChooserDialog extends JDialog {
 
	private JList destList;
	private JComboBox destDiskComboBox;
	private JComboBox destComboBox;
	private JList sourceList;
	private JComboBox sourceDiskComboBox;
	private JComboBox sourceComboBox;
	private class closeButtonListener implements ActionListener {
 
		@SuppressWarnings("unchecked")
		// generics...bleh
		public void actionPerformed(ActionEvent e) {
			// The operator selected OK
			System.exit(0);
		}
	}
 
	private static final long serialVersionUID = 1L;
 
	/**
	 * Create the dialog
	 */
	public TestFileChooserDialog() {
		super();
 
		setAlwaysOnTop(true);
 
		setResizable(false);
		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		setTitle("File Select");
		getContentPane().setLayout(null);
		setName("FileSelectDlg");
		getContentPane().setName("fileSelectPanel");
		setBounds(948, 80, 329, 617);
 
		final JButton closeButton = new JButton();
		closeButton.addActionListener(new closeButtonListener());
		closeButton.setText("Close");
		closeButton.setBounds(212, 535, 75, 26);
		getContentPane().add(closeButton);
 
		final JButton copyButton = new JButton();
		copyButton.setText("Copy");
		copyButton.setBounds(40, 535, 75, 26);
		getContentPane().add(copyButton);
 
		sourceComboBox = new JComboBox();
		sourceComboBox.setModel(new DefaultComboBoxModel(new String[] {"Computer 1", "Computer 2", "Computer 3"}));
		sourceComboBox.setBounds(80, 40, 110, 25);
		getContentPane().add(sourceComboBox);
 
		final JLabel sourceLabel = new JLabel();
		sourceLabel.setHorizontalAlignment(SwingConstants.TRAILING);
		sourceLabel.setText("Source");
		sourceLabel.setBounds(5, 45, 70, 16);
		getContentPane().add(sourceLabel);
 
		sourceDiskComboBox = new JComboBox();
		sourceDiskComboBox.setModel(new DefaultComboBoxModel(new String[] {"A", "B"}));
		sourceDiskComboBox.setBounds(256, 40, 53, 25);
		getContentPane().add(sourceDiskComboBox);
 
		final JLabel sourceDiskLabel = new JLabel();
		sourceDiskLabel.setText("Disk");
		sourceDiskLabel.setBounds(223, 46, 35, 16);
		getContentPane().add(sourceDiskLabel);
 
		final JButton sourceSelectAllButton = new JButton();
		sourceSelectAllButton.setText("Select All");
		sourceSelectAllButton.setBounds(205, 75, 106, 26);
		getContentPane().add(sourceSelectAllButton);
 
		final JButton sourceDeselectAllButton = new JButton();
		sourceDeselectAllButton.setText("Deselect All");
		sourceDeselectAllButton.setBounds(205, 107, 106, 26);
		getContentPane().add(sourceDeselectAllButton);
 
		final JButton sourceRefreshButton = new JButton();
		sourceRefreshButton.setText("Refresh");
		sourceRefreshButton.setBounds(205, 139, 106, 26);
		getContentPane().add(sourceRefreshButton);
 
		final JButton sourceDeleteButton = new JButton();
		sourceDeleteButton.setText("Delete");
		sourceDeleteButton.setBounds(205, 225, 106, 26);
		getContentPane().add(sourceDeleteButton);
 
		final JSeparator separator = new JSeparator();
		separator.setBounds(0, 267, 323, 20);
		getContentPane().add(separator);
 
		sourceList = new JList();
		sourceList.setModel(new DefaultComboBoxModel(new String[] {"/tmpDirxyz/file123.dat", "/tmpDirxyz/file456.dat", "/tmpDirxyz/file789.dat"}));
		sourceList.setBounds(15, 93, 172, 158);
		getContentPane().add(sourceList);
 
		destComboBox = new JComboBox();
		destComboBox.setModel(new DefaultComboBoxModel(new String[] {"Computer 1", "Computer 2"}));
		destComboBox.setBounds(80, 286, 110, 25);
		getContentPane().add(destComboBox);
 
		final JLabel destLabel = new JLabel();
		destLabel.setHorizontalAlignment(SwingConstants.TRAILING);
		destLabel.setText("Destination");
		destLabel.setBounds(5, 291, 70, 16);
		getContentPane().add(destLabel);
 
		destDiskComboBox = new JComboBox();
		destDiskComboBox.setModel(new DefaultComboBoxModel(new String[] {"A", "B"}));
		destDiskComboBox.setBounds(251, 286, 53, 25);
		getContentPane().add(destDiskComboBox);
 
		final JLabel destDiskLabel = new JLabel();
		destDiskLabel.setText("Disk");
		destDiskLabel.setBounds(218, 292, 35, 16);
		getContentPane().add(destDiskLabel);
 
		final JButton destSelectAllButton = new JButton();
		destSelectAllButton.setText("Select All");
		destSelectAllButton.setBounds(200, 321, 106, 26);
		getContentPane().add(destSelectAllButton);
 
		final JButton destDeselectAllButton = new JButton();
		destDeselectAllButton.setText("Deselect All");
		destDeselectAllButton.setBounds(200, 353, 106, 26);
		getContentPane().add(destDeselectAllButton);
 
		final JButton destRefreshButton = new JButton();
		destRefreshButton.setText("Refresh");
		destRefreshButton.setBounds(200, 385, 106, 26);
		getContentPane().add(destRefreshButton);
 
		final JButton destDeleteButton = new JButton();
		destDeleteButton.setText("Delete");
		destDeleteButton.setBounds(200, 471, 106, 26);
		getContentPane().add(destDeleteButton);
 
		destList = new JList();
		destList.setBounds(10, 339, 172, 158);
		getContentPane().add(destList);
 
		final JSeparator separator_1 = new JSeparator();
		separator_1.setBounds(0, 515, 323, 20);
		getContentPane().add(separator_1);
 
	}
 
	
	public static void main(String args[]) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TestFileChooserDialog dialog = new TestFileChooserDialog();
					dialog.addWindowListener(new WindowAdapter() {
						@Override
						public void windowClosing(WindowEvent e) {
							System.exit(0);
						}
					});
					dialog.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
}

Open in new window

FileSelect.bmp
0
 
LVL 37

Accepted Solution

by:
zzynx earned 500 total points
ID: 22749529
>> Now I'll start looking for code to list the files in a given directory
Listing the Files or Subdirectories in a Directory: http://www.exampledepot.com/egs/java.io/GetFiles.html
0
 

Author Closing Comment

by:dbCnc
ID: 31506915
Thanks for spending the time and working through this.

That link did the job...
0
 
LVL 37

Expert Comment

by:zzynx
ID: 22758564
thanx 4 axxepting
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question