Results 1 to 4 of 4
  1. #1
    ashton is offline Member
    Join Date
    Jan 2009
    Posts
    31
    Rep Power
    0

    Default Changing images by clicking arrow buttons. help?

    Hi guys! I'll just go straight to the point.

    I have a program that displays an image. what i want to do is, as i click the right button, the image will change to a different one. And if i click the left button it will display the previous image.

    I was playing with if-else's, do-whiles, but I still cant get through my problem..

    Does anyone knows how to write a code that can enable you to browse images with the help of left and right buttons?? Please, I need you help.

    Here's my code anyways: (just copy it all and run it)
    Java Code:
    [FONT="Courier New"]/*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /*
     * CounterFrame.java
     *
     * Created on Feb 6, 2009, 12:53:29 PM
     */
    
    package mycounter;
    
    //some imported packages are here..
    import java.io.*;
    import javax.swing.UIManager.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.lang.*;
    import java.lang.String;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    /**
     *
     * @author Kent
     */
    public class CounterFrame extends javax.swing.JFrame {
    
        /** Creates new form CounterFrame */
        public CounterFrame() {
    
    
            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() {
    
            jDialog1 = new javax.swing.JDialog();
            jFileChooser1 = new javax.swing.JFileChooser();
            jLayeredPane1 = new javax.swing.JLayeredPane();
            jLabel2 = new javax.swing.JLabel();
            rightButton = new javax.swing.JButton();
            leftButton = new javax.swing.JButton();
            jLabel4 = new javax.swing.JLabel();
            progressBar = new javax.swing.JProgressBar();
            previewImage = new javax.swing.JLabel();
            buyButton = new javax.swing.JButton();
            stockLabel = new javax.swing.JLabel();
            jComboBox1 = new javax.swing.JComboBox();
            nameField = new javax.swing.JTextField();
            contactField = new javax.swing.JTextField();
            addressField = new javax.swing.JTextField();
            jLabel1 = new javax.swing.JLabel();
            jMenuBar1 = new javax.swing.JMenuBar();
            jMenu1 = new javax.swing.JMenu();
            jMenuItem1 = new javax.swing.JMenuItem();
            jMenuItem2 = new javax.swing.JMenuItem();
            jMenu3 = new javax.swing.JMenu();
            jMenuItem4 = new javax.swing.JMenuItem();
            jMenu2 = new javax.swing.JMenu();
            jMenuItem3 = new javax.swing.JMenuItem();
    
            javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
            jDialog1.getContentPane().setLayout(jDialog1Layout);
            jDialog1Layout.setHorizontalGroup(
                jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 400, Short.MAX_VALUE)
                .addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jDialog1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jFileChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
            );
            jDialog1Layout.setVerticalGroup(
                jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 300, Short.MAX_VALUE)
                .addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jDialog1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jFileChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
            );
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
            setResizable(false);
    
            jLabel2.setText("                    .");
            jLabel2.setBounds(10, 520, 320, 20);
            jLayeredPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);
    
            rightButton.setText(">");
            rightButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    rightButtonActionPerformed(evt);
                }
            });
            rightButton.setBounds(620, 510, 41, 23);
            jLayeredPane1.add(rightButton, javax.swing.JLayeredPane.DEFAULT_LAYER);
    
            leftButton.setText("<");
            leftButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    leftButtonActionPerformed(evt);
                }
            });
            leftButton.setBounds(500, 510, 41, 23);
            jLayeredPane1.add(leftButton, javax.swing.JLayeredPane.DEFAULT_LAYER);
    
            jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12));
            jLabel4.setForeground(new java.awt.Color(209, 172, 47));
            jLabel4.setText("Browse");
            jLabel4.setBounds(560, 520, 50, 15);
            jLayeredPane1.add(jLabel4, javax.swing.JLayeredPane.DEFAULT_LAYER);
            progressBar.setBounds(0, 540, 730, 14);
            jLayeredPane1.add(progressBar, javax.swing.JLayeredPane.DEFAULT_LAYER);
    
            previewImage.setForeground(new java.awt.Color(204, 204, 204));
            previewImage.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mycounter/newFolder/newFolder/Sofa5.jpg"))); // NOI18N
            previewImage.setText("Preview..");
            previewImage.setCursor(new java.awt.Cursor(java.awt.Cursor.CROSSHAIR_CURSOR));
            previewImage.setMaximumSize(new java.awt.Dimension(120, 120));
            previewImage.setMinimumSize(new java.awt.Dimension(100, 100));
            previewImage.setPreferredSize(new java.awt.Dimension(100, 100));
            previewImage.setBounds(490, 340, 190, 160);
            jLayeredPane1.add(previewImage, javax.swing.JLayeredPane.DEFAULT_LAYER);
    
            buyButton.setBackground(new java.awt.Color(0, 0, 0));
            buyButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mycounter/Buy.jpg"))); // NOI18N
            buyButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
            buyButton.setSelected(true);
            buyButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    buyButtonActionPerformed(evt);
                }
            });
            buyButton.setBounds(570, 280, 110, 40);
            jLayeredPane1.add(buyButton, javax.swing.JLayeredPane.DEFAULT_LAYER);
    
            stockLabel.setFont(new java.awt.Font("Tahoma", 1, 14));
            stockLabel.setForeground(new java.awt.Color(255, 204, 51));
            stockLabel.setText("Stocks Left:    ..");
            stockLabel.setBounds(560, 260, 150, 17);
            jLayeredPane1.add(stockLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
    
            jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Chairs", "Sofa", "Tables", "Beds", "Desks", "Counters", "Cabinets", "Shelves", "Accessories" }));
            jComboBox1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jComboBox1ActionPerformed(evt);
                }
            });
            jComboBox1.setBounds(400, 230, 310, 20);
            jLayeredPane1.add(jComboBox1, javax.swing.JLayeredPane.DEFAULT_LAYER);
    
            nameField.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    nameFieldActionPerformed(evt);
                }
            });
            nameField.setBounds(200, 160, 310, 30);
            jLayeredPane1.add(nameField, javax.swing.JLayeredPane.DEFAULT_LAYER);
            contactField.setBounds(190, 310, 170, 30);
            jLayeredPane1.add(contactField, javax.swing.JLayeredPane.DEFAULT_LAYER);
            addressField.setBounds(130, 240, 200, 30);
            jLayeredPane1.add(addressField, javax.swing.JLayeredPane.DEFAULT_LAYER);
    
            jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mycounter/apartament1.jpg"))); // NOI18N
            jLabel1.setBounds(0, 0, 730, 550);
            jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);
    
            jMenu1.setText("File");
    
            jMenuItem1.setText("About");
            jMenu1.add(jMenuItem1);
    
            jMenuItem2.setText("Exit");
            jMenu1.add(jMenuItem2);
    
            jMenuBar1.add(jMenu1);
    
            jMenu3.setText("Check Order Confirmation");
    
            jMenuItem4.setText("Check Order Form");
            jMenu3.add(jMenuItem4);
    
            jMenuBar1.add(jMenu3);
    
            jMenu2.setText("Help");
    
            jMenuItem3.setText("How_to_Use");
            jMenu2.add(jMenuItem3);
    
            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)
                .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 731, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 550, javax.swing.GroupLayout.PREFERRED_SIZE)
            );
    
            pack();
        }// </editor-fold>
    
        private void nameFieldActionPerformed(java.awt.event.ActionEvent evt) {                                          
            // TODO add your handling code here:
        }                                         
    
        private void buyButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
            // TODO add your handling code here:
    }                                         
    
        private void leftButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
            // TODO add your handling code here:
        }                                          
    [COLOR="DarkRed"]     //this is where I can't get my imaged-Label to change..[/COLOR]
        private void rightButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
            int i = 1;
            i++;//incrementing i
    
            if(i == 1){
                previewImage.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mycounter/Chair2.jpg")));
                i = 2;
            }else if(i == 2 ){
                previewImage.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mycounter/Chair3.jpg")));
                i = 3;
            }else if(i == 3){
                previewImage.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mycounter/Chair4.jpg")));
                i = 4;
            }else if(i == 4){
                previewImage.setIcon(new javax.swing.ImageIcon(getClass().getResource("mycounter/Chair5.jpg")));
                i = 5;
            }
        }                                           
    
        private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
            String obj = (String) evt.getSource();
            if(obj=="Sofa"){
                previewImage.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mycounter/Chair4.jpg")));
            }
        }                                          
    
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) throws ClassNotFoundException, InstantiationException, 
                                                        IllegalAccessException, UnsupportedLookAndFeelException {
    
            //This is the window's visual setting..
            UIManager.put("control", new Color(143,154,166));
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new CounterFrame().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify
        private javax.swing.JTextField addressField;
        private javax.swing.JButton buyButton;
        private javax.swing.JTextField contactField;
        private javax.swing.JComboBox jComboBox1;
        private javax.swing.JDialog jDialog1;
        private javax.swing.JFileChooser jFileChooser1;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JLayeredPane jLayeredPane1;
        private javax.swing.JMenu jMenu1;
        private javax.swing.JMenu jMenu2;
        private javax.swing.JMenu jMenu3;
        private javax.swing.JMenuBar jMenuBar1;
        private javax.swing.JMenuItem jMenuItem1;
        private javax.swing.JMenuItem jMenuItem2;
        private javax.swing.JMenuItem jMenuItem3;
        private javax.swing.JMenuItem jMenuItem4;
        private javax.swing.JButton leftButton;
        private javax.swing.JTextField nameField;
        private javax.swing.JLabel previewImage;
        private javax.swing.JProgressBar progressBar;
        private javax.swing.JButton rightButton;
        private javax.swing.JLabel stockLabel;
        // End of variables declaration
    
    }
    [/FONT]

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    it is because you initialize int i and increment it to 2 in rightButtonActionPreformed method

    it always shows the second pic
    you can add a global variable to store which pic should shown

  3. #3
    ashton is offline Member
    Join Date
    Jan 2009
    Posts
    31
    Rep Power
    0

    Default

    what do you suppose I do?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Again, as was already stated, don't declare and initialize "i" from within the actionperformed method. Declare it and initialize it to 0 in the class.

Similar Threads

  1. Double clicking executables jar files?
    By cruxblack in forum New To Java
    Replies: 6
    Last Post: 04-23-2009, 03:00 AM
  2. painting lines when clicking a panel
    By yuriythebest in forum New To Java
    Replies: 0
    Last Post: 12-14-2008, 10:56 PM
  3. Arrow Button Example
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 05:44 PM
  4. Draw an arrow
    By Albert in forum SWT / JFace
    Replies: 3
    Last Post: 02-01-2008, 09:11 AM
  5. Java3D: Clicking and getting coordinates?
    By seabhcan in forum Advanced Java
    Replies: 0
    Last Post: 01-11-2008, 03:46 PM

Posting Permissions

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