import java.util.Vector;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import javax.swing.JTable;
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
public class Hauptfenster extends javax.swing.JFrame {
public Hauptfenster() {
super("BT Techarts. Copyright 2008. ");
initComponents();
}
public static String Ergebnisse="";
//Methode sucht Dateien mit bestimmten Endungen; hier MP3`s
private void besucheDirectory (String dir)
{
File verzeichnis = new File(dir);
String aktuellesDirectory= verzeichnis.getAbsolutePath();
if (!verzeichnis.isFile())
{
String Dateien[]=verzeichnis.list();
for (int i = 0;i<Dateien.length;i++)
{
Dateien[i]=aktuellesDirectory + "\\" + Dateien[i];
File aktuelleDatei = new File(Dateien[i]);
if(aktuelleDatei.isFile()&&!aktuelleDatei.isDirectory())
{
int dot = Dateien[i].indexOf(jTextField1.getText());
if (dot>=0)
{
Ergebnisse+=(Dateien[i] + "\n");
}
}
else if (aktuelleDatei.isDirectory())
{
dir = aktuelleDatei.getAbsolutePath();
besucheDirectory(dir);
}
}
}
}
//Suche nach Ordnern
private void besucheOrdner (String dir)
{
File verzeichnis = new File(dir);
String aktuellesDirectory= verzeichnis.getAbsolutePath();
if (!verzeichnis.isFile())
{
String Dateien[]=verzeichnis.list();
for (int i = 0;i<Dateien.length;i++)
{
Dateien[i]=aktuellesDirectory + "\\" + Dateien[i];
File aktuelleDatei = new File(Dateien[i]);
if(aktuelleDatei.isFile()&&aktuelleDatei.isDirectory())
{
int dot = Dateien[i].indexOf(jTextField1.getText());
if (dot>=0)
{
Ergebnisse+=(Dateien[i] + "\n");
}
}
else if (aktuelleDatei.isDirectory())
{
dir = aktuelleDatei.getAbsolutePath();
besucheDirectory(dir);
}
}
}
}
//Listet ebenfalls bestimmte Dateien, aber über andere Struktur
private void JFileChooserDemo()
{
JFileChooser fc = new JFileChooser ();
fc.setFileFilter(new FileFilter()
{
@Override public boolean accept (File f)
{
return f.isDirectory()||f.getName().toLowerCase().endsWith(".doc");
}
@Override public String getDescription ()
{
return "Texte";
}
});
int state = fc.showOpenDialog(null);
if (state == JFileChooser.APPROVE_OPTION)
{
File file =fc.getSelectedFile();
jTextField1.setText(file.getName());
}
else
{
String aus;
aus=("Auswahl abgebrochen");
jTextField1.setText(aus);
}
System.exit(0);
}
//Suche der Interpreten über Abfrage an Access DB
private void aktualisiereTabelle() {
Vector columnNames = new Vector();
Vector data = new Vector();
try {
String query = "SELECT*FROM " + jTextField1.getText() +";";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/Dokumente und Einstellungen/Rasty/Eigene Dateien/Musikdb.mdb");
Statement stmt = con.createStatement();
ResultSet rst = stmt.executeQuery(query);
ResultSetMetaData md = rst.getMetaData();
int columns = md.getColumnCount();
for (int i = 1; i <= columns; i++) {
String colname = md.getColumnName(i);
columnNames.addElement(colname);
}
while (rst.next()) {
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++)
{
row.addElement(rst.getObject(i));
}
data.addElement(row);
}
rst.close();
stmt.close();
}
catch (Exception e) {
e.printStackTrace();
jTextField1.setText("Fehler. Interpret nicht gefunden.");
}
JTable table = new JTable(data, columnNames);
jScrollPane1.setViewportView(table);
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
jButton3 = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jTextField2 = new javax.swing.JTextField();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem3 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem3 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextField1.setMinimumSize(new java.awt.Dimension(40, 20));
jButton3.setText("Suche Lied");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton1.setText("Suche Album");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Suche Interpret");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jTextField2.setEditable(false);
jTextField2.setText("Keine Leerzeichen bei der Suche verwenden!!");
jTextField2.setVerifyInputWhenFocusTarget(false);
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane2.setViewportView(jTextArea1);
jMenu1.setText("File");
jMenuItem3.setText("Öffnen");
jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem3ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem3);
jMenuItem2.setText("Beenden");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem2);
jMenuBar1.add(jMenu1);
jMenu2.setText("Edit");
jMenuItem1.setText("Feld leeren");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem1);
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
jMenu1.setText("File");
jMenuItem3.setText("Öffnen");
jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem3ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem3);
jMenuItem2.setText("Beenden");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem2);
jMenuBar1.add(jMenu1);
jMenu2.setText("Edit");
jMenuItem1.setText("Feld leeren");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem1);
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)
.addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE))
.addGap(31, 31, 31)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
.addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE))
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(240, 240, 240))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(44, 44, 44)
.addComponent(jButton2)
.addGap(11, 11, 11)
.addComponent(jButton1))
.addGroup(layout.createSequentialGroup()
.addGap(36, 36, 36)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton3)
.addGap(18, 18, 18)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 247, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jScrollPane1))
.addGap(150, 150, 150))
);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-800)/2, (screenSize.height-600)/2, 800, 600);
}// </editor-fold>
//Suche Interpret
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
aktualisiereTabelle();
}
//Löscht Textfeld und Textbereich
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText("");
jTextArea1.setText("");
}
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooserDemo();
}
//Suche Lied
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
besucheDirectory("D:/A-Z");
jTextArea1.setText(Ergebnisse);
}
//Suche Album
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
besucheOrdner("D:/A-Z");
jTextArea1.setText(Ergebnisse);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Hauptfenster().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
public javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration
}