Results 1 to 3 of 3
  1. #1
    darth_tater is offline Member
    Join Date
    Jan 2012
    Posts
    1
    Rep Power
    0

    Default 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.

    Java 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)?



    ****
    I solved my own "problem". Ha, spent a few hours googling changing an image on Jframe, all I had to do was fiddle with the code.

    Java Code:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    jLabel1.setIcon(new javax.swing.ImageIcon("/home/batman/Desktop/Viper-Orange.jpg"));
    }
    seems to work. hmm it seemed it had not worked earlier.

    ***Now my question is though: I figure this is not buffered? So every time I load a new image is the old image being discarded or staying in memory?
    Last edited by darth_tater; 01-29-2012 at 02:27 AM.

  2. #2
    Majora94 is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

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

    At the start of the initComponents() function you can set it by using

    frame.setIconImage(Toolkit.getDefaultToolkit().get Image("your_image.gif"));

    But you cannot use frame.setIco..etc because there is no frame created at the start. Therefore you can replace 'frame.' the default frame for a java app. This can be accessed by using 'this' or getParentComponant I think. This is all off the top of my head but at least you now know what you are looking for. Just google these parts

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

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

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. the icon won't change on clicking
    By rajasohaibmaroof in forum AWT / Swing
    Replies: 36
    Last Post: 10-03-2011, 07:46 AM
  2. icon does not change when I want it too
    By drewtrcy in forum New To Java
    Replies: 1
    Last Post: 05-05-2011, 05:10 PM
  3. change icon of frame?
    By Manish87 in forum AWT / Swing
    Replies: 9
    Last Post: 04-15-2011, 04:11 AM
  4. How to change Window Icon
    By sharafat in forum AWT / Swing
    Replies: 7
    Last Post: 01-15-2010, 12:37 AM
  5. [SOLVED] JFrame Image Icon
    By Ch4os in forum NetBeans
    Replies: 1
    Last Post: 11-24-2008, 10:53 AM

Posting Permissions

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