Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-02-2008, 03:10 PM
Member
 
Join Date: May 2008
Posts: 18
impact is on a distinguished road
[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

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 */ 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; } } /** * 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)); There is an error here which states content pane is already defined. //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(); } }); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-02-2008, 08:24 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
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(); } }); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-02-2008, 09:43 PM
Member
 
Join Date: May 2008
Posts: 18
impact is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in adding sound. shanky_sanks Java Applets 6 03-29-2008 09:37 PM
HashMap and ComboBox banie AWT / Swing 2 03-26-2008 12:58 AM
Adding taglibs in JSP Java Tip Java Tips 0 01-14-2008 12:43 AM
ComboBox with database options Goldy Advanced Java 0 12-01-2007 10:43 PM
Adding a Restock Fee Nexcompac New To Java 2 07-31-2007 03:46 PM


All times are GMT +3. The time now is 12:37 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org