Link to home
Start Free TrialLog in
Avatar of dbCnc
dbCncFlag for United States of America

asked on

Need JFilechooser Functionality on a Parent Panel

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
Avatar of zzynx
zzynx
Flag of Belgium image

>> 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
Avatar of dbCnc

ASKER

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

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?
Avatar of dbCnc

ASKER


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
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dbCnc

ASKER

Thanks for spending the time and working through this.

That link did the job...
thanx 4 axxepting