2014-03-13

Hi this is Abhishek

Am trying to do a small project on file explorer, i have add one list directory to the panel. Now i want the same list directory to the right side of the first one.

Am not able to add it. My idea was to add one list directory to display local files and another list directory will list files from hadoop cluster.

Some one please help me add the second list directory.

This is the code

import javax.swing.*;

import java.awt.*;

import java.text.DateFormat;

import javax.swing.border.*;

import java.awt.event.*;

import javax.swing.event.*;

import java.io.*;

import javax.swing.filechooser.FileSystemView;

import java.io.FileFilter;

import javax.swing.JToolTip;

import javax.swing.ToolTipManager;

import javax.swing.border.Border;

import javax.swing.border.LineBorder;

public class Testing extends JFrame implements ActionListener, ItemListener

{

private JSplitPane splitPaneH;

private JPanel topPanel,p1,p2,p3;

private JButton up;

private JTextField details;

private File currentDir;

private String[] files;

private List list;

private FilenameFilter filter;

private DateFormat dateFormatter =DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);

private JPopupMenu popup;

private JMenuItem item;

public Testing()

{

setBounds(100, 100, 800, 600);

//JPanel p1;

p1= new JPanel();

p1.setBorder(new EmptyBorder(5, 5, 5, 5));

p1.setLayout(new BorderLayout(1, 1));

setContentPane(p1);

topPanel = new JPanel();

topPanel.setLayout( new BorderLayout() );

getContentPane().add( topPanel );

JLabel lbl = new JLabel("Exp");

p1.add(lbl, BorderLayout.NORTH);

splitPaneH = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT );

topPanel.add( splitPaneH, BorderLayout.CENTER );

}

public void Panel2()

{

JButton b1,b2,b3,b4;

//JTextField t1,t2;

//JLabel l1,l2;

p2=new JPanel();

p2.setLayout(null);

b1=new JButton("Upload");

b2=new JButton("Download");

b3=new JButton("View files");

//t1=new JTextField(10);

//t2=new JTextField(10);

b4=new JButton("Upload");

//t1.setBounds(10,10,110,30);

b1.setBounds(10, 45, 110, 30);

//t2.setBounds(10, 80, 110, 30);

b2.setBounds(10, 110, 110, 30);

b3.setBounds(10, 135, 110, 30);

b4.setBounds(100, 135, 160, 30);

//p2.add(t1);

//p2.add(t2);

p2.add(b1);

p2.add(b2);

p2.add(b3);

//p2.setBounds(0,0,150,800);

//p2.setBorder(BorderFactory.createLineBorder(Color. BLACK, 1));

add(p2);

setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public void Panel3(String directory,FilenameFilter filter)

{

//JPanel p3;

p3=new JPanel(new BorderLayout());

list =new List();

list.addActionListener(this);

list.addItemListener(this);

JToolBar jtb=new JToolBar();

up=new JButton();

up.setBounds( 0, 0,25,25);

up.addActionListener(this);

jtb.add(up);

p3.add(jtb,BorderLayout.NORTH);

details=new JTextField();

list.setFont(new Font("MonoSpaced",Font.PLAIN,12));

details.setEditable(false);

p3.add(list,"Center");

p3.add(details,"South");

p3.setBounds(0,0,0,300);

listDirectory(directory);

}

public void listDirectory(String directory) {

// Convert the string to a File object, and check that the dir exists

File dir = new File(directory);

if (!dir.isDirectory( ))

throw new IllegalArgumentException("FileLister: no such directory");

// Get the (filtered) directory entries

files = dir.list(filter);

// Sort the list of filenames.

java.util.Arrays.sort(files);

// Remove any old entries in the list, and add the new ones

list.removeAll( );

list.add("[Up to Parent Directory]"); // A special case entry

for(int i = 0; i < files.length; i++)

{

list.add(files[i]);

}

// Display directory name in window titlebar and in the details box

this.setTitle(directory);

details.setText(directory);

// Remember this directory for later.

currentDir = dir;

}

public void split()

{

splitPaneH.setDividerSize(15);

splitPaneH.setDividerLocation(200);

splitPaneH.setLeftComponent(p2);

splitPaneH.setRightComponent(p3);

}

public void itemStateChanged(ItemEvent e)

{

int i = list.getSelectedIndex( ) - 1; // minus 1 for Up To Parent entry

if (i < 0) return;

String filename = files[i]; // Get the selected entry

File f = new File(currentDir, filename); // Convert to a File

if (!f.exists( )) // Confirm that it exists

throw new IllegalArgumentException("FileLister: " +

"no such file or directory");

// Get the details about the file or directory, concatenate to a string

String info = filename;

if (f.isDirectory( )) info += File.separator;

info += " " + f.length( ) + " bytes ";

info += dateFormatter.format(new java.util.Date(f.lastModified( )));

if (f.canRead( )) info += " Read";

if (f.canWrite( )) info += " Write";

// And display the details string

details.setText(info);

}

/**

* This ActionListener method is invoked when the user double-clicks on an

* entry or clicks on one of the buttons. If they double-click on a file,

* create a FileViewer to display that file. If they double-click on a

* directory, call the listDirectory( ) method to display that directory

**/

public void actionPerformed(ActionEvent e) {

if (e.getSource() == up) {

up();

} else if (e.getSource() == list) {

int i = list.getSelectedIndex();

String name = files[i];

File f = new File(currentDir, name);

String fullname = f.getAbsolutePath();

if (f.isDirectory())

listDirectory(fullname);

else

new FileViewer(fullname).show();

// }

}

}

/** A convenience method to display the contents of the parent directory */

protected void up( )

{

String parent = currentDir.getParent( );

if (parent == null) return;

listDirectory(parent);

}

/** A convenience method used by main( ) */

public static void usage( ) {

System.out.println("Usage: java Testing [directory_name] " +

"[-e file_extension]");

System.exit(0);

}

public static void main(String args[ ])

{

Testing mainFrame = new Testing();

mainFrame.setVisible( true );

mainFrame.Panel2();

FilenameFilter filter = null; // The filter, if any

String directory = null; // The specified dir, or the current dir

// Loop through args array, parsing arguments

for(int i = 0; i < args.length; i++) {

if (args[i].equals("-e")) {

if (++i >= args.length) usage( );

final String suffix = args[i]; // final for anon. class below

// This class is a simple FilenameFilter. It defines the

// accept( ) method required to determine whether a specified

// file should be listed. A file will be listed if its name

// ends with the specified extension, or if it is a directory.

filter = new FilenameFilter( ) {

public boolean accept(File dir, String name) {

if (name.endsWith(suffix)) return true;

else return (new File(dir, name)).isDirectory( );

}

};

}

else {

if (directory != null) usage( ); // If already specified, fail.

else directory = args[i];

}

}

// if no directory specified, use the current directory

if (directory == null) directory = System.getProperty("user.dir");

mainFrame.Panel3(directory,filter);

mainFrame.split();

}

}

class FileViewer extends JFrame implements ActionListener {

String directory; // The default directory to display in the FileDialog

JTextArea textarea; // The area to display the file contents into

/** Convenience constructor: file viewer starts out blank */

public FileViewer() { this(null, null); }

/** Convenience constructor: display file from current directory */

public FileViewer(String filename) { this(null, filename); }

/**

* The real constructor. Create a FileViewer object to display the

* specified file from the specified directory

**/

public FileViewer(String directory, String filename) {

super(); // Create the frame

// Create the frame

// Destroy the window when the user requests it

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) { dispose(); }

});

// Create a TextArea to display the contents of the file in

textarea = new JTextArea("", 24, 80);

textarea.setFont(new Font("MonoSpaced", Font.PLAIN, 12));

textarea.setEditable(false);

this.add("Center", textarea);

// Create a bottom panel to hold a couple of buttons in

Panel p = new Panel();

p.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 5));

this.add(p, "South");

// Create the buttons and arrange to handle button clicks

Font font = new Font("SansSerif", Font.BOLD, 14);

Button close = new Button("Close");

close.addActionListener(this);

close.setFont(font);

p.add(close);

this.pack();

// Figure out the directory, from filename or current dir, if necessary

if (directory == null) {

File f;

if ((filename != null)&& (f = new File(filename)).isAbsolute()) {

directory = f.getParent();

filename = f.getName();

}

else directory = System.getProperty("user.dir");

}

this.directory = directory; // Remember the directory, for FileDialog

setFile(directory, filename); // Now load and display the file

}

/**

* Load and display the specified file from the specified directory

**/

public void setFile(String directory, String filename) {

if ((filename == null) || (filename.length() == 0)) return;

File f;

FileReader in = null;

// Read and display the file contents. Since we're reading text, we

// use a FileReader instead of a FileInputStream.

try {

f = new File(directory, filename); // Create a file object

in = new FileReader(f); // And a char stream to read it

char[] buffer = new char[4096]; // Read 4K characters at a time

int len; // How many chars read each time

textarea.setText(""); // Clear the text area

while((len = in.read(buffer)) != -1) { // Read a batch of chars

String s = new String(buffer, 0, len); // Convert to a string

textarea.append(s); // And display them

}

this.setTitle("FileViewer: " + filename); // Set the window title

textarea.setCaretPosition(0); // Go to start of file

}

// Display messages if something goes wrong

catch (IOException e) {

textarea.setText(e.getClass().getName() + ": " + e.getMessage());

this.setTitle("FileViewer: " + filename + ": I/O Exception");

}

// Always be sure to close the input stream!

finally { try { if (in!=null) in.close(); } catch (IOException e) {} }

}

/**

* Handle button clicks

**/

public void actionPerformed(ActionEvent e) {

this.dispose(); // then close the window

}

/**

* The FileViewer can be used by other classes, or it can be

* used standalone with this main() method.

**/

static public void main(String[] args) throws IOException {

// Create a FileViewer object

Frame f = new FileViewer((args.length == 1)?args[0]:null);

// Arrange to exit when the FileViewer window closes

f.addWindowListener(new WindowAdapter() {

public void windowClosed(WindowEvent e) { System.exit(0); }

});

// And pop the window up

f.show();

}

}

I there a way to add panel4 to the right and split it like splitPaneH.setRightComponent(p4) ??

can some one help me adding, it will be very helpful

Attached Images

Screenshot from 2014-03-13 11:25:16.png
(91.3 KB)

Show more