Results 1 to 3 of 3
  1. #1
    impact is offline Member
    Join Date
    May 2008
    Posts
    18
    Rep Power
    0

    Default [SOLVED] Adding ComboBox

    Hi,
    I am trying to add a combo box to frame which already contains a button. I have done the following code but for some reason it doesn't work. Can you please show me where I have gone wrong?

    The code in red is the code for the combo box. The rest of the code is for button which is working fine without the combo box.

    Thank you

    Java Code:
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package test;
    
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Font;
    import javax.swing.AbstractButton;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import javax.swing.BorderFactory;
    import javax.swing.ImageIcon;
    import javax.swing.JComboBox;
    import javax.swing.JComponent;
    import javax.swing.JLabel;
    
    /**
     *
     * @author Owner
     */
    public class NewClass extends JPanel implements ActionListener {
        private JLabel picture;
    protected JButton b1;
    
        public void Main() {
           
            b1 = new JButton("Disable middle button");
            b1.setVerticalTextPosition(AbstractButton.CENTER);
            b1.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for left-to-right locales
            b1.setMnemonic(KeyEvent.VK_D);
            b1.setActionCommand("disable");
    
    
            //Listen for actions on buttons 1 and 3.
            b1.addActionListener(this);
           
    
            b1.setToolTipText("Go");
            
    
            //Add Components to this container, using the default FlowLayout.
            add(b1);
           
        }
    
        public void actionPerformed(ActionEvent e) {
            if ("disable".equals(e.getActionCommand())) {
                
                b1.setEnabled(true);
                
            } else {
               
                b1.setEnabled(true);
                
            }
        }
    
        /** Returns an ImageIcon, or null if the path was invalid. */
      
    
        /**
         * Combo box
         */
        
        [COLOR="Red"]public void ComboBoxDemo() {
         // super(new BorderLayout());
            String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
            
            //Create the combo box, select the item at index 0.
            JComboBox petList = new JComboBox(petStrings);
            petList.setSelectedIndex(0);
            petList.addActionListener(this);
            
            //Lay out the demo.
            picture = new JLabel();
            picture.setFont(picture.getFont().deriveFont(Font.ITALIC));
            picture.setHorizontalAlignment(JLabel.CENTER);
            add(petList, BorderLayout.PAGE_START);
            add(picture, BorderLayout.PAGE_END);
            setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
        }
        
        public void comActionPerformed(ActionEvent e) {
            JComboBox cb = (JComboBox)e.getSource();
            String petName = (String)cb.getSelectedItem();
            updateLabel(petName);
        }
            
        protected void updateLabel(String name) {
            ImageIcon icon = createImageIcon("images/" + name + ".gif");
            picture.setIcon(icon);
            picture.setToolTipText("A drawing of a " + name.toLowerCase());
            if (icon != null) {
                picture.setText(null);
            } else {
                picture.setText("Image not found");
            }
        }
    
        /** Returns an ImageIcon, or null if the path was invalid. */
        protected static ImageIcon createImageIcon(String path) {
            java.net.URL imgURL = NewClass.class.getResource(path);
            if (imgURL != null) {
                return new ImageIcon(imgURL);
            } else {
                System.err.println("Couldn't find file: " + path);
                return null;
            }
        }
        
        
        [/COLOR]
        /**
         * Create the GUI and show it.  For thread safety, 
         * this method should be invoked from the 
         * event-dispatching thread.
         */
        private static void createAndShowGUI() {
    
            //Create and set up the window.
            JFrame frame = new JFrame("ButtonDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Create and set up the content pane.
            Main newContentPane = new Main();
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(newContentPane);
            frame.setPreferredSize(new Dimension(300, 300));
    
           [COLOR="DarkOrange"] There is an error here which states content pane is already defined. [/COLOR]
            [COLOR="Red"]//Create and set up the content pane.
            
            JComponent newContentPane = new NewClass();
            
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(newContentPane);
            [/COLOR]
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI(); 
                }
            });
        }
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Font;
    import javax.swing.AbstractButton;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import javax.swing.BorderFactory;
    import javax.swing.ImageIcon;
    import javax.swing.JComboBox;
    import javax.swing.JComponent;
    import javax.swing.JLabel;
    
    public class NewClassRx extends JPanel implements ActionListener {
        private JLabel picture;
        protected JButton b1;
    
        // This method was not being called.
    //    public void Main() {
        // You could name it something like "initComponents"
        // or you could put the code inside a constructor:
        public NewClassRx() {     
            b1 = new JButton("Disable middle button");
            b1.setVerticalTextPosition(AbstractButton.CENTER);
            //aka LEFT, for left-to-right locales
            b1.setHorizontalTextPosition(AbstractButton.LEADING);
            b1.setMnemonic(KeyEvent.VK_D);
            b1.setActionCommand("disable");
    
    
            //Listen for actions on buttons 1 and 3.
            b1.addActionListener(this);
           
    
            b1.setToolTipText("Go");
            
    
            //Add Components to this container, using the default FlowLayout.
            add(b1);
            addComboBox();
        }
    
        public void actionPerformed(ActionEvent e) {
            if ("disable".equals(e.getActionCommand())) {
                
                b1.setEnabled(true);
                
            } else {
               
                b1.setEnabled(true);
                
            }
        }  
    
        /**
         * Combo box
         */
        
        public void addComboBox() {
         // super(new BorderLayout());
            String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
            
            //Create the combo box, select the item at index 0.
            JComboBox petList = new JComboBox(petStrings);
            petList.setSelectedIndex(0);
            petList.addActionListener(this);
            
            //Lay out the demo.
            picture = new JLabel();
            picture.setFont(picture.getFont().deriveFont(Font.ITALIC));
            picture.setHorizontalAlignment(JLabel.CENTER);
            add(petList, BorderLayout.PAGE_START);
            add(picture, BorderLayout.PAGE_END);
            setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
        }
        
        public void comActionPerformed(ActionEvent e) {
            JComboBox cb = (JComboBox)e.getSource();
            String petName = (String)cb.getSelectedItem();
            updateLabel(petName);
        }
            
        protected void updateLabel(String name) {
            ImageIcon icon = createImageIcon("images/" + name + ".gif");
            picture.setIcon(icon);
            picture.setToolTipText("A drawing of a " + name.toLowerCase());
            if (icon != null) {
                picture.setText(null);
            } else {
                picture.setText("Image not found");
            }
        }
    
        /** Returns an ImageIcon, or null if the path was invalid. */
        protected static ImageIcon createImageIcon(String path) {
            java.net.URL imgURL = NewClassRx.class.getResource(path);
            if (imgURL != null) {
                return new ImageIcon(imgURL);
            } else {
                System.err.println("Couldn't find file: " + path);
                return null;
            }
        }
    
        /**
         * Create the GUI and show it.  For thread safety, 
         * this method should be invoked from the 
         * event-dispatching thread.
         */
        private static void createAndShowGUI() {
    
            //Create and set up the window.
            JFrame frame = new JFrame("ButtonDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Create and set up the content pane.
            // Main is a different class, not defined here.
    //        Main newContentPane = new Main();
    
            // The enclosing class is NewClassRx:
            NewClassRx newContentPane = new NewClassRx();
    
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(newContentPane);
            frame.setPreferredSize(new Dimension(300, 300));
    
            //There is an error here which states content pane is already defined. 
            // Yes. You can see the reference "newContentPane" declared
            // above and below.
            //Create and set up the content pane.
    /*
            JComponent newContentPane = new NewClass();
            
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(newContentPane);
    */
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI(); 
                }
            });
        }
    }

  3. #3
    impact is offline Member
    Join Date
    May 2008
    Posts
    18
    Rep Power
    0

    Default

    I am afraid even your code dont show what I am looking for. Your code does open a window and it has only a button. I dont see the combobox in it. Do you know why?

    Thanks

Similar Threads

  1. Problem in adding sound.
    By shanky_sanks in forum Java Applets
    Replies: 6
    Last Post: 03-29-2008, 09:37 PM
  2. HashMap and ComboBox
    By banie in forum AWT / Swing
    Replies: 2
    Last Post: 03-26-2008, 12:58 AM
  3. Adding taglibs in JSP
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-14-2008, 12:43 AM
  4. ComboBox with database options
    By Goldy in forum Advanced Java
    Replies: 0
    Last Post: 12-01-2007, 10:43 PM
  5. Adding a Restock Fee
    By Nexcompac in forum New To Java
    Replies: 2
    Last Post: 07-31-2007, 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
  •