-
Swing JFileChooser
Hi,
I have a dilemma. It should be possible but I can't seem to add a file chooser into a JPanel. What I am trying to is put a file chooser in my main window of my swing applet. I know it is possible but I can't seem to find the right way to do it.
Any Ideas???
Many Thanks,
Steve
-
Can you post the code that you are using?
~evlich
-
applet?
applets may not access files and thus perhaps not access jfilechooser.
have a look at other threads in this forum.
for applications you can have a look at http://java.sun.com/docs/books/tutor...lechooser.html.
by the way: jfilechooser will open it's own jframe and is not thought as en embedded control.
-
Yes, I am using an applet. I'll explain, what I am trying to do is write an SFTP client for my website but in applet form. I am going to use the JFileChooser as the basis for my drag and drop transfer GUI. I know applets can access files because I have seen FTP client applets so I know it can be done. Please Please Please though if you know a better way to do it then please fill me in. I am very open to changing my methods or techniques if it makes for better software. I have read the tutorial you suggested, infact it has been like my bible thus far!!! lol. Here is my code sofar, Its very rough at the moment, I will clean it all up once I have a working solution.
CODE:
Code:
/*
***********************************
Sting SFtp
Creator: Stephen Cheesley
Designer: Stephen Cheesley
***********************************
*/
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFileChooser;
import java.applet.*;
public class Sting extends JApplet implements ActionListener
{
//Methods to load on applet initialisation
public void init()
{
try
{
javax.swing.SwingUtilities.invokeAndWait(new Runnable()
{
public void run()
{
guiLayout();
}
});
}
catch (Exception e)
{
System.err.println("guiLayout didn't successfully complete");
}
}
public void start()
{
}
public void stop()
{
}
// **********************************************************
// * *
// * This method dictates the GUI Layout of the Applet *
// * *
// **********************************************************
public void guiLayout()
{
//Where the GUI is created:
JMenuBar menuBar;
JMenu file, help;
JMenuItem menuItem;
JPanel welcome, files_local, files_server;
JLabel Wlabel, Llabel, Slabel, about1;
//Create the menu bar.
menuBar = new JMenuBar();
// *****************************************************
//Create file menu.
file = new JMenu("File");
file.setMnemonic(KeyEvent.VK_A);
file.getAccessibleContext().setAccessibleDescription(
"The only menu in this program that has menu items");
menuBar.add(file);
//Add Items to the file menu
menuItem = new JMenuItem("Cut");
file.add(menuItem);
menuItem = new JMenuItem("Copy");
file.add(menuItem);
menuItem = new JMenuItem("Paste");
file.add(menuItem);
file.addSeparator();
menuItem = new JMenuItem("Logout");
file.add(menuItem);
//Create help menu
help = new JMenu("Help");
menuBar.add(help);
//Add Items to the help menu
menuItem = new JMenuItem("Readme");
help.add(menuItem);
// help.addSeparator(); --- Separator
menuItem = new JMenuItem("About Sting...");
help.add(menuItem);
menuItem.addActionListener(this);
//Add the menu bar to the GUI
setJMenuBar(menuBar);
// *****************************************************
// ------------------------------------------------
// This section sets the three main layout panels in
// positions top left and right.
// The top "Welcome" panel
welcome = new JPanel();
welcome.setBackground(Color.white);
getContentPane().add(welcome, BorderLayout.PAGE_START);
Wlabel = new JLabel("Welcome <user> ...");
welcome.add("West",Wlabel);
// The left panel for local file handler
files_local = new JPanel();
files_local.setBackground(Color.red);
getContentPane().add(files_local, BorderLayout.LINE_START);
//Llabel = new JLabel("Local Files ");
//files_local.add(Llabel);
//The right panel for server file Handler
files_server = new JPanel();
files_server.setBackground(Color.blue);
getContentPane().add(files_server, BorderLayout.LINE_END);
Slabel = new JLabel("Server Files ");
files_server.add(Slabel);
// -------------------------------------------------
JFileChooser chooser = new JFileChooser();
files_local.add("Center", chooser);
chooser.validate();
// *****************************************************
}
public void actionPerformed(ActionEvent e)
{
JLabel about1, about2, about3, title;
JPanel about;
about1 = new JLabel("Sting SFTP...");
about2 = new JLabel("Written By: Stephen Cheesley");
about3 = new JLabel("Developed: 26/06/2006");
about = new JPanel();
about.setLayout(new BorderLayout());
about.add("North", about1);
about.add("Center",about2);
about.add("South",about3);
JOptionPane.showMessageDialog(getContentPane(),about,"Sting SFTP",JOptionPane.PLAIN_MESSAGE);
}
}
That is the entirity of my code, however the main section that I tried for JFileChooser is......
Code:
JFileChooser chooser = new JFileChooser();
files_local.add("Center", chooser);
chooser.validate();
-
well, that files_local.add("Center", chooser); will not work properly.
as far as i know, the filechoose creates it's own frame to display the chooser.
so you just have to instantiate it and not add to a component.
when you want the filechooser to display, you invoke the following method:
int returnVal = chooser.showOpenDialog(aComponent);
where aComponent is the parent component to which the chooser shall be the child.
suppose you have a button called fileselect, which will pop up the chooser (in fact you can use any actionPerformed method of any component, like menu items etc.). for aComponent i suggest you to use the applet itself. for this purpose you will need a reference to the applet itself. since "this" will not function in an actionlistener, which itself is a local object, you can store a reference in the init method:
Code:
public class Sting extends JApplet implements ActionListener{
private Component me;
public void init(){
me = this;
...
}
....
then while building the gui you will create the button fileselect and make a actionlistener:
Code:
fileselect.addActionListener(
new ActionListener(){
public void actionPerformed(...){
int returnVal = chooser.showOpenDialog(me);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
if (file!=null) // do sthng with file
}
}
}
);
as you can see you don't add the chooser to any component, like you do with buttons. instead you give the chooser a parent component for display purposes when displaying.
-
Hey, i see what you are saying now, that totally makes sense. I am still having troubles though. I am trying to add it in to the files_local JPanel and I am still unsure as to how I am doing that from where I am now. This is my code, ammended after taking on board what you said:
Code:
/*
***********************************
Sting SFtp
Creator: Stephen Cheesley
Designer: Stephen Cheesley
***********************************
*/
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFileChooser;
import java.applet.*;
public class Sting extends JApplet implements ActionListener
{
private Component me;
//Methods to load on applet initialisation
public void init()
{
try
{
javax.swing.SwingUtilities.invokeAndWait(new Runnable()
{
public void run()
{
guiLayout();
}
});
}
catch (Exception e)
{
System.err.println("guiLayout didn't successfully complete");
}
me = this;
}
public void start()
{
}
public void stop()
{
}
// **********************************************************
// * *
// * This method dictates the GUI Layout of the Applet *
// * *
// **********************************************************
public void guiLayout()
{
//Where the GUI is created:
JMenuBar menuBar;
JMenu file, help;
JMenuItem menuItem;
final JPanel welcome, files_local, files_server;
JLabel Wlabel, Llabel, Slabel, about1;
JButton testButton;
//Create the menu bar.
menuBar = new JMenuBar();
// *****************************************************
//Create file menu.
file = new JMenu("File");
file.setMnemonic(KeyEvent.VK_A);
file.getAccessibleContext().setAccessibleDescription(
"The only menu in this program that has menu items");
menuBar.add(file);
//Add Items to the file menu
menuItem = new JMenuItem("Cut");
file.add(menuItem);
menuItem = new JMenuItem("Copy");
file.add(menuItem);
menuItem = new JMenuItem("Paste");
file.add(menuItem);
file.addSeparator();
menuItem = new JMenuItem("Logout");
file.add(menuItem);
//Create help menu
help = new JMenu("Help");
menuBar.add(help);
//Add Items to the help menu
menuItem = new JMenuItem("Readme");
help.add(menuItem);
// help.addSeparator(); --- Separator
menuItem = new JMenuItem("About Sting...");
help.add(menuItem);
menuItem.addActionListener(this);
//Add the menu bar to the GUI
setJMenuBar(menuBar);
// *****************************************************
// ------------------------------------------------
// This section sets the three main layout panels in
// positions top left and right.
// The top "Welcome" panel
welcome = new JPanel();
welcome.setBackground(Color.white);
getContentPane().add(welcome, BorderLayout.PAGE_START);
Wlabel = new JLabel("Welcome <user> ...");
welcome.add("West",Wlabel);
testButton = new JButton("Test");
welcome.add(testButton);
// The left panel for local file handler
files_local = new JPanel();
files_local.setBackground(Color.red);
getContentPane().add(files_local, BorderLayout.LINE_START);
Llabel = new JLabel("Local Files ");
files_local.add(Llabel);
//The right panel for server file Handler
files_server = new JPanel();
files_server.setBackground(Color.blue);
getContentPane().add(files_server, BorderLayout.LINE_END);
Slabel = new JLabel("Server Files ");
files_server.add(Slabel);
// -------------------------------------------------
final JFileChooser chooser = new JFileChooser();
testButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int returnVal = chooser.showOpenDialog(me);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = chooser.getSelectedFile();
if (file!=null)
{}// do something with file
}
}
});
// *****************************************************
}
public void actionPerformed(ActionEvent e)
{
JLabel about1, about2, about3, title;
JPanel about;
about1 = new JLabel("Sting SFTP...");
about2 = new JLabel("Written By: Stephen Cheesley");
about3 = new JLabel("Developed: 26/06/2006");
about = new JPanel();
about.setLayout(new BorderLayout());
about.add("North", about1);
about.add("Center",about2);
about.add("South",about3);
JOptionPane.showMessageDialog(getContentPane(),about,"Sting SFTP",JOptionPane.PLAIN_MESSAGE);
}
}
This is the relevant section for the ammended code:
Code:
final JFileChooser chooser = new JFileChooser();
testButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int returnVal = chooser.showOpenDialog(me);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = chooser.getSelectedFile();
if (file!=null)
{}// do something with file
}
}
});
What I am struggling to comprehend is how from loading the Open Dialog into the me component do I get it into the relevant JPanel??
Many thanks for ur help, its really appreciated
Last edited by sc_3007; 06-28-2006 at 06:34 PM.
-
when showing a filechooser, the only thing you can do is give it a parent component, which also may be a jpanel. but the chooser will pop up its own window, like JOptionPane.showMessageDialog(..) does. in fact message dialogs work nearly the same way, as you give them a parent component for display and relation purposes. so there is (to me) no known way to embed a filechooser into another component.
for now, your code seems to work properly.
Similar Threads
-
By Darren Bell in forum Talk to the Editors
Replies: 2
Last Post: 09-20-2002, 10:32 AM
-
By JB in forum Talk to the Editors
Replies: 3
Last Post: 09-16-2002, 08:15 PM
-
By Jeremy Dean in forum Java
Replies: 0
Last Post: 08-14-2001, 08:00 AM
-
By Shaun Botha in forum Java
Replies: 5
Last Post: 06-25-2001, 09:19 PM
-
By Fabio Luis De Paoli in forum Java
Replies: 0
Last Post: 08-17-2000, 01:06 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|