I want to allow user to select files from a fixed directory. I don’t want user to navigate to other places.
How can I make a list of files where user can tap on a file to select? The list should be dynamic and can be updated as required.
Thanks in advance.
A first step can be :
File projetFile = dataFile(sketchPath()); // access to your data folder
if (projetFile!=null) {
for (int i=0; i<projetFile.list().length; i++) println(projetFile.list()[i]);
}
2 Likes
Thanks for the response.
Do you also know how to make the list in a GUI and user can tap on an item to select the file?
Let me update my library, there’s a menu for just this function.
svan
October 14, 2021, 4:57am
5
For your consideration; works ok in Android mode, never tried it in Java. Automatically adjusts to length of array.
// Drop Down List control parts => a.)display field, b.)arrow, c.)listItems
// Rev_1
final int _displayX = 100;
final int _displayY = 120;
final int _displayW = 500;
final int _displayH = 70;
final int _itemH = 70;
final int _arrwX = _displayX + _displayW;
final int _arrwY = _displayY;
final int _arrwSize = _displayH;
boolean dropped;
color BLUE = color(64,124,188);
color GREEN = color(0,126,0);
String[] fruit = {"Apples","Peaches","Oranges","Bananas","Pears", "Cherries"};
int[] _itemY;
int selectedItem = -1;
class ListDisplay {
void press(float mx, float my){
// arrow touches
if(dropped){
dropped = false;
} else {
dropped = true;
}
println("dropped =",dropped);
}
void displayFieldString(String str) {
fill(255); // display field background color
rect(_displayX,_displayY,_displayW,_displayH);
fill(0); // text color
textSize(42);
text(str, _displayX + 10, _displayY + 15, _displayW, _displayH);
}
void display(){
// display field
if(selectedItem == -1){
displayFieldString("Select fruit:");
} else {
displayFieldString(fruit[selectedItem]);
}
// arrow
fill(255); // arrow background color
rect(_arrwX, _arrwY, _arrwSize, _arrwSize);
fill(GREEN); // arrow color
triangle(_arrwX+5, _arrwY+5, _arrwX+_arrwSize-5, _arrwY+5, _arrwX+_arrwSize/2, _arrwY+_arrwSize-5);
}
}
ListDisplay listDisplay;
class ListItems {
void press(float mx, float my){
// item touches
if (dropped) {
for(int j = 0; j < fruit.length; j++){
if((mx >= _displayX) && (mx <= _displayX + _displayW) && (my >= _itemY[j] ) && (my <= _itemY[j] + _itemH)) {
selectedItem = j;
dropped = false;
println("dropped =",dropped);
}
}
}
}
void display() {
// list items
if (dropped){
_itemY = new int[fruit.length];
for(int j = 0; j < fruit.length; j++){
_itemY[j] = (_displayY + _displayH) + j*_itemH;
fill(255);
rect(_displayX,_itemY[j],_displayW,_itemH);
fill(0);
textSize(42);
text(fruit[j], _displayX + 10, _itemY[j] + 15, _displayW, _itemH);
}
}
}
}
ListItems listItems;
void setup() {
fullScreen();
// orientation(LANDSCAPE);
orientation(PORTRAIT);
listDisplay = new ListDisplay();
listItems = new ListItems();
dropped = false; // initial setting
}
void draw() {
background(BLUE);
listDisplay.display();
listItems.display();
}
void mousePressed(){
if((mouseX >= _arrwX) && (mouseX <= _arrwX+_arrwSize) && (mouseY >= _arrwY) && (mouseY <= _arrwY+_arrwSize)){
listDisplay.press(mouseX, mouseY);
}
if(dropped) {
if((mouseX >= _displayX) && (mouseX <= _displayX + _displayW) && (mouseY >= _displayY + _displayH) && (mouseY <= _displayY + _displayH + fruit.length*_itemH)) {
listItems.press(mouseX, mouseY);
}
}
}
Thanks, I’ll give it a try.
Works like a charm in Java and Android, thanks so much.
1 Like