Java Fifth time job

Source: Internet
Author: User

Use component JList to complete the display of specified file contents with a list component

Import java.awt.*;
Import java.util.ArrayList;
Import java.util.List;
Import javax.swing.*;

public class Liebiao extends JFrame {
Private static final String music = "File";
Private JFrame frame;
Private JList fileList;

Public Liebiao () {

string[] FileNames = findfiles (music, NULL);
Makeframe (FileNames);
Frame.pack ();
Frame.setvisible (TRUE);
Frame.setsize (250, 380);
Frame.setlocation (550, 400);
Frame.setdefaultcloseoperation (Jframe.exit_on_close);

Private string[] FindFiles (string dirName, string suffix) {
File dir = new file (dirName);
if (Dir.isdirectory ()) {
string[] Allfiles = Dir.list ();
if (suffix = = null) {
return allfiles;
} else {
List<string> selected = new arraylist<string> ();
for (String filename:allfiles) {
if (filename.endswith (suffix)) {
Selected.add (filename);
Return Selected.toarray (New String[selected.size ()));
} else {
System.out.println ("Error:" + dirName + "must be a directory");
return null;

private void Makeframe (string[] audiofiles) {

frame = new JFrame ();
Setdefaultcloseoperation (Exit_on_close);

JPanel ContentPane = (JPanel) getcontentpane ();

Contentpane.setlayout (New BorderLayout (8, 8));

JPanel Leftpane = new JPanel ();
Leftpane.setlayout (New BorderLayout (8, 8));

FileList = new JList (audiofiles);
Filelist.setforeground (New Color (140, 171, 226));
Filelist.setbackground (New Color (0, 0, 0));
Filelist.setselectionbackground (New Color (87, 49, 134));
Filelist.setselectionforeground (New Color (140, 171, 226));
JScrollPane ScrollPane = new JScrollPane (fileList);
Scrollpane.setcolumnheaderview (New JLabel ("Files list");
Leftpane.add (ScrollPane,;
Contentpane.add (Leftpane,;
Frame.add (ContentPane);

public static void Main (string[] args) {
TODO auto-generated Method Stub
New Liebiao ();



Java Fifth time job

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.