Newbie just trying to change icon image in a Jframe
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Newbie just trying to change icon image in a Jframe

  1. #1
    Join Date
    Jan 2012
    Posts
    1

    Newbie just trying to change icon image in a Jframe

    I used Netbeans drag and drop design interface and it spit out this code: (I know some basic Java, but hardly any Swing or Jframe stuff, although I did read a few chapters from Dietel & Deitel book (actually, many chapters). Seems it should be pretty easy but I am not sure exactly how to go about it. The code it generated has setIcon() as part of a private function...I've looked up repaint? and others but the example they gave were not clear to me.

    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /*
     * JavaImageAppUI.java
     *
     * Created on Jan 28, 2012, 6:26:33 PM
     */
    package my.javaimageappui;
    
    /**
     *
     * @author batman
     */
    public class JavaImageAppUI extends javax.swing.JFrame {
    
        /** Creates new form JavaImageAppUI */
        public JavaImageAppUI() {
            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() {
    
            jLabel1 = new javax.swing.JLabel();
            jButton1 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jLabel1.setIcon(new javax.swing.ImageIcon("/home/batman/Desktop/Viper-Green.jpg")); // NOI18N
            jLabel1.setText("jLabel1");
    
            jButton1.setText("Next Image>");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(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()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(148, 148, 148)
                            .addComponent(jButton1))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(62, 62, 62)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 307, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(71, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addGap(38, 38, 38)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
                    .addComponent(jButton1)
                    .addContainerGap())
            );
    
            pack();
        }// </editor-fold>
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    //This is where I would like to add some code to change the photo
    }
    
        /**
         * @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 http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            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(JavaImageAppUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(JavaImageAppUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(JavaImageAppUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(JavaImageAppUI.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 JavaImageAppUI().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        private javax.swing.JLabel jLabel1;
        // End of variables declaration
    }
    I figure I can make a function that does setIcon()? Not exactly sure to do it though. How do I add programming to change the image Viper-Green.jpg (to another image in same directory called Viper-Orange.jpg)?

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Newbie just trying to change icon image in a Jframe

    I'm not sure I understand what you want to do as you don't appear to understand the code you have generated and hence your question is not clear.

    The code it generated has setIcon() as part of a private function
    Terminology: Java has methods and not functions.

    setIcon() is not a private method and it has not been generated by NetBeans, it's public method declared in the JLabel class.

    How do I add programming to change the image Viper-Green.jpg (to another image in same directory called Viper-Orange.jpg)?
    Just do a search and replace ie search for "Viper-Green.jpg" and replace it with "Viper-Orange.jpg".

    If you mean how do you change it programmatically ie once the program is running then at the point you want to change it you need to call setIcon(..) passing in the new icon image.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center