| | |
problem in listing directories in list box
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
•
•
Am I mistaken and List is not a class? What about this. However, you right List can be also interface but with less option then List class
Please disregard that bit about List
•
•
Join Date: Jun 2007
Posts: 7
Reputation:
Solved Threads: 0
Hi,
Thanks.Iv fixed the problem and its working.
Now got another problem. When i select a drive from the combo box the directories get listed in the listbox.But when i choose the next drive the directoreis in the that choosen drive is appened along with the listed directoreis in the list box. I tried the clear method but not working.What may be my mistale?
Thank you
Thanks.Iv fixed the problem and its working.
Now got another problem. When i select a drive from the combo box the directories get listed in the listbox.But when i choose the next drive the directoreis in the that choosen drive is appened along with the listed directoreis in the list box. I tried the clear method but not working.What may be my mistale?
Thank you
Please post your updated code and we will look at it
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Jun 2007
Posts: 7
Reputation:
Solved Threads: 0
Updated code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.File;
import java.io.FilenameFilter;
import java.io.FileFilter;
public class Prod_applet extends JFrame implements ItemListener{
boolean inAnApplet = true;
final boolean shouldFill = true;
final boolean shouldWeightX = true;
public List local_jlist = new List();
public Prod_applet() {
JPanel local_window = new JPanel();
JPanel server_window = new JPanel();
JPanel button_window = new JPanel();
JPanel action_window = new JPanel();
JButton connect = new JButton("Connect");
connect.setMargin(new Insets(0, 0, 0, 0));
JButton disconnect = new JButton("Disconnect");
disconnect.setMargin(new Insets(0, 0, 0, 0));
JButton up_but = new JButton(">>");
up_but.setMargin(new Insets(0, 0, 0, 0));
JButton down_but = new JButton("<<");
down_but.setMargin(new Insets(0, 0, 0, 0));
JLabel local_lbl = new JLabel("Local Site");
JLabel server_lbl = new JLabel("Remote Site");
JComboBox local_combobox = new JComboBox();
local_combobox.setEditable(true);
JComboBox server_combobox = new JComboBox();
//List local_jlist = new List();
//local_jlist.setMargin(new Insets(0, 0, 0, 0));
List server_jlist = new List();
//server_jlist.setMargin(new Insets(0, 0, 0, 0));
JButton local_mkdir = new JButton("MkDir");
local_mkdir.setMargin(new Insets(0, 0, 0, 0));
JButton server_mkdir = new JButton("MkDir");
server_mkdir.setMargin(new Insets(0, 0, 0, 0));
JButton local_delete = new JButton("Delete");
local_delete.setMargin(new Insets(0, 0, 0, 0));
JButton server_delete = new JButton("Delete");
server_delete.setMargin(new Insets(0, 0, 0, 0));
JButton local_refresh = new JButton("Refresh");
local_refresh.setMargin(new Insets(0, 0, 0, 0));
JButton server_refresh = new JButton("Refresh");
server_refresh.setMargin(new Insets(0, 0, 0, 0));
Container contentPane = getContentPane();
BorderLayout border = new BorderLayout();
contentPane.setLayout(border);
contentPane.add(local_window, BorderLayout.WEST);
contentPane.add(server_window, BorderLayout.EAST);
contentPane.add(button_window, BorderLayout.CENTER);
contentPane.add (action_window, BorderLayout.SOUTH);
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(2, 2, 2, 2);
GridBagConstraints c1 = new GridBagConstraints();
c1.insets = new Insets(2, 2, 2, 2);
GridBagConstraints c2 = new GridBagConstraints();
c2.insets = new Insets(2, 2, 2, 2);
GridBagConstraints c3 = new GridBagConstraints();
c3.insets = new Insets(2, 2, 2, 2);
GridBagConstraints c4 = new GridBagConstraints();
c4.insets = new Insets(2, 2, 2, 2);
GridBagConstraints c5 = new GridBagConstraints();
c5.insets = new Insets(2, 2, 2, 2);
GridBagConstraints c6 = new GridBagConstraints();
c6.insets = new Insets(2, 2, 2, 2);
local_window.setLayout(gridbag);
server_window.setLayout(gridbag);
button_window.setLayout(gridbag);
action_window.setLayout(gridbag);
File[] drives = File.listRoots();
for(int i = 0 ; i <drives.length;i++)
{
local_combobox.addItem(drives[i]);
server_combobox.addItem(drives[i]);
}
local_combobox. addItemListener(this);
server_combobox. addItemListener(this);
//Connect Button
c.gridx = 0;
c.gridy = 0;
gridbag.setConstraints(connect, c);
local_window.add(connect);
//Disconnect Button
c.gridx = 25;
c.gridy = 0;
gridbag.setConstraints(disconnect, c);
server_window.add(disconnect);
//Local Label
c1.gridx = 0;
c1.gridy = 3;
gridbag.setConstraints(local_lbl, c1);
local_window.add(local_lbl);
//Local combobox
c1.gridx = 10;
c1.gridy = 3;
gridbag.setConstraints(local_combobox, c1);
local_window.add(local_combobox);
//Server Label
c1.gridx = 25;
c1.gridy = 3;
gridbag.setConstraints(server_lbl, c1);
server_window.add(server_lbl);
//Server combobox
c1.gridx = 35;
c1.gridy = 3;
gridbag.setConstraints(server_combobox, c1);
server_window.add(server_combobox);
//Local jlist
c2.gridx = 0;
c2.gridy = 6;
gridbag.setConstraints(local_jlist, c2);
local_window.add(local_jlist);
//Server jlist
c2.gridx = 25;
c2.gridy = 6;
gridbag.setConstraints(server_jlist, c2);
server_window.add(server_jlist);
//Up Button
c3.gridx = 10;
c3.gridy = 8;
gridbag.setConstraints(up_but, c3);
button_window.add(up_but);
//Down Button
c3.gridx = 10;
c3.gridy = 9;
gridbag.setConstraints(down_but, c3);
button_window.add(down_but);
//local_mkdir
c4.gridx = 0;
c4.gridy = 10;
gridbag.setConstraints(local_mkdir,c4);
action_window.add(local_mkdir);
//server_mkdir
c4.gridx = 25;
c4.gridy = 10;
gridbag.setConstraints (server_mkdir, c4);
action_window.add(server_mkdir);
//local_delete
c5.gridx = 1;
c5.gridy = 10;
gridbag.setConstraints(local_delete, c5);
action_window.add(local_delete);
//server_delete
c5.gridx = 26;
c5.gridy = 10;
gridbag.setConstraints(server_delete, c5);
action_window.add(server_delete);
//local_refresh
c6.gridx = 2;
c6.gridy = 10;
gridbag.setConstraints(local_refresh, c6);
action_window.add(local_refresh);
//server_refresh
c6.gridx = 27;
c6.gridy = 10;
gridbag.setConstraints(server_refresh, c6);
action_window.add(server_refresh);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (inAnApplet) {
dispose();
} else {
System.exit(0);
}
}
});
}
public void itemStateChanged(ItemEvent e)
{
JComboBox local_combobox = (JComboBox)e.getSource();
JComboBox server_combobox = (JComboBox)e.getSource();
//List local_jlist=null;
String newSelectionStrName_local = null;
String newSelectionStrName_server = null;
if ( local_combobox.getSelectedItem() instanceof File)
{
File newSelection_local = (File)local_combobox.getSelectedItem();
newSelectionStrName_local = newSelection_local.getPath();
}
else if (local_combobox.getSelectedItem() instanceof String)
{
newSelectionStrName_local = (String) local_combobox.getSelectedItem();
}
if ( server_combobox.getSelectedItem() instanceof File)
{
File newSelection_server = (File)server_combobox.getSelectedItem();
newSelectionStrName_server = newSelection_server.getPath();
}
else if (server_combobox.getSelectedItem() instanceof String)
{
newSelectionStrName_server = (String) server_combobox.getSelectedItem();
}
File file1 = new File(newSelectionStrName_local);
File file2 = new File(newSelectionStrName_server);
String[] local_files = file1.list();
//local_jlist.clear();
for (int i=0;i<local_files.length;i++)
{
local_jlist.add(local_files[i]);
}
//local_jlist=new List();
}
public static void main(String args[]) {
Prod_applet window = new Prod_applet();
window.setSize(800,600);
window.inAnApplet = false;
window.setTitle("FTP_CLIENT");
window.pack();
window.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.File;
import java.io.FilenameFilter;
import java.io.FileFilter;
public class Prod_applet extends JFrame implements ItemListener{
boolean inAnApplet = true;
final boolean shouldFill = true;
final boolean shouldWeightX = true;
public List local_jlist = new List();
public Prod_applet() {
JPanel local_window = new JPanel();
JPanel server_window = new JPanel();
JPanel button_window = new JPanel();
JPanel action_window = new JPanel();
JButton connect = new JButton("Connect");
connect.setMargin(new Insets(0, 0, 0, 0));
JButton disconnect = new JButton("Disconnect");
disconnect.setMargin(new Insets(0, 0, 0, 0));
JButton up_but = new JButton(">>");
up_but.setMargin(new Insets(0, 0, 0, 0));
JButton down_but = new JButton("<<");
down_but.setMargin(new Insets(0, 0, 0, 0));
JLabel local_lbl = new JLabel("Local Site");
JLabel server_lbl = new JLabel("Remote Site");
JComboBox local_combobox = new JComboBox();
local_combobox.setEditable(true);
JComboBox server_combobox = new JComboBox();
//List local_jlist = new List();
//local_jlist.setMargin(new Insets(0, 0, 0, 0));
List server_jlist = new List();
//server_jlist.setMargin(new Insets(0, 0, 0, 0));
JButton local_mkdir = new JButton("MkDir");
local_mkdir.setMargin(new Insets(0, 0, 0, 0));
JButton server_mkdir = new JButton("MkDir");
server_mkdir.setMargin(new Insets(0, 0, 0, 0));
JButton local_delete = new JButton("Delete");
local_delete.setMargin(new Insets(0, 0, 0, 0));
JButton server_delete = new JButton("Delete");
server_delete.setMargin(new Insets(0, 0, 0, 0));
JButton local_refresh = new JButton("Refresh");
local_refresh.setMargin(new Insets(0, 0, 0, 0));
JButton server_refresh = new JButton("Refresh");
server_refresh.setMargin(new Insets(0, 0, 0, 0));
Container contentPane = getContentPane();
BorderLayout border = new BorderLayout();
contentPane.setLayout(border);
contentPane.add(local_window, BorderLayout.WEST);
contentPane.add(server_window, BorderLayout.EAST);
contentPane.add(button_window, BorderLayout.CENTER);
contentPane.add (action_window, BorderLayout.SOUTH);
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(2, 2, 2, 2);
GridBagConstraints c1 = new GridBagConstraints();
c1.insets = new Insets(2, 2, 2, 2);
GridBagConstraints c2 = new GridBagConstraints();
c2.insets = new Insets(2, 2, 2, 2);
GridBagConstraints c3 = new GridBagConstraints();
c3.insets = new Insets(2, 2, 2, 2);
GridBagConstraints c4 = new GridBagConstraints();
c4.insets = new Insets(2, 2, 2, 2);
GridBagConstraints c5 = new GridBagConstraints();
c5.insets = new Insets(2, 2, 2, 2);
GridBagConstraints c6 = new GridBagConstraints();
c6.insets = new Insets(2, 2, 2, 2);
local_window.setLayout(gridbag);
server_window.setLayout(gridbag);
button_window.setLayout(gridbag);
action_window.setLayout(gridbag);
File[] drives = File.listRoots();
for(int i = 0 ; i <drives.length;i++)
{
local_combobox.addItem(drives[i]);
server_combobox.addItem(drives[i]);
}
local_combobox. addItemListener(this);
server_combobox. addItemListener(this);
//Connect Button
c.gridx = 0;
c.gridy = 0;
gridbag.setConstraints(connect, c);
local_window.add(connect);
//Disconnect Button
c.gridx = 25;
c.gridy = 0;
gridbag.setConstraints(disconnect, c);
server_window.add(disconnect);
//Local Label
c1.gridx = 0;
c1.gridy = 3;
gridbag.setConstraints(local_lbl, c1);
local_window.add(local_lbl);
//Local combobox
c1.gridx = 10;
c1.gridy = 3;
gridbag.setConstraints(local_combobox, c1);
local_window.add(local_combobox);
//Server Label
c1.gridx = 25;
c1.gridy = 3;
gridbag.setConstraints(server_lbl, c1);
server_window.add(server_lbl);
//Server combobox
c1.gridx = 35;
c1.gridy = 3;
gridbag.setConstraints(server_combobox, c1);
server_window.add(server_combobox);
//Local jlist
c2.gridx = 0;
c2.gridy = 6;
gridbag.setConstraints(local_jlist, c2);
local_window.add(local_jlist);
//Server jlist
c2.gridx = 25;
c2.gridy = 6;
gridbag.setConstraints(server_jlist, c2);
server_window.add(server_jlist);
//Up Button
c3.gridx = 10;
c3.gridy = 8;
gridbag.setConstraints(up_but, c3);
button_window.add(up_but);
//Down Button
c3.gridx = 10;
c3.gridy = 9;
gridbag.setConstraints(down_but, c3);
button_window.add(down_but);
//local_mkdir
c4.gridx = 0;
c4.gridy = 10;
gridbag.setConstraints(local_mkdir,c4);
action_window.add(local_mkdir);
//server_mkdir
c4.gridx = 25;
c4.gridy = 10;
gridbag.setConstraints (server_mkdir, c4);
action_window.add(server_mkdir);
//local_delete
c5.gridx = 1;
c5.gridy = 10;
gridbag.setConstraints(local_delete, c5);
action_window.add(local_delete);
//server_delete
c5.gridx = 26;
c5.gridy = 10;
gridbag.setConstraints(server_delete, c5);
action_window.add(server_delete);
//local_refresh
c6.gridx = 2;
c6.gridy = 10;
gridbag.setConstraints(local_refresh, c6);
action_window.add(local_refresh);
//server_refresh
c6.gridx = 27;
c6.gridy = 10;
gridbag.setConstraints(server_refresh, c6);
action_window.add(server_refresh);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (inAnApplet) {
dispose();
} else {
System.exit(0);
}
}
});
}
public void itemStateChanged(ItemEvent e)
{
JComboBox local_combobox = (JComboBox)e.getSource();
JComboBox server_combobox = (JComboBox)e.getSource();
//List local_jlist=null;
String newSelectionStrName_local = null;
String newSelectionStrName_server = null;
if ( local_combobox.getSelectedItem() instanceof File)
{
File newSelection_local = (File)local_combobox.getSelectedItem();
newSelectionStrName_local = newSelection_local.getPath();
}
else if (local_combobox.getSelectedItem() instanceof String)
{
newSelectionStrName_local = (String) local_combobox.getSelectedItem();
}
if ( server_combobox.getSelectedItem() instanceof File)
{
File newSelection_server = (File)server_combobox.getSelectedItem();
newSelectionStrName_server = newSelection_server.getPath();
}
else if (server_combobox.getSelectedItem() instanceof String)
{
newSelectionStrName_server = (String) server_combobox.getSelectedItem();
}
File file1 = new File(newSelectionStrName_local);
File file2 = new File(newSelectionStrName_server);
String[] local_files = file1.list();
//local_jlist.clear();
for (int i=0;i<local_files.length;i++)
{
local_jlist.add(local_files[i]);
}
//local_jlist=new List();
}
public static void main(String args[]) {
Prod_applet window = new Prod_applet();
window.setSize(800,600);
window.inAnApplet = false;
window.setTitle("FTP_CLIENT");
window.pack();
window.setVisible(true);
}
}
replace line 214 (according my IDE) which you commented out This will sort your problem with apend.
//local_jlist.clear(); with local_jlist.removeAll(); . If you check API properly you will find that List.clear() •
•
•
•
Deprecated. As of JDK version 1.1, replaced by removeAll().
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Jun 2007
Posts: 5
Reputation:
Solved Threads: 2
hi tolearn
i have indicated where you made mistakes using "++++++++++++++++++" look out these sections and you will get some coooments
Also not that list is an abstract window toolkit componet (awt) in the package java.awt.
it does have the method addElement(String)
instead you use
List mylist = new List();
mylist.add( String)
the reason you were getting the error variable local_jlist might not have been initialized is because you declared it in the method Prod_applet as follows
List local_jlist;
instead of List local_jlist = new List();
This happens when you are using java 1.4 and bellow. With java 1.5 you do not get that message
The following is the new code and let me know if you get any more problems
mport java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.File;
import java.io.FilenameFilter;
import java.io.FileFilter;
public class Prod_applet extends JFrame implements ItemListener{
boolean inAnApplet = true;
final boolean shouldFill = true;
final boolean shouldWeightX = true;
//++++++++++++++++++++++++++++++++++++++++++++
//make the local_jlist public to all methods in the class
List local_jlist;
public Prod_applet() {
JPanel local_window = new JPanel();
JPanel server_window = new JPanel();
JPanel button_window = new JPanel();
JPanel action_window = new JPanel();
JButton connect = new JButton("Connect");
connect.setMargin(new Insets(0, 0, 0, 0));
JButton disconnect = new JButton("Disconnect");
disconnect.setMargin(new Insets(0, 0, 0, 0));
JButton up_but = new JButton(">>");
up_but.setMargin(new Insets(0, 0, 0, 0));
JButton down_but = new JButton("<<");
down_but.setMargin(new Insets(0, 0, 0, 0));
JLabel local_lbl = new JLabel("Local Site");
JLabel server_lbl = new JLabel("Remote Site");
JComboBox local_combobox = new JComboBox();
local_combobox.setEditable(true);
JComboBox server_combobox = new JComboBox();
//++++++++++++++++++++++++++++++++++
//this is only visible in this method i.e the constructo
//make it visible to the whole class
//List local_jlist = new List();
//the correct code is
local_jlist = new List();
//local_jlist.setMargin(new Insets(0, 0, 0, 0));
List server_jlist = new List();
//server_jlist.setMargin(new Insets(0, 0, 0, 0));
JButton local_mkdir = new JButton("MkDir");
local_mkdir.setMargin(new Insets(0, 0, 0, 0));
JButton server_mkdir = new JButton("MkDir");
server_mkdir.setMargin(new Insets(0, 0, 0, 0));
JButton local_delete = new JButton("Delete");
local_delete.setMargin(new Insets(0, 0, 0, 0));
JButton server_delete = new JButton("Delete");
server_delete.setMargin(new Insets(0, 0, 0, 0));
JButton local_refresh = new JButton("Refresh");
local_refresh.setMargin(new Insets(0, 0, 0, 0));
JButton server_refresh = new JButton("Refresh");
server_refresh.setMargin(new Insets(0, 0, 0, 0));
Container contentPane = getContentPane();
BorderLayout border = new BorderLayout();
contentPane.setLayout(border);
contentPane.add(local_window, BorderLayout.WEST);
contentPane.add(server_window, BorderLayout.EAST);
contentPane.add(button_window, BorderLayout.CENTER);
contentPane.add (action_window, BorderLayout.SOUTH);
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(2, 2, 2, 2);
GridBagConstraints c1 = new GridBagConstraints();
c1.insets = new Insets(2, 2, 2, 2);
GridBagConstraints c2 = new GridBagConstraints();
c2.insets = new Insets(2, 2, 2, 2);
GridBagConstraints c3 = new GridBagConstraints();
c3.insets = new Insets(2, 2, 2, 2);
GridBagConstraints c4 = new GridBagConstraints();
c4.insets = new Insets(2, 2, 2, 2);
GridBagConstraints c5 = new GridBagConstraints();
c5.insets = new Insets(2, 2, 2, 2);
GridBagConstraints c6 = new GridBagConstraints();
c6.insets = new Insets(2, 2, 2, 2);
local_window.setLayout(gridbag);
server_window.setLayout(gridbag);
button_window.setLayout(gridbag);
action_window.setLayout(gridbag);
File[] drives = File.listRoots();
for(int i = 0 ; i <drives.length;i++)
{
local_combobox.addItem(drives[i]);
server_combobox.addItem(drives[i]);
}
local_combobox. addItemListener(this);
server_combobox. addItemListener(this);
//Connect Button
c.gridx = 0;
c.gridy = 0;
gridbag.setConstraints(connect, c);
local_window.add(connect);
//Disconnect Button
c.gridx = 25;
c.gridy = 0;
gridbag.setConstraints(disconnect, c);
server_window.add(disconnect);
//Local Label
c1.gridx = 0;
c1.gridy = 3;
gridbag.setConstraints(local_lbl, c1);
local_window.add(local_lbl);
//Local combobox
c1.gridx = 10;
c1.gridy = 3;
gridbag.setConstraints(local_combobox, c1);
local_window.add(local_combobox);
//Server Label
c1.gridx = 25;
c1.gridy = 3;
gridbag.setConstraints(server_lbl, c1);
server_window.add(server_lbl);
//Server combobox
c1.gridx = 35;
c1.gridy = 3;
gridbag.setConstraints(server_combobox, c1);
server_window.add(server_combobox);
//Local jlist
c2.gridx = 0;
c2.gridy = 6;
gridbag.setConstraints(local_jlist, c2);
local_window.add(local_jlist);
//Server jlist
c2.gridx = 25;
c2.gridy = 6;
gridbag.setConstraints(server_jlist, c2);
server_window.add(server_jlist);
//Up Button
c3.gridx = 10;
c3.gridy = 8;
gridbag.setConstraints(up_but, c3);
button_window.add(up_but);
//Down Button
c3.gridx = 10;
c3.gridy = 9;
gridbag.setConstraints(down_but, c3);
button_window.add(down_but);
//local_mkdir
c4.gridx = 0;
c4.gridy = 10;
gridbag.setConstraints(local_mkdir,c4);
action_window.add(local_mkdir);
//server_mkdir
c4.gridx = 25;
c4.gridy = 10;
gridbag.setConstraints (server_mkdir, c4);
action_window.add(server_mkdir);
//local_delete
c5.gridx = 1;
c5.gridy = 10;
gridbag.setConstraints(local_delete, c5);
action_window.add(local_delete);
//server_delete
c5.gridx = 26;
c5.gridy = 10;
gridbag.setConstraints(server_delete, c5);
action_window.add(server_delete);
//local_refresh
c6.gridx = 2;
c6.gridy = 10;
gridbag.setConstraints(local_refresh, c6);
action_window.add(local_refresh);
//server_refresh
c6.gridx = 27;
c6.gridy = 10;
gridbag.setConstraints(server_refresh, c6);
action_window.add(server_refresh);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (inAnApplet) {
dispose();
} else {
System.exit(0);
}
}
});
}
public void itemStateChanged(ItemEvent e)
{
JComboBox local_combobox = (JComboBox)e.getSource();
JComboBox server_combobox = (JComboBox)e.getSource();
String newSelectionStrName_local = null;
String newSelectionStrName_server = null;
if ( local_combobox.getSelectedItem() instanceof File)
{
File newSelection_local = (File)local_combobox.getSelectedItem();
newSelectionStrName_local = newSelection_local.getPath();
}
else if (local_combobox.getSelectedItem() instanceof String)
{
newSelectionStrName_local = (String) local_combobox.getSelectedItem();
}
if ( server_combobox.getSelectedItem() instanceof File)
{
File newSelection_server = (File)server_combobox.getSelectedItem();
newSelectionStrName_server = newSelection_server.getPath();
}
else if (server_combobox.getSelectedItem() instanceof String)
{
newSelectionStrName_server = (String) server_combobox.getSelectedItem();
}
File file1 = new File(newSelectionStrName_local);
File file2 = new File(newSelectionStrName_server);
String[] local_files = file1.list();
for (int i=0;i<local_files.length;i++)
{
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
//this is not correct
// local_jlist.addElement(local_files[i]);
//the correct code is
local_jlist.add(local_fiels[i]);
}
}
public static void main(String args[]) {
Prod_applet window = new Prod_applet();
window.setSize(800,600);
window.inAnApplet = false;
window.setTitle("FTP_CLIENT");
window.pack();
window.setVisible(true);
}
}
i have indicated where you made mistakes using "++++++++++++++++++" look out these sections and you will get some coooments
Also not that list is an abstract window toolkit componet (awt) in the package java.awt.
it does have the method addElement(String)
instead you use
List mylist = new List();
mylist.add( String)
the reason you were getting the error variable local_jlist might not have been initialized is because you declared it in the method Prod_applet as follows
List local_jlist;
instead of List local_jlist = new List();
This happens when you are using java 1.4 and bellow. With java 1.5 you do not get that message
The following is the new code and let me know if you get any more problems
mport java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.File;
import java.io.FilenameFilter;
import java.io.FileFilter;
public class Prod_applet extends JFrame implements ItemListener{
boolean inAnApplet = true;
final boolean shouldFill = true;
final boolean shouldWeightX = true;
//++++++++++++++++++++++++++++++++++++++++++++
//make the local_jlist public to all methods in the class
List local_jlist;
public Prod_applet() {
JPanel local_window = new JPanel();
JPanel server_window = new JPanel();
JPanel button_window = new JPanel();
JPanel action_window = new JPanel();
JButton connect = new JButton("Connect");
connect.setMargin(new Insets(0, 0, 0, 0));
JButton disconnect = new JButton("Disconnect");
disconnect.setMargin(new Insets(0, 0, 0, 0));
JButton up_but = new JButton(">>");
up_but.setMargin(new Insets(0, 0, 0, 0));
JButton down_but = new JButton("<<");
down_but.setMargin(new Insets(0, 0, 0, 0));
JLabel local_lbl = new JLabel("Local Site");
JLabel server_lbl = new JLabel("Remote Site");
JComboBox local_combobox = new JComboBox();
local_combobox.setEditable(true);
JComboBox server_combobox = new JComboBox();
//++++++++++++++++++++++++++++++++++
//this is only visible in this method i.e the constructo
//make it visible to the whole class
//List local_jlist = new List();
//the correct code is
local_jlist = new List();
//local_jlist.setMargin(new Insets(0, 0, 0, 0));
List server_jlist = new List();
//server_jlist.setMargin(new Insets(0, 0, 0, 0));
JButton local_mkdir = new JButton("MkDir");
local_mkdir.setMargin(new Insets(0, 0, 0, 0));
JButton server_mkdir = new JButton("MkDir");
server_mkdir.setMargin(new Insets(0, 0, 0, 0));
JButton local_delete = new JButton("Delete");
local_delete.setMargin(new Insets(0, 0, 0, 0));
JButton server_delete = new JButton("Delete");
server_delete.setMargin(new Insets(0, 0, 0, 0));
JButton local_refresh = new JButton("Refresh");
local_refresh.setMargin(new Insets(0, 0, 0, 0));
JButton server_refresh = new JButton("Refresh");
server_refresh.setMargin(new Insets(0, 0, 0, 0));
Container contentPane = getContentPane();
BorderLayout border = new BorderLayout();
contentPane.setLayout(border);
contentPane.add(local_window, BorderLayout.WEST);
contentPane.add(server_window, BorderLayout.EAST);
contentPane.add(button_window, BorderLayout.CENTER);
contentPane.add (action_window, BorderLayout.SOUTH);
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(2, 2, 2, 2);
GridBagConstraints c1 = new GridBagConstraints();
c1.insets = new Insets(2, 2, 2, 2);
GridBagConstraints c2 = new GridBagConstraints();
c2.insets = new Insets(2, 2, 2, 2);
GridBagConstraints c3 = new GridBagConstraints();
c3.insets = new Insets(2, 2, 2, 2);
GridBagConstraints c4 = new GridBagConstraints();
c4.insets = new Insets(2, 2, 2, 2);
GridBagConstraints c5 = new GridBagConstraints();
c5.insets = new Insets(2, 2, 2, 2);
GridBagConstraints c6 = new GridBagConstraints();
c6.insets = new Insets(2, 2, 2, 2);
local_window.setLayout(gridbag);
server_window.setLayout(gridbag);
button_window.setLayout(gridbag);
action_window.setLayout(gridbag);
File[] drives = File.listRoots();
for(int i = 0 ; i <drives.length;i++)
{
local_combobox.addItem(drives[i]);
server_combobox.addItem(drives[i]);
}
local_combobox. addItemListener(this);
server_combobox. addItemListener(this);
//Connect Button
c.gridx = 0;
c.gridy = 0;
gridbag.setConstraints(connect, c);
local_window.add(connect);
//Disconnect Button
c.gridx = 25;
c.gridy = 0;
gridbag.setConstraints(disconnect, c);
server_window.add(disconnect);
//Local Label
c1.gridx = 0;
c1.gridy = 3;
gridbag.setConstraints(local_lbl, c1);
local_window.add(local_lbl);
//Local combobox
c1.gridx = 10;
c1.gridy = 3;
gridbag.setConstraints(local_combobox, c1);
local_window.add(local_combobox);
//Server Label
c1.gridx = 25;
c1.gridy = 3;
gridbag.setConstraints(server_lbl, c1);
server_window.add(server_lbl);
//Server combobox
c1.gridx = 35;
c1.gridy = 3;
gridbag.setConstraints(server_combobox, c1);
server_window.add(server_combobox);
//Local jlist
c2.gridx = 0;
c2.gridy = 6;
gridbag.setConstraints(local_jlist, c2);
local_window.add(local_jlist);
//Server jlist
c2.gridx = 25;
c2.gridy = 6;
gridbag.setConstraints(server_jlist, c2);
server_window.add(server_jlist);
//Up Button
c3.gridx = 10;
c3.gridy = 8;
gridbag.setConstraints(up_but, c3);
button_window.add(up_but);
//Down Button
c3.gridx = 10;
c3.gridy = 9;
gridbag.setConstraints(down_but, c3);
button_window.add(down_but);
//local_mkdir
c4.gridx = 0;
c4.gridy = 10;
gridbag.setConstraints(local_mkdir,c4);
action_window.add(local_mkdir);
//server_mkdir
c4.gridx = 25;
c4.gridy = 10;
gridbag.setConstraints (server_mkdir, c4);
action_window.add(server_mkdir);
//local_delete
c5.gridx = 1;
c5.gridy = 10;
gridbag.setConstraints(local_delete, c5);
action_window.add(local_delete);
//server_delete
c5.gridx = 26;
c5.gridy = 10;
gridbag.setConstraints(server_delete, c5);
action_window.add(server_delete);
//local_refresh
c6.gridx = 2;
c6.gridy = 10;
gridbag.setConstraints(local_refresh, c6);
action_window.add(local_refresh);
//server_refresh
c6.gridx = 27;
c6.gridy = 10;
gridbag.setConstraints(server_refresh, c6);
action_window.add(server_refresh);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (inAnApplet) {
dispose();
} else {
System.exit(0);
}
}
});
}
public void itemStateChanged(ItemEvent e)
{
JComboBox local_combobox = (JComboBox)e.getSource();
JComboBox server_combobox = (JComboBox)e.getSource();
String newSelectionStrName_local = null;
String newSelectionStrName_server = null;
if ( local_combobox.getSelectedItem() instanceof File)
{
File newSelection_local = (File)local_combobox.getSelectedItem();
newSelectionStrName_local = newSelection_local.getPath();
}
else if (local_combobox.getSelectedItem() instanceof String)
{
newSelectionStrName_local = (String) local_combobox.getSelectedItem();
}
if ( server_combobox.getSelectedItem() instanceof File)
{
File newSelection_server = (File)server_combobox.getSelectedItem();
newSelectionStrName_server = newSelection_server.getPath();
}
else if (server_combobox.getSelectedItem() instanceof String)
{
newSelectionStrName_server = (String) server_combobox.getSelectedItem();
}
File file1 = new File(newSelectionStrName_local);
File file2 = new File(newSelectionStrName_server);
String[] local_files = file1.list();
for (int i=0;i<local_files.length;i++)
{
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
//this is not correct
// local_jlist.addElement(local_files[i]);
//the correct code is
local_jlist.add(local_fiels[i]);
}
}
public static void main(String args[]) {
Prod_applet window = new Prod_applet();
window.setSize(800,600);
window.inAnApplet = false;
window.setTitle("FTP_CLIENT");
window.pack();
window.setVisible(true);
}
}
![]() |
Other Threads in the Java Forum
- Previous Thread: Dots and Boxes Client/Server in java
- Next Thread: Screenshot of JFrame which is Invisible
Views: 2733 | Replies: 16
| Thread Tools | Search this Thread |
Tag cloud for Java
911 addressbook android api append apple applet application arguments array arrays automation binary bluetooth character chat class classes client code component csv database draw eclipse error event exception file fractal ftp game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me japplet java javaarraylist javaprojects jmf jni jpanel julia linked linux list loop map method methods mobile netbeans newbie number object objects oracle oriented panel print printf problem program programming project projects recursion replaydirector reporting researchinmotion return robot rotatetext scanner screen se server set size sms socket sort sql stream string swing test threads time transfer tree ubuntu windows






