Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to output the list of files of the folder in Swing  RSS feed

 
adagio life
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need create the window with the title of the folder name. In the north area i need create the text field where i need to insert the path to to folder. In the central area it needs create the text field where to output the list of the files of folder with such features as the name type size and last change of files. The output shoud be done after Clicking Enter key. Where i con find the similar examples what libraries we need to use. How create the text fields and connect the inserting text and outputuing list to fields. And the main how connect the clicking Enter with some event code with outputting list. It should probably through the .list method.
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1103
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi adagio,
Welcome to the Ranch!

There are 2 parts to your question. You need to work with 2 APIs to achieve this. First is I/O and you can read about folders/files in this tutorial

Second part deals with creating a GUI window. If you are not aware of programming in Swing, you can start here. You need to understand layouts to position the components as you want. You can read about layouts here.

Your requirement to display the name, size, last changed would fit better in a JTable than a JTextField.
 
adagio life
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@@@@import javax.swing.*;
import javax.swing.JTextField;
import java.io.*;
import java.io.File;
import java.awt.*;
import java.awt.event.*;
public class MyFrame1
extends JFrame {
public MyFrame1(){
setLocation(400, 200);
setSize(200, 200);
setTitle("Enter Folder Name");
setVisible(true);
JTextField t=new JTextField(80);
add (t);
JTextArea area=new JTextArea(10,100);
add (area);
t.addKeyListener (new KeyAdapter (){
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_ENTER){
String line=t.getText().trim();
}}});
File d = new File("D:qqq");
if (d.exists() && d.isDirectory()) {
String[] s = d.list();
for (int i = 0; i < s.length; i++)
String t=(s[i]+"\t"+s[i].length ()+"\n");
area.append (t);
}
}
}
}
public static void main(String[] args) {
new MyFrame1();
}
}

Here is my code. Could anybody point to the main errors especially in structure? why textf and textarea is not displayed correctly how to output the string of files names array (is there such possibility to concatanete si and si.length) especially how to show the feature last modified?
 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would probably use a JFileChooser for this.
 
adagio life
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jfilechooser: for what? I an tasked to use the jawa library: jawa.io.File. But anyway what to correct as the most of job is done.
 
Piet Souris
Rancher
Posts: 1980
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob is right. How about:



But there is nothing wrong with your code. You might want to put your textarea in a JScrollPane.
And if you want to do more: how about displaying the contents of a subdirectory, if one of the displayed files happens to be a directory?

 
adagio life
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need use key ENTER as an event. But compiler says that t shoud be final. Anyway the code is not workable as the textfield and textarea are not created properly. But if it would work i do not see the nessecity to use filechooses here as there is an method getText. Them i need it to bound to some variable string and transfer as the parametr to file name of folder where files should be leasted. I think it is the main bottleneck. Should I create new class or method for it after selecting the text.
 
adagio life
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class TestFrame extends JFrame {

public TestFrame() {
super("Тестовое окно");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

panel.add(Box.createVerticalGlue());

final JLabel label = new JLabel("Выбранный файл");
label.setAlignmentX(CENTER_ALIGNMENT);
panel.add(label);

panel.add(Box.createRigidArea(new Dimension(10, 10)));

JButton button = new JButton("Показать JFileChooser");
button.setAlignmentX(CENTER_ALIGNMENT);

button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fileopen = new JFileChooser();
int ret = fileopen.showDialog(null, "Открыть файл");
if (ret == JFileChooser.APPROVE_OPTION) {
File file = fileopen.getSelectedFile();
label.setText(file.getName());
}
}
});

panel.add(button);
panel.add(Box.createVerticalGlue());
getContentPane().add(panel);

setPreferredSize(new Dimension(260, 220));
pack();
setLocationRelativeTo(null);
setVisible(true);
}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
new TestFrame();
}
});
}
}

аt the basic of this code i created mine? that is compiled except the last line
@@@import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.File;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class TestFrame extends JFrame {

public TestFrame() {
super("Тестовое окно");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());

final JPanel rootContent = new JPanel();
rootContent.setLayout(new BoxLayout(rootContent, BoxLayout.Y_AXIS));
JTextField t=new JTextField(80);



t.addKeyListener (new KeyAdapter (){
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_ENTER){
String line=t.getText().trim();

File[] children = line.listFiles();
if (children != null) {
for (int i = 0; i < children.length; i++) {
JLabel label = new JLabel(children[i].getName()+"zmineno"+children[i].lastModified()+"dovzhyna"+children[i].length());
rootContent.add(label);
}
}
rootContent.repaint();
rootContentScroll.revalidate();
}

mainPanel.add(new JScrollPane(list), BorderLayout.NORTH);
mainPanel.add(rootContentScroll, BorderLayout.CENTER);

getContentPane().add(mainPanel);

setPreferredSize(new Dimension(260, 220));
pack();
setLocationRelativeTo(null);
setVisible(true);
}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame.setDefaultLookAndFeelDecorated(true);
new TestFrame();
}
});
}
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please UseCodeTags in the future.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!