Problem mit JLabel und setIcon

  • Themenstarter Themenstarter Gast
  • Beginndatum Beginndatum
Status
Nicht offen für weitere Antworten.
G

Gast

Gast
Ich möchte ganz einfach nur ein JDialog erzeugen, der einen JButton zum Bestätigen enthält. Zu dem soll über dem JButton ein Label stehen mit eigenem Icon.
Ich habe schon viel im Net recherchiert und habe bisher keine zufrieden stellende Lösung zu folgendem runtime-error gefunden:

Code:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at javax.swing.ImageIcon.<init>(ImageIcon.java:138)
        at sn32.mapeditor32.ErrorMsg.JErrorDialog.initComponents(JErrorDialog.java:26)
        at sn32.mapeditor32.ErrorMsg.JErrorDialog.<init>(JErrorDialog.java:7)
        at sn32.mapeditor32.ErrorMsg.JErrorDialog$1.run(JErrorDialog.java:61)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
        at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)



Hierzu der Quellcode:

Code:
package sn32.mapeditor32.ErrorMsg;

public class JErrorDialog extends javax.swing.JDialog {
    
    public JErrorDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Fehlermeldung");
        setName("JErrorDialog"); // NOI18N
        setResizable(false);

        jButton1.setText("OK");
        jButton1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jButton1.setDebugGraphicsOptions(javax.swing.DebugGraphics.BUFFERED_OPTION);
        jButton1.setDoubleBuffered(true);
        jButton1.setName("JButtonOK"); // NOI18N

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getClassLoader().getResource("icons/01.gif")));
        jLabel1.setLabelFor(this);
        jLabel1.setText("Falsches Tileset-Format. ");
        jLabel1.setName("JLabel"); // NOI18N

        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()
                .addContainerGap(22, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 73, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(71, 71, 71))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 182, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(18, 18, 18))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(18, 18, 18)
                .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 27, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        
    
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                JErrorDialog dialog = new JErrorDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration                   
    
}

Sämtliche Vorschläge in Foren bzgl. der Quellcode-Zeile...
Code:
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getClassLoader().getResource("icons/01.gif")));
...konnten das Problem nicht beseitigen. Die Datei "01.gif" liegt im gleichen Verzeichnis wie die *.class-Datei.
Ich habe setIcon auch von netbeans selber generieren lassen. Jedoch genauso ohne Erfolg - ähnlich in eclipse.

Weiß jmd. Rat?

Danke im Voraus :?
 
Mal davon abgesehen.
Ich habe beides versucht: mit Unterverzeichnis ("/icons/01.gif") und ohne Untervezeichnis ("01.gif") (ich habe den Quellcode an der Stelle vergessen abzuändern. Es sollte heißen
Code:
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getClassLoader().getResource("01.gif")));

Nunja, wie dem auch sei. Es haut trotzdem nicht hin.
Muss ich in netbeans irgendwo irgendwie Verzeichnisse hinzufügen? ???:L
Anstelle von "/" "//" zu benutzen oder mit "./" einzubauen bringt auch nichts.
 
Nochmal ein kleiner Nachtrag:

Ohne-Titel.gif


Analoge Situation im .../build/classes/...-Verzeichnis.
Vielleicht habe ich ja nur etwas übersehen :autsch:
 
Hey,

lass ma den classLoader weg, dann gehts:

Code:
jLabel1.setIcon(new ImageIcon(getClass().getResource("01.gif")));


Warum allerdings... weiß ich grad au ned...
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben