Java displays files of the specified type

Source: Internet
Author: User

As a unit of data storage, files are classified into many types, which are called file types. When operating data files, different types of files are often required for different processing. In this example, files of the specified type in the folder are read and displayed in the table control. This has played a leading role in file classification during project development.

Train of Thought Analysis:

The Code is as follows:

Import java. awt. borderLayout; import java. awt. eventQueue; import java. awt. gridBagConstraints; import java. awt. gridBagLayout; import java. awt. insets; import java. awt. event. actionEvent; import java. awt. event. actionListener; import java. io. file; import java. SQL. date; import javax. swing. JButton; import javax. swing. JFileChooser; import javax. swing. JFrame; import javax. swing. JLabel; import javax. swing. JPanel; import j Avax. swing. JScrollPane; import javax. swing. JTable; import javax. swing. JTextField; import javax. swing. border. emptyBorder; import javax. swing. event. caretEvent; import javax. swing. event. caretListener; import javax. swing. table. defaultTableModel; public class ListCustomTypeFile extends JFrame {/*****/private static final long serialVersionUID =-626213104443132420l; /*** custom extension filter ** @ author Li Zhongwei */priv Ate final class CustomFilter implements java. io. fileFilter {@ Override public boolean accept (File pathname) {// obtain the specified extension String extName = extNameField set by the user. getText (); if (extName = null | extName. isEmpty () return false; if (! ExtName. startsWith (". ") // determine the extension prefix extName = ". "+ extName; // The extension prefix extName = extName. toLowerCase (); // determines whether the extension meets the criteria for filtering the file name if (pathname. getName (). toLowerCase (). endsWith (extName) return true; return false ;}} private JPanel contentPane; private JTextField extNameField; private JTable table; private File dir; private JLabel label; /*** Launch the application. */public static void main (String [] args) {EventQueue. invokeLater (new Runnable () {public void run () {try {ListCustomTypeFile frame = new ListCustomTypeFile (); frame. setVisible (true);} catch (Exception e) {e. printStackTrace () ;}});}/*** Create the frame. */public ListCustomTypeFile () {setTitle ("display files of the specified type"); setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); setBounds (100,100,450,300); contentPane = new JPanel (); contentPane. setBorder (new EmptyBorder (5, 5, 5, 5); contentPane. setLayout (new BorderLayout (0, 0); setContentPane (contentPane); JPanel panel = new JPanel (); contentPane. add (panel, BorderLayout. NORTH); GridBagLayout gbl_panel = new GridBagLayout (); gbl_panel.columnWidths = new int [] {93, 54, 0}; gbl_panel.rowHeights = new int [] {23, 0, 0 }; gbl_panel.columnWeights = new double [] {0.0, 1.0, Double. MIN_VALUE}; gbl_panel.rowWeights = new double [] {0.0, 0.0, Double. MIN_VALUE}; panel. setLayout (gbl_panel); JButton button = new JButton ("select folder"); button. addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent e) {response (e) ;}}); GridBagConstraints gbc_button = new GridBagConstraints (); response = GridBagConstraints. NORTH; gbc_button.gridx = 0; gbc_button.gridy = 0; panel. add (button, gbc_button); label = new JLabel ("folder"); GridBagConstraints gbc_label = new GridBagConstraints (); gbc_label.fill = GridBagConstraints. HORIZONTAL; gbc_label.gridx = 1; gbc_label.gridy = 0; panel. add (label, gbc_label); JLabel label_1 = new JLabel ("Enter the extension name of the specified file:"); GridBagConstraints gbc_label_1 = new GridBagConstraints (); constraint = GridBagConstraints. EAST; gbc_label_1.insets = new Insets (0, 0, 0, 5); gbc_label_1.gridx = 0; gbc_label_1.gridy = 1; panel. add (label_1, gbc_label_1); extNameField = new JTextField (); extNameField. addCaretListener (new CaretListener () {public void caretUpdate (CaretEvent e) {do_extNameField_caretUpdate (e) ;}}); extNameField. setText (". gif); GridBagConstraints gbc_extNameField = new GridBagConstraints (); gbc_extNameField.insets = new Insets (0, 0, 5, 0); gbc_extNameField.fill = GridBagConstraints. HORIZONTAL; gbc_extNameField.gridx = 1; gbc_extNameField.gridy = 1; panel. add (extNameField, gbc_extNameField); extNameField. setColumns (10); JScrollPane scrollPane = new JScrollPane (); contentPane. add (scrollPane, BorderLayout. CENTER); table = new JTable (); table. setAutoResizeMode (JTable. AUTO_RESIZE_OFF); table. setModel (new DefaultTableModel (new Object [] [] {}, new String [] {"file name", "file size", "Modify date "}) {/***/private static final long serialVersionUID = 5274214559103654856L; boolean [] columnEditables = new boolean [] {false, false, false}; public boolean isCellEditable (int row, int column) {return columnEditables [column] ;}}); table. getColumnModel (). getColumn (0 ). setPreferredWidth (220); table. getColumnModel (). getColumn (1 ). setPreferredWidth (85); table. getColumnModel (). getColumn (2 ). setPreferredWidth (110); scrollPane. setViewportView (table);}/*** select the folder button event processing method ** @ param e */protected void do_button_actionreceivmed (ActionEvent e) {JFileChooser chooser = new JFileChooser (); // create a file selector // set the selector's filter chooser. setFileSelectionMode (JFileChooser. DIRECTORIES_ONLY); chooser. showDialog (this, null); dir = chooser. getSelectedFile (); getLabel (). setText (dir. toString (); // obtain the filtered array of eligible files listFiles ();}/*** display files in the folder */private void listFiles () {if (dir = null) return; // obtain the File array File [] files = dir. listFiles (new CustomFilter (); // obtain the table data model DefaultTableModel model = (DefaultTableModel) table. getModel (); model. setRowCount (0); for (File file: files) {// traverse the file array // create table row data Object [] row = {File. getName (), file. length (), new Date (file. lastModified ()}; model. addRow (row); // Add row data to table model} protected void do_extNameField_caretUpdate (CaretEvent e) {listFiles ();} protected JLabel getLabel () {return label ;}}

Effect

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: info-contact@alibabacloud.com 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.