icon aus exe auslesen mithilfe des JFilechooser ?

FunZel

Mitglied
Hallo an alle,

ich hoffe euch gehts allen gut 😉

Ich würde gern ein Icon aus einer exe auslesen mithilfe des JfileChooser und sie dann als ausführbarer button sichtbar machen, nur ich weiss leider nicht wie, da meine Java kenntnisse richtung 0 gehen.

Ich habe auch schon mehrere stunden gegooglt. Bin leider nicht zu dem Erfolg gekommen, den ich erziehlen wollte.

Ich "Arbeite" mit netbeans.

Icon aus exe auslesen habe ich bei google gefunden.


Java:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication17;

import java.io.File;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.filechooser.FileSystemView;

/**
 *
 * @author Funzel
 */
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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>
     
 
  
      
        public static void main(String[] args) {

           
            String s = "C:/Windows/notepad.exe";
        File file = new File (s);

        // Get metadata and create an icon
        Icon icon = FileSystemView.getFileSystemView().getSystemIcon(file);
 
        // show the icon
        JLabel ficon = new JLabel(s, icon, SwingConstants.LEFT);
        

        JFrame frame = new JFrame();
        frame.getContentPane().add(ficon);
        frame.pack();
        frame.setVisible(true);
 
    
        /*  java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
               }
        });
        */
  }
      
      
      
      
      
      /**
     * @param args the command line arguments
     */
  
    // Variables declaration - do not modify
    // End of variables declaration
}

Nur da ich leide nicht die Ahnung von JAVA habe, hänge ich an diesem Punkt leider fest.

Mein "lächerlicher" ansatz hierzu wäre

Java:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication16;
import java.io.File;
import javax.swing.Icon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.filechooser.FileSystemView;

/**
 *
 * @author Funzel
 */
public class NewJFrame extends javax.swing.JFrame {
    private File file;
    private String s;

    /**
     * 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("What ...");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ActionPerformedLink(evt);
            }
        });

        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(151, 151, 151)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(163, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(68, 68, 68)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(188, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void ActionPerformedLink(java.awt.event.ActionEvent evt) {                                     
       
           JFileChooser chooser = new JFileChooser();
           chooser.setCurrentDirectory(new java.io.File( System.getProperty("user.home") + "\\Desktop\\" ));
           chooser.setDialogTitle("choosertitle");
           chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
           chooser.setAcceptAllFileFilterUsed(true);


                               
        if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION)
        {
        
            String s = getName();
        File file = new File (s);

        // Get metadata and create an icon
        Icon icon = FileSystemView.getFileSystemView().getSystemIcon(file);
 
        // show the icon
        JLabel ficon = new JLabel(s, icon, SwingConstants.LEFT);
        

          
            System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
            System.out.println("getSelectedFile() : " + chooser.getSelectedFile());
        }
        else {
            System.out.println("No Selection ");
        }
        

    }                                    

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see [url=http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html]How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)[/url] 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    // End of variables declaration
}

Vielen Dank für jede Hilfe && Durchlesen
- Lg - FunZel
 
Ich weiss halt nicht wo ich ansetzten soll ... ich kann die Jfile chosser aufrufen, aber mit dem output nichts anfangen ... ich kann die exe auslesen aber weiss nicht weiter ..

Was ich halte machen möchte ist ... die ausgewählte datei von "jfilechooser" nehmen und damit einen neuen button erstellen, jener soll dann die ausgewählte datei starten und das icon der ausgewählten exe enthalten ...


ich weiss klinkt sehr kompliziert aber es ist genau das was ich brauche .... vllt hat jemand andere vorschläge wie ich das machen kann .

lg
 
Java:
javax.swing.filechooser.FileSystemView.getSystemIcon(java.io.File)
 
Hab die lösung für mein "Problem gefunden".

Java:
    private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          

    if     (ProgramsButton.isEnabled())
            {
            JFileChooser chooser = new JFileChooser();
            chooser.showSaveDialog(null);
            File f1 = chooser.getSelectedFile();
            Icon icon = FileSystemView.getFileSystemView().getSystemIcon(f1);
            final String filename =f1.getAbsolutePath();
            LinkStart.setIcon(icon);
    if      (AddButton.isEnabled()) 
            {
            LinkStart.addActionListener(new ActionListener() 
            {
            public void actionPerformed(ActionEvent e)
            {
            LinkStart.addActionListener(null);
            {
    try
            {             
            Process p = Runtime.getRuntime().exec(filename);
            Thread.sleep(0);
            }
    catch
            (IOException | InterruptedException m)
            {   
            m.printStackTrace();
            }
            }
           }
          }); // diese Klammer wird rot unterstrichen und als " ')' expected " gekennzeichnet Edit "Done"
         }
        }
    else
        {
            ProgramsButton.setEnabled(false);
        }
    }


Wie schon im Code zu sehen ist, bekomme ich eine "Fehlermeldung" aber debugger sagt nichts:

debug:
BUILD SUCCESSFUL (total time: 14 seconds)

Ich weiss nicht ob, das wirklich ein problem darstellt, aber ich hasse jene ... ich hoffe jmd. kann mir dort weiterhelfen.

Wie schon im 1. Post geschrieben benutzte ich Netbeans.

[EDIT]Edit: Wer lesen kann ist klar im Vorteil .....

Funktioniert nun alles wie gewünscht.[/EDIT]

Lg
 
Zuletzt bearbeitet:

Zurück
Oben