コメント |
@DIV
import javax.swing.JFileChooser;
import java.io.*;
import java.io.File;
import javax.swing.filechooser.FileFilter;
public class NewJFrame extends javax.swing.JFrame {
/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("\u30d5\u30a1\u30a4\u30eb\u3092\u958b\u304f");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(132, 132, 132)
.add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 196, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(layout.createSequentialGroup()
.add(40, 40, 40)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 406, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(22, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(19, 19, 19)
.add(jButton1)
.add(22, 22, 22)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 440, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(36, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
JFileChooser chooser = new JFileChooser();
myFileFilter filter = new myFileFilter();
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog( null );
if( returnVal == JFileChooser.APPROVE_OPTION ) {
try {
String filePath = chooser.getSelectedFile().getPath();
BufferedReader inFile = new BufferedReader( new FileReader( filePath ) );
String line = null;
String data = "";
while ( ( line = inFile.readLine() ) != null) {
data += line + "\r\n";
}
inFile.close();
jTextArea1.setText(data);
}
catch ( Exception e ) {
System.out.println( e.getMessage() );
}
}
}//GEN-LAST:event_jButton1ActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration//GEN-END:variables
}
class myFileFilter extends FileFilter {
public boolean accept(File f) {
if( f.isDirectory() ) {
return true;
}
String s = f.getName();
int x = s.lastIndexOf('.');
if( x < 0 ) {
return false;
}
String extention = s.substring(x+1).toLowerCase();
if( extention.equals("txt") ) {
return true;
}
if( extention.equals("csv") ) {
return true;
}
return false;
}
public String getDescription() {
return "テキスト";
}
}
@END
|