Click to See Complete Forum and Search --> : jList help


MadSkillz
December 4th, 2009, 04:20 PM
Hello!
I have 3 jLists (jList1, jList2, jList3).
Is there any way that I can click on one item in the 1st jList and view the content (the files in the folder) of 2 different folders in the other 2 jLists at the same time? Help me with the code pls! Thank you!

dlorde
December 4th, 2009, 05:35 PM
I think the answer is probably 'yes', but you'll have to explain more clearly what you want. How are the 2 folders displayed in the 2nd & 3rd JLists related to the item clicked in the 1st JList?

The hardest part of the software task is arriving at a complete and consistent specification, and much of the essence of building a program is in fact the debugging of the specification...
F. Brooks

MadSkillz
December 5th, 2009, 12:20 AM
if i click the 1st Jlist ITEM1 in the 2nd list will be displayed *.* of D:\REC\ITEM1 and in the 3rd Jlist will be despayed *.* of D:\TRS\ITEM1

if i click the 1st Jlist ITEM2 in the 2nd list will be displayed *.* of D:\REC\ITEM2 and in the 3rd Jlist will be despayed *.* of D:\TRS\ITEM2

if i click the 1st Jlist ITEM3 in the 2nd list will be displayed *.* of D:\REC\ITEM3 and in the 3rd Jlist will be despayed *.* of D:\TRS\ITEM3

and so on...

keang
December 5th, 2009, 04:45 AM
Create a File object for the directory you want to display the contents of and call it's list() method to obtain a list of the files in the directory. Use this array of file names to populate the JList.

If you want to restrict the type of files to display there is a list(..) method that takes a FilenameFilter object. You can use this to filter the names to be included in the list for example to only include files with a .txt extension or files beginning with 'S' etc.

MadSkillz
December 6th, 2009, 01:21 AM
Create a File object for the directory you want to display the contents of and call it's list() method to obtain a list of the files in the directory. Use this array of file names to populate the JList.

If you want to restrict the type of files to display there is a list(..) method that takes a FilenameFilter object. You can use this to filter the names to be included in the list for example to only include files with a .txt extension or files beginning with 'S' etc.

Hmmm... Can you help me with the code, if you don't mind. I'm a beginner and I'm learning from examples.

keang
December 7th, 2009, 08:47 AM
Hmmm... Can you help me with the code...Err No. The best way to learn is to try to do it yourself.

I've explained what you should be trying to do, now it's your turn to put some effort in. If you get stuck post your code and we will help but it's unlikely anyone here is going to write it for you.

dlorde
December 8th, 2009, 07:33 AM
Hmmm... Can you help me with the code, if you don't mind. I'm a beginner and I'm learning from examples.

I'll help you with the code - just post the code you need help with and explain exactly what part you are having difficulty with.

Learning results from what the student does and thinks, and only from what the student does and thinks. The teacher can advance learning only by influencing the student to learn...
H. Simon

MadSkillz
December 8th, 2009, 03:21 PM
public class RxFer_v3Frame extends javax.swing.JFrame {

public RxFer_v3Frame() {
initComponents();
}

@SuppressWarnings("unchecked")

private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList();
jPanel2 = new javax.swing.JPanel();
jScrollPane2 = new javax.swing.JScrollPane();
jList2 = new javax.swing.JList();
jPanel3 = new javax.swing.JPanel();
jScrollPane3 = new javax.swing.JScrollPane();
jList3 = new javax.swing.JList();
jProgressBar1 = new javax.swing.JProgressBar();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jSeparator2 = new javax.swing.JSeparator();
jMenuItem2 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenuItem3 = new javax.swing.JMenuItem();
jSeparator1 = new javax.swing.JSeparator();
jMenuItem4 = new javax.swing.JMenuItem();
jMenu3 = new javax.swing.JMenu();
jMenuItem5 = new javax.swing.JMenuItem();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setResizable(false);

jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(" Groups: "));

jList1.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Esa", "Escl", "Media" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
jScrollPane1.setViewportView(jList1);

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 378, Short.MAX_VALUE)
);

jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(" Files for upload: "));

jScrollPane2.setViewportView(jList2);

javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 378, Short.MAX_VALUE)
);

jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(" Downloaded files: "));

jScrollPane3.setViewportView(jList3);

javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 378, Short.MAX_VALUE)
);

jProgressBar1.setPreferredSize(new java.awt.Dimension(146, 15));

jMenu1.setText("Upload");

jMenuItem1.setText("Upload 1");
jMenu1.add(jMenuItem1);
jMenu1.add(jSeparator2);

jMenuItem2.setText("Upload 2");
jMenu1.add(jMenuItem2);

jMenuBar1.add(jMenu1);

jMenu2.setText("Download");

jMenuItem3.setText("Download 1");
jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem3ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem3);
jMenu2.add(jSeparator1);

jMenuItem4.setText("Download 2");
jMenu2.add(jMenuItem4);

jMenuBar1.add(jMenu2);

jMenu3.setText("Quit");

jMenuItem5.setText("Quit RFer");
jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem5ActionPerformed(evt);
}
});
jMenu3.add(jMenuItem5);

jMenuBar1.add(jMenu3);

setJMenuBar(jMenuBar1);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jProgressBar1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);

pack();
}

private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {

}

private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {

System.exit(0);
}


public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new RxFer_v3Frame().setVisible(true);
}
});
}


private javax.swing.JList jList1;
private javax.swing.JList jList2;
private javax.swing.JList jList3;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JMenuItem jMenuItem5;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JProgressBar jProgressBar1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JSeparator jSeparator2;

}



if i click the 1st Jlist Esa in the 2nd jlist will be displayed *.* of D:\REC\Esa and in the 3rd Jlist will be despayed *.* of D:\TRS\Esa

if i click the 1st Jlist Escl in the 2nd jlist will be displayed *.* of D:\REC\Escl and in the 3rd Jlist will be despayed *.* of D:\TRS\Escl

if i click the 1st Jlist Media in the 2nd jlist will be displayed *.* of D:\REC\Media and in the 3rd Jlist will be despayed *.* of D:\TRS\Media

and so on...

dlorde
December 8th, 2009, 06:42 PM
No, I didn't mean post empty method bodies for the bit you need help with, I meant post your attempt to solve the problem (i.e. fill in the missing code) and explain exactly where you are getting stuck with it.

If you're not going to bother to try to solve your problem, I certainly won't.

If I had eight hours to chop down a tree, I would spend 6 hours sharpening an axe...
Anon.

MadSkillz
December 9th, 2009, 01:34 AM
I posted an image too.

I need an event or something that when I'm clicking on a certain item ("Esa", "Escl", "Media") from this jlist to open in the other 2 jlists 2 different directories



jList1 = new javax.swing.JList();

jList1.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Esa", "Escl", "Media" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});

jScrollPane1.setViewportView(jList1);

MadSkillz
December 9th, 2009, 05:03 AM
I bought The Definitive Guide to NetBeans Platform 6.5 but there is nothing there about showing files in a jList.

keang
December 9th, 2009, 05:36 AM
try reading this (http://java.sun.com/docs/books/tutorial/uiswing/events/index.html)

MadSkillz
December 12th, 2009, 11:20 AM
i made this program to list all files and folders from a specified location. but how to make them be displayed in a jList? Help me with the code pls. I already have headache.








import java.io.File;
import java.util.Scanner;

public class DirectoryList {


public static void main(String[] args) {

String directoryName;
File directory;
String[] files;
Scanner scanner;

scanner = new Scanner(System.in);

System.out.print("Enter a directory name: ");
directoryName = scanner.nextLine().trim();
directory = new File(directoryName);

if (directory.isDirectory() == false) {
if (directory.exists() == false)
System.out.println("There is no such directory!");
else
System.out.println("That file is not a directory.");
}
else {
files = directory.list();
System.out.println("Files in directory \"" + directory + "\":");
for (int i = 0; i < files.length; i++)
System.out.println(" " + files[i]);
}

}

}

dlorde
December 13th, 2009, 06:06 AM
This really isn't rocket science... have you read the JList API doc with its examples? Have you read the Java Tutorial on How To Use Lists (http://java.sun.com/docs/books/tutorial/uiswing/components/list.html) ?

If so, please explain exactly which part of displaying a list of files in a JList you are stuck on.

Remember, you can add a collection or array of elements to a JList when you construct it, but if you want to add individual elements, you need to create the ListModel (use DefaultListModel) yourself and add the elements to that.

Good teaching is more a giving of the right questions than a giving of the right answers...
J. Albers